Commit | Line | Data |
---|---|---|
91d76f53 | 1 | /* |
21cf9b6b | 2 | * Copyright (C) 2011 EfficiOS Inc. |
ab5be9fa | 3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
91d76f53 | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
91d76f53 | 6 | * |
91d76f53 DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
d7bfb9b0 JG |
10 | |
11 | #include "buffer-registry.hpp" | |
12 | #include "condition-internal.hpp" | |
13 | #include "event-notifier-error-accounting.hpp" | |
14 | #include "event.hpp" | |
15 | #include "fd-limit.hpp" | |
16 | #include "field.hpp" | |
17 | #include "health-sessiond.hpp" | |
18 | #include "lttng-sessiond.hpp" | |
19 | #include "lttng-ust-ctl.hpp" | |
20 | #include "lttng-ust-error.hpp" | |
21 | #include "notification-thread-commands.hpp" | |
22 | #include "rotate.hpp" | |
23 | #include "session.hpp" | |
24 | #include "ust-app.hpp" | |
25 | #include "ust-consumer.hpp" | |
26 | #include "ust-field-convert.hpp" | |
27 | #include "utils.hpp" | |
28 | ||
29 | #include <common/bytecode/bytecode.hpp> | |
30 | #include <common/common.hpp> | |
31 | #include <common/compat/errno.hpp> | |
32 | #include <common/exception.hpp> | |
33 | #include <common/format.hpp> | |
34 | #include <common/hashtable/utils.hpp> | |
35 | #include <common/make-unique.hpp> | |
36 | #include <common/sessiond-comm/sessiond-comm.hpp> | |
37 | #include <common/urcu.hpp> | |
38 | ||
39 | #include <lttng/condition/condition.h> | |
40 | #include <lttng/condition/event-rule-matches-internal.hpp> | |
41 | #include <lttng/condition/event-rule-matches.h> | |
42 | #include <lttng/event-rule/event-rule-internal.hpp> | |
43 | #include <lttng/event-rule/event-rule.h> | |
44 | #include <lttng/event-rule/user-tracepoint.h> | |
45 | #include <lttng/trigger/trigger-internal.hpp> | |
46 | ||
533a90fb FD |
47 | #include <errno.h> |
48 | #include <fcntl.h> | |
7972aab2 | 49 | #include <inttypes.h> |
91d76f53 | 50 | #include <pthread.h> |
d7bfb9b0 | 51 | #include <signal.h> |
91d76f53 DG |
52 | #include <stdio.h> |
53 | #include <stdlib.h> | |
099e26bd | 54 | #include <string.h> |
533a90fb | 55 | #include <sys/mman.h> |
aba8e916 DG |
56 | #include <sys/stat.h> |
57 | #include <sys/types.h> | |
099e26bd | 58 | #include <unistd.h> |
0df502fd | 59 | #include <urcu/compiler.h> |
d7bfb9b0 | 60 | #include <vector> |
bec39940 | 61 | |
d7bfb9b0 JG |
62 | namespace lsu = lttng::sessiond::ust; |
63 | namespace lst = lttng::sessiond::trace; | |
d80a6244 | 64 | |
44cdb3a2 MJ |
65 | struct lttng_ht *ust_app_ht; |
66 | struct lttng_ht *ust_app_ht_by_sock; | |
67 | struct lttng_ht *ust_app_ht_by_notify_sock; | |
68 | ||
c4b88406 MD |
69 | static |
70 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); | |
71 | ||
d9bf3ca4 MD |
72 | /* Next available channel key. Access under next_channel_key_lock. */ |
73 | static uint64_t _next_channel_key; | |
74 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
75 | ||
76 | /* Next available session ID. Access under next_session_id_lock. */ | |
77 | static uint64_t _next_session_id; | |
78 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
ffe60014 | 79 | |
d7bfb9b0 JG |
80 | namespace { |
81 | ||
82 | /* | |
83 | * Return the session registry according to the buffer type of the given | |
84 | * session. | |
85 | * | |
86 | * A registry per UID object MUST exists before calling this function or else | |
87 | * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired. | |
88 | */ | |
b0f2e8db | 89 | static lsu::registry_session *get_session_registry( |
d7bfb9b0 JG |
90 | const struct ust_app_session *ua_sess) |
91 | { | |
b0f2e8db | 92 | lsu::registry_session *registry = NULL; |
d7bfb9b0 JG |
93 | |
94 | LTTNG_ASSERT(ua_sess); | |
95 | ||
96 | switch (ua_sess->buffer_type) { | |
97 | case LTTNG_BUFFER_PER_PID: | |
98 | { | |
99 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
100 | if (!reg_pid) { | |
101 | goto error; | |
102 | } | |
103 | registry = reg_pid->registry->reg.ust; | |
104 | break; | |
105 | } | |
106 | case LTTNG_BUFFER_PER_UID: | |
107 | { | |
108 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( | |
109 | ua_sess->tracing_id, ua_sess->bits_per_long, | |
110 | lttng_credentials_get_uid(&ua_sess->real_credentials)); | |
111 | if (!reg_uid) { | |
112 | goto error; | |
113 | } | |
114 | registry = reg_uid->registry->reg.ust; | |
115 | break; | |
116 | } | |
117 | default: | |
118 | abort(); | |
119 | }; | |
120 | ||
121 | error: | |
122 | return registry; | |
123 | } | |
124 | ||
b0f2e8db | 125 | lsu::registry_session::locked_ptr |
d7bfb9b0 JG |
126 | get_locked_session_registry(const struct ust_app_session *ua_sess) |
127 | { | |
128 | auto session = get_session_registry(ua_sess); | |
129 | if (session) { | |
130 | pthread_mutex_lock(&session->_lock); | |
131 | } | |
132 | ||
b0f2e8db | 133 | return lsu::registry_session::locked_ptr{session}; |
d7bfb9b0 JG |
134 | } |
135 | } /* namespace */ | |
136 | ||
ffe60014 | 137 | /* |
d9bf3ca4 | 138 | * Return the incremented value of next_channel_key. |
ffe60014 | 139 | */ |
d9bf3ca4 | 140 | static uint64_t get_next_channel_key(void) |
ffe60014 | 141 | { |
d9bf3ca4 MD |
142 | uint64_t ret; |
143 | ||
144 | pthread_mutex_lock(&next_channel_key_lock); | |
145 | ret = ++_next_channel_key; | |
146 | pthread_mutex_unlock(&next_channel_key_lock); | |
147 | return ret; | |
ffe60014 DG |
148 | } |
149 | ||
150 | /* | |
7972aab2 | 151 | * Return the atomically incremented value of next_session_id. |
ffe60014 | 152 | */ |
d9bf3ca4 | 153 | static uint64_t get_next_session_id(void) |
ffe60014 | 154 | { |
d9bf3ca4 MD |
155 | uint64_t ret; |
156 | ||
157 | pthread_mutex_lock(&next_session_id_lock); | |
158 | ret = ++_next_session_id; | |
159 | pthread_mutex_unlock(&next_session_id_lock); | |
160 | return ret; | |
ffe60014 DG |
161 | } |
162 | ||
d65d2de8 | 163 | static void copy_channel_attr_to_ustctl( |
b623cb6a | 164 | struct lttng_ust_ctl_consumer_channel_attr *attr, |
fc4b93fa | 165 | struct lttng_ust_abi_channel_attr *uattr) |
d65d2de8 DG |
166 | { |
167 | /* Copy event attributes since the layout is different. */ | |
168 | attr->subbuf_size = uattr->subbuf_size; | |
169 | attr->num_subbuf = uattr->num_subbuf; | |
170 | attr->overwrite = uattr->overwrite; | |
171 | attr->switch_timer_interval = uattr->switch_timer_interval; | |
172 | attr->read_timer_interval = uattr->read_timer_interval; | |
7966af57 | 173 | attr->output = (lttng_ust_abi_output) uattr->output; |
491d1539 | 174 | attr->blocking_timeout = uattr->u.s.blocking_timeout; |
d65d2de8 DG |
175 | } |
176 | ||
025faf73 DG |
177 | /* |
178 | * Match function for the hash table lookup. | |
179 | * | |
180 | * It matches an ust app event based on three attributes which are the event | |
181 | * name, the filter bytecode and the loglevel. | |
182 | */ | |
18eace3b DG |
183 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
184 | { | |
185 | struct ust_app_event *event; | |
186 | const struct ust_app_ht_key *key; | |
2106efa0 | 187 | int ev_loglevel_value; |
18eace3b | 188 | |
a0377dfe FD |
189 | LTTNG_ASSERT(node); |
190 | LTTNG_ASSERT(_key); | |
18eace3b DG |
191 | |
192 | event = caa_container_of(node, struct ust_app_event, node.node); | |
7966af57 | 193 | key = (ust_app_ht_key *) _key; |
2106efa0 | 194 | ev_loglevel_value = event->attr.loglevel; |
18eace3b | 195 | |
1af53eb5 | 196 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
18eace3b DG |
197 | |
198 | /* Event name */ | |
199 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
200 | goto no_match; | |
201 | } | |
202 | ||
203 | /* Event loglevel. */ | |
2106efa0 | 204 | if (ev_loglevel_value != key->loglevel_type) { |
fc4b93fa | 205 | if (event->attr.loglevel_type == LTTNG_UST_ABI_LOGLEVEL_ALL |
2106efa0 PP |
206 | && key->loglevel_type == 0 && |
207 | ev_loglevel_value == -1) { | |
025faf73 DG |
208 | /* |
209 | * Match is accepted. This is because on event creation, the | |
210 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and | |
211 | * -1 are accepted for this loglevel type since 0 is the one set by | |
212 | * the API when receiving an enable event. | |
213 | */ | |
214 | } else { | |
215 | goto no_match; | |
216 | } | |
18eace3b DG |
217 | } |
218 | ||
219 | /* One of the filters is NULL, fail. */ | |
220 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
221 | goto no_match; | |
222 | } | |
223 | ||
025faf73 DG |
224 | if (key->filter && event->filter) { |
225 | /* Both filters exists, check length followed by the bytecode. */ | |
226 | if (event->filter->len != key->filter->len || | |
227 | memcmp(event->filter->data, key->filter->data, | |
228 | event->filter->len) != 0) { | |
229 | goto no_match; | |
230 | } | |
18eace3b DG |
231 | } |
232 | ||
1af53eb5 JI |
233 | /* One of the exclusions is NULL, fail. */ |
234 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
235 | goto no_match; | |
236 | } | |
237 | ||
238 | if (key->exclusion && event->exclusion) { | |
239 | /* Both exclusions exists, check count followed by the names. */ | |
240 | if (event->exclusion->count != key->exclusion->count || | |
241 | memcmp(event->exclusion->names, key->exclusion->names, | |
fc4b93fa | 242 | event->exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) != 0) { |
1af53eb5 JI |
243 | goto no_match; |
244 | } | |
245 | } | |
246 | ||
247 | ||
025faf73 | 248 | /* Match. */ |
18eace3b DG |
249 | return 1; |
250 | ||
251 | no_match: | |
252 | return 0; | |
18eace3b DG |
253 | } |
254 | ||
025faf73 DG |
255 | /* |
256 | * Unique add of an ust app event in the given ht. This uses the custom | |
257 | * ht_match_ust_app_event match function and the event name as hash. | |
258 | */ | |
d0b96690 | 259 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
18eace3b DG |
260 | struct ust_app_event *event) |
261 | { | |
262 | struct cds_lfht_node *node_ptr; | |
263 | struct ust_app_ht_key key; | |
d0b96690 | 264 | struct lttng_ht *ht; |
18eace3b | 265 | |
a0377dfe FD |
266 | LTTNG_ASSERT(ua_chan); |
267 | LTTNG_ASSERT(ua_chan->events); | |
268 | LTTNG_ASSERT(event); | |
18eace3b | 269 | |
d0b96690 | 270 | ht = ua_chan->events; |
18eace3b DG |
271 | key.name = event->attr.name; |
272 | key.filter = event->filter; | |
7966af57 | 273 | key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel; |
91c89f23 | 274 | key.exclusion = event->exclusion; |
18eace3b DG |
275 | |
276 | node_ptr = cds_lfht_add_unique(ht->ht, | |
277 | ht->hash_fct(event->node.key, lttng_ht_seed), | |
278 | ht_match_ust_app_event, &key, &event->node.node); | |
a0377dfe | 279 | LTTNG_ASSERT(node_ptr == &event->node.node); |
18eace3b DG |
280 | } |
281 | ||
d88aee68 DG |
282 | /* |
283 | * Close the notify socket from the given RCU head object. This MUST be called | |
284 | * through a call_rcu(). | |
285 | */ | |
286 | static void close_notify_sock_rcu(struct rcu_head *head) | |
287 | { | |
288 | int ret; | |
289 | struct ust_app_notify_sock_obj *obj = | |
0114db0e | 290 | lttng::utils::container_of(head, &ust_app_notify_sock_obj::head); |
d88aee68 DG |
291 | |
292 | /* Must have a valid fd here. */ | |
a0377dfe | 293 | LTTNG_ASSERT(obj->fd >= 0); |
d88aee68 DG |
294 | |
295 | ret = close(obj->fd); | |
296 | if (ret) { | |
297 | ERR("close notify sock %d RCU", obj->fd); | |
298 | } | |
299 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
300 | ||
301 | free(obj); | |
302 | } | |
303 | ||
55cc08a6 DG |
304 | /* |
305 | * Delete ust context safely. RCU read lock must be held before calling | |
306 | * this function. | |
307 | */ | |
308 | static | |
fb45065e MD |
309 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, |
310 | struct ust_app *app) | |
55cc08a6 | 311 | { |
ffe60014 DG |
312 | int ret; |
313 | ||
a0377dfe | 314 | LTTNG_ASSERT(ua_ctx); |
48b7cdc2 | 315 | ASSERT_RCU_READ_LOCKED(); |
ffe60014 | 316 | |
55cc08a6 | 317 | if (ua_ctx->obj) { |
fb45065e | 318 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 319 | ret = lttng_ust_ctl_release_object(sock, ua_ctx->obj); |
fb45065e | 320 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
321 | if (ret < 0) { |
322 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
323 | DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d", | |
324 | app->pid, app->sock); | |
325 | } else if (ret == -EAGAIN) { | |
326 | WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d", | |
327 | app->pid, app->sock); | |
328 | } else { | |
329 | ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d", | |
330 | ua_ctx->obj->handle, ret, | |
331 | app->pid, app->sock); | |
332 | } | |
ffe60014 | 333 | } |
55cc08a6 DG |
334 | free(ua_ctx->obj); |
335 | } | |
336 | free(ua_ctx); | |
337 | } | |
338 | ||
d80a6244 DG |
339 | /* |
340 | * Delete ust app event safely. RCU read lock must be held before calling | |
341 | * this function. | |
342 | */ | |
8b366481 | 343 | static |
fb45065e MD |
344 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event, |
345 | struct ust_app *app) | |
d80a6244 | 346 | { |
ffe60014 DG |
347 | int ret; |
348 | ||
a0377dfe | 349 | LTTNG_ASSERT(ua_event); |
48b7cdc2 | 350 | ASSERT_RCU_READ_LOCKED(); |
ffe60014 | 351 | |
53a80697 | 352 | free(ua_event->filter); |
951f0b71 JI |
353 | if (ua_event->exclusion != NULL) |
354 | free(ua_event->exclusion); | |
edb67388 | 355 | if (ua_event->obj != NULL) { |
fb45065e | 356 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 357 | ret = lttng_ust_ctl_release_object(sock, ua_event->obj); |
fb45065e | 358 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
359 | if (ret < 0) { |
360 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
361 | DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d", | |
362 | app->pid, app->sock); | |
363 | } else if (ret == -EAGAIN) { | |
364 | WARN("UST app release event failed. Communication time out: pid = %d, sock = %d", | |
365 | app->pid, app->sock); | |
366 | } else { | |
367 | ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d", | |
368 | ret, app->pid, app->sock); | |
369 | } | |
ffe60014 | 370 | } |
edb67388 DG |
371 | free(ua_event->obj); |
372 | } | |
d80a6244 DG |
373 | free(ua_event); |
374 | } | |
375 | ||
993578ff JR |
376 | /* |
377 | * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called | |
378 | * through a call_rcu(). | |
379 | */ | |
380 | static | |
381 | void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head) | |
382 | { | |
0114db0e JG |
383 | struct ust_app_event_notifier_rule *obj = lttng::utils::container_of( |
384 | head, &ust_app_event_notifier_rule::rcu_head); | |
993578ff JR |
385 | |
386 | free(obj); | |
387 | } | |
388 | ||
389 | /* | |
390 | * Delete ust app event notifier rule safely. | |
391 | */ | |
392 | static void delete_ust_app_event_notifier_rule(int sock, | |
393 | struct ust_app_event_notifier_rule *ua_event_notifier_rule, | |
394 | struct ust_app *app) | |
395 | { | |
396 | int ret; | |
397 | ||
a0377dfe | 398 | LTTNG_ASSERT(ua_event_notifier_rule); |
993578ff JR |
399 | |
400 | if (ua_event_notifier_rule->exclusion != NULL) { | |
401 | free(ua_event_notifier_rule->exclusion); | |
402 | } | |
403 | ||
404 | if (ua_event_notifier_rule->obj != NULL) { | |
405 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 406 | ret = lttng_ust_ctl_release_object(sock, ua_event_notifier_rule->obj); |
993578ff | 407 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
408 | if (ret < 0) { |
409 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
410 | DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d", | |
411 | app->pid, app->sock); | |
412 | } else if (ret == -EAGAIN) { | |
413 | WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d", | |
414 | app->pid, app->sock); | |
415 | } else { | |
416 | ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d", | |
417 | ret, app->pid, app->sock); | |
418 | } | |
993578ff JR |
419 | } |
420 | ||
421 | free(ua_event_notifier_rule->obj); | |
422 | } | |
423 | ||
267d66aa | 424 | lttng_trigger_put(ua_event_notifier_rule->trigger); |
993578ff JR |
425 | call_rcu(&ua_event_notifier_rule->rcu_head, |
426 | free_ust_app_event_notifier_rule_rcu); | |
427 | } | |
428 | ||
d80a6244 | 429 | /* |
7972aab2 DG |
430 | * Release ust data object of the given stream. |
431 | * | |
432 | * Return 0 on success or else a negative value. | |
d80a6244 | 433 | */ |
fb45065e MD |
434 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, |
435 | struct ust_app *app) | |
d80a6244 | 436 | { |
7972aab2 | 437 | int ret = 0; |
ffe60014 | 438 | |
a0377dfe | 439 | LTTNG_ASSERT(stream); |
ffe60014 | 440 | |
8b366481 | 441 | if (stream->obj) { |
fb45065e | 442 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 443 | ret = lttng_ust_ctl_release_object(sock, stream->obj); |
fb45065e | 444 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
445 | if (ret < 0) { |
446 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
447 | DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d", | |
448 | app->pid, app->sock); | |
449 | } else if (ret == -EAGAIN) { | |
450 | WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d", | |
451 | app->pid, app->sock); | |
452 | } else { | |
453 | ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d", | |
454 | ret, app->pid, app->sock); | |
455 | } | |
ffe60014 | 456 | } |
4063050c | 457 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
458 | free(stream->obj); |
459 | } | |
7972aab2 DG |
460 | |
461 | return ret; | |
462 | } | |
463 | ||
464 | /* | |
465 | * Delete ust app stream safely. RCU read lock must be held before calling | |
466 | * this function. | |
467 | */ | |
468 | static | |
fb45065e MD |
469 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream, |
470 | struct ust_app *app) | |
7972aab2 | 471 | { |
a0377dfe | 472 | LTTNG_ASSERT(stream); |
48b7cdc2 | 473 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 | 474 | |
fb45065e | 475 | (void) release_ust_app_stream(sock, stream, app); |
84cd17c6 | 476 | free(stream); |
d80a6244 DG |
477 | } |
478 | ||
36b588ed MD |
479 | static |
480 | void delete_ust_app_channel_rcu(struct rcu_head *head) | |
481 | { | |
482 | struct ust_app_channel *ua_chan = | |
0114db0e | 483 | lttng::utils::container_of(head, &ust_app_channel::rcu_head); |
36b588ed | 484 | |
3c339053 FD |
485 | lttng_ht_destroy(ua_chan->ctx); |
486 | lttng_ht_destroy(ua_chan->events); | |
36b588ed MD |
487 | free(ua_chan); |
488 | } | |
489 | ||
fb83fe64 JD |
490 | /* |
491 | * Extract the lost packet or discarded events counter when the channel is | |
492 | * being deleted and store the value in the parent channel so we can | |
493 | * access it from lttng list and at stop/destroy. | |
82cac6d2 JG |
494 | * |
495 | * The session list lock must be held by the caller. | |
fb83fe64 JD |
496 | */ |
497 | static | |
498 | void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan) | |
499 | { | |
500 | uint64_t discarded = 0, lost = 0; | |
501 | struct ltt_session *session; | |
502 | struct ltt_ust_channel *uchan; | |
503 | ||
fc4b93fa | 504 | if (ua_chan->attr.type != LTTNG_UST_ABI_CHAN_PER_CPU) { |
fb83fe64 JD |
505 | return; |
506 | } | |
507 | ||
508 | rcu_read_lock(); | |
509 | session = session_find_by_id(ua_chan->session->tracing_id); | |
d68ec974 JG |
510 | if (!session || !session->ust_session) { |
511 | /* | |
512 | * Not finding the session is not an error because there are | |
513 | * multiple ways the channels can be torn down. | |
514 | * | |
515 | * 1) The session daemon can initiate the destruction of the | |
516 | * ust app session after receiving a destroy command or | |
517 | * during its shutdown/teardown. | |
518 | * 2) The application, since we are in per-pid tracing, is | |
519 | * unregistering and tearing down its ust app session. | |
520 | * | |
521 | * Both paths are protected by the session list lock which | |
522 | * ensures that the accounting of lost packets and discarded | |
523 | * events is done exactly once. The session is then unpublished | |
524 | * from the session list, resulting in this condition. | |
525 | */ | |
fb83fe64 JD |
526 | goto end; |
527 | } | |
528 | ||
529 | if (ua_chan->attr.overwrite) { | |
530 | consumer_get_lost_packets(ua_chan->session->tracing_id, | |
531 | ua_chan->key, session->ust_session->consumer, | |
532 | &lost); | |
533 | } else { | |
534 | consumer_get_discarded_events(ua_chan->session->tracing_id, | |
535 | ua_chan->key, session->ust_session->consumer, | |
536 | &discarded); | |
537 | } | |
538 | uchan = trace_ust_find_channel_by_name( | |
539 | session->ust_session->domain_global.channels, | |
540 | ua_chan->name); | |
541 | if (!uchan) { | |
542 | ERR("Missing UST channel to store discarded counters"); | |
543 | goto end; | |
544 | } | |
545 | ||
546 | uchan->per_pid_closed_app_discarded += discarded; | |
547 | uchan->per_pid_closed_app_lost += lost; | |
548 | ||
549 | end: | |
550 | rcu_read_unlock(); | |
e32d7f27 JG |
551 | if (session) { |
552 | session_put(session); | |
553 | } | |
fb83fe64 JD |
554 | } |
555 | ||
d80a6244 DG |
556 | /* |
557 | * Delete ust app channel safely. RCU read lock must be held before calling | |
558 | * this function. | |
82cac6d2 JG |
559 | * |
560 | * The session list lock must be held by the caller. | |
d80a6244 | 561 | */ |
d7bfb9b0 JG |
562 | static void delete_ust_app_channel(int sock, |
563 | struct ust_app_channel *ua_chan, | |
564 | struct ust_app *app, | |
b0f2e8db | 565 | const lsu::registry_session::locked_ptr& locked_registry) |
d80a6244 DG |
566 | { |
567 | int ret; | |
bec39940 | 568 | struct lttng_ht_iter iter; |
d80a6244 | 569 | struct ust_app_event *ua_event; |
55cc08a6 | 570 | struct ust_app_ctx *ua_ctx; |
030a66fa | 571 | struct ust_app_stream *stream, *stmp; |
d80a6244 | 572 | |
a0377dfe | 573 | LTTNG_ASSERT(ua_chan); |
48b7cdc2 | 574 | ASSERT_RCU_READ_LOCKED(); |
ffe60014 DG |
575 | |
576 | DBG3("UST app deleting channel %s", ua_chan->name); | |
577 | ||
55cc08a6 | 578 | /* Wipe stream */ |
d80a6244 | 579 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
84cd17c6 | 580 | cds_list_del(&stream->list); |
fb45065e | 581 | delete_ust_app_stream(sock, stream, app); |
d80a6244 DG |
582 | } |
583 | ||
55cc08a6 | 584 | /* Wipe context */ |
bec39940 | 585 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
31746f93 | 586 | cds_list_del(&ua_ctx->list); |
bec39940 | 587 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
a0377dfe | 588 | LTTNG_ASSERT(!ret); |
fb45065e | 589 | delete_ust_app_ctx(sock, ua_ctx, app); |
55cc08a6 | 590 | } |
d80a6244 | 591 | |
55cc08a6 | 592 | /* Wipe events */ |
bec39940 DG |
593 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
594 | node.node) { | |
595 | ret = lttng_ht_del(ua_chan->events, &iter); | |
a0377dfe | 596 | LTTNG_ASSERT(!ret); |
fb45065e | 597 | delete_ust_app_event(sock, ua_event, app); |
d80a6244 | 598 | } |
edb67388 | 599 | |
c8335706 MD |
600 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
601 | /* Wipe and free registry from session registry. */ | |
d7bfb9b0 JG |
602 | if (locked_registry) { |
603 | try { | |
604 | locked_registry->remove_channel(ua_chan->key, sock >= 0); | |
605 | } catch (const std::exception &ex) { | |
606 | DBG("Could not find channel for removal: %s", ex.what()); | |
607 | } | |
e38d96f9 | 608 | } |
d7bfb9b0 | 609 | |
45798a31 JG |
610 | /* |
611 | * A negative socket can be used by the caller when | |
612 | * cleaning-up a ua_chan in an error path. Skip the | |
613 | * accounting in this case. | |
614 | */ | |
e38d96f9 MD |
615 | if (sock >= 0) { |
616 | save_per_pid_lost_discarded_counters(ua_chan); | |
c8335706 | 617 | } |
7972aab2 | 618 | } |
d0b96690 | 619 | |
edb67388 | 620 | if (ua_chan->obj != NULL) { |
d0b96690 DG |
621 | /* Remove channel from application UST object descriptor. */ |
622 | iter.iter.node = &ua_chan->ust_objd_node.node; | |
c6e62271 | 623 | ret = lttng_ht_del(app->ust_objd, &iter); |
a0377dfe | 624 | LTTNG_ASSERT(!ret); |
fb45065e | 625 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 626 | ret = lttng_ust_ctl_release_object(sock, ua_chan->obj); |
fb45065e | 627 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
628 | if (ret < 0) { |
629 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
630 | DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d", | |
631 | ua_chan->name, app->pid, | |
632 | app->sock); | |
633 | } else if (ret == -EAGAIN) { | |
634 | WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d", | |
635 | ua_chan->name, app->pid, | |
636 | app->sock); | |
637 | } else { | |
638 | ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d", | |
639 | ua_chan->name, ret, app->pid, | |
640 | app->sock); | |
641 | } | |
ffe60014 | 642 | } |
7972aab2 | 643 | lttng_fd_put(LTTNG_FD_APPS, 1); |
edb67388 DG |
644 | free(ua_chan->obj); |
645 | } | |
36b588ed | 646 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
d80a6244 DG |
647 | } |
648 | ||
fb45065e MD |
649 | int ust_app_register_done(struct ust_app *app) |
650 | { | |
651 | int ret; | |
652 | ||
653 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 654 | ret = lttng_ust_ctl_register_done(app->sock); |
fb45065e MD |
655 | pthread_mutex_unlock(&app->sock_lock); |
656 | return ret; | |
657 | } | |
658 | ||
fc4b93fa | 659 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data) |
fb45065e MD |
660 | { |
661 | int ret, sock; | |
662 | ||
663 | if (app) { | |
664 | pthread_mutex_lock(&app->sock_lock); | |
665 | sock = app->sock; | |
666 | } else { | |
667 | sock = -1; | |
668 | } | |
b623cb6a | 669 | ret = lttng_ust_ctl_release_object(sock, data); |
fb45065e MD |
670 | if (app) { |
671 | pthread_mutex_unlock(&app->sock_lock); | |
672 | } | |
673 | return ret; | |
674 | } | |
675 | ||
331744e3 | 676 | /* |
1b532a60 DG |
677 | * Push metadata to consumer socket. |
678 | * | |
0f1b1d25 | 679 | * RCU read-side lock must be held to guarantee existence of socket. |
dc2bbdae MD |
680 | * Must be called with the ust app session lock held. |
681 | * Must be called with the registry lock held. | |
331744e3 JD |
682 | * |
683 | * On success, return the len of metadata pushed or else a negative value. | |
2c57e06d MD |
684 | * Returning a -EPIPE return value means we could not send the metadata, |
685 | * but it can be caused by recoverable errors (e.g. the application has | |
686 | * terminated concurrently). | |
331744e3 | 687 | */ |
b0f2e8db | 688 | ssize_t ust_app_push_metadata(const lsu::registry_session::locked_ptr& locked_registry, |
d7bfb9b0 JG |
689 | struct consumer_socket *socket, |
690 | int send_zero_data) | |
331744e3 JD |
691 | { |
692 | int ret; | |
693 | char *metadata_str = NULL; | |
c585821b | 694 | size_t len, offset, new_metadata_len_sent; |
331744e3 | 695 | ssize_t ret_val; |
93ec662e | 696 | uint64_t metadata_key, metadata_version; |
331744e3 | 697 | |
d7bfb9b0 | 698 | LTTNG_ASSERT(locked_registry); |
a0377dfe | 699 | LTTNG_ASSERT(socket); |
48b7cdc2 | 700 | ASSERT_RCU_READ_LOCKED(); |
1b532a60 | 701 | |
d7bfb9b0 | 702 | metadata_key = locked_registry->_metadata_key; |
c585821b | 703 | |
ce34fcd0 | 704 | /* |
dc2bbdae MD |
705 | * Means that no metadata was assigned to the session. This can |
706 | * happens if no start has been done previously. | |
ce34fcd0 | 707 | */ |
c585821b | 708 | if (!metadata_key) { |
ce34fcd0 MD |
709 | return 0; |
710 | } | |
711 | ||
d7bfb9b0 JG |
712 | offset = locked_registry->_metadata_len_sent; |
713 | len = locked_registry->_metadata_len - locked_registry->_metadata_len_sent; | |
714 | new_metadata_len_sent = locked_registry->_metadata_len; | |
715 | metadata_version = locked_registry->_metadata_version; | |
331744e3 JD |
716 | if (len == 0) { |
717 | DBG3("No metadata to push for metadata key %" PRIu64, | |
d7bfb9b0 | 718 | locked_registry->_metadata_key); |
331744e3 JD |
719 | ret_val = len; |
720 | if (send_zero_data) { | |
721 | DBG("No metadata to push"); | |
722 | goto push_data; | |
723 | } | |
724 | goto end; | |
725 | } | |
726 | ||
727 | /* Allocate only what we have to send. */ | |
64803277 | 728 | metadata_str = calloc<char>(len); |
331744e3 JD |
729 | if (!metadata_str) { |
730 | PERROR("zmalloc ust app metadata string"); | |
731 | ret_val = -ENOMEM; | |
732 | goto error; | |
733 | } | |
c585821b | 734 | /* Copy what we haven't sent out. */ |
d7bfb9b0 | 735 | memcpy(metadata_str, locked_registry->_metadata + offset, len); |
331744e3 JD |
736 | |
737 | push_data: | |
d7bfb9b0 | 738 | pthread_mutex_unlock(&locked_registry->_lock); |
c585821b MD |
739 | /* |
740 | * We need to unlock the registry while we push metadata to | |
741 | * break a circular dependency between the consumerd metadata | |
742 | * lock and the sessiond registry lock. Indeed, pushing metadata | |
743 | * to the consumerd awaits that it gets pushed all the way to | |
744 | * relayd, but doing so requires grabbing the metadata lock. If | |
745 | * a concurrent metadata request is being performed by | |
746 | * consumerd, this can try to grab the registry lock on the | |
747 | * sessiond while holding the metadata lock on the consumer | |
748 | * daemon. Those push and pull schemes are performed on two | |
749 | * different bidirectionnal communication sockets. | |
750 | */ | |
751 | ret = consumer_push_metadata(socket, metadata_key, | |
93ec662e | 752 | metadata_str, len, offset, metadata_version); |
d7bfb9b0 | 753 | pthread_mutex_lock(&locked_registry->_lock); |
331744e3 | 754 | if (ret < 0) { |
000baf6a | 755 | /* |
dc2bbdae MD |
756 | * There is an acceptable race here between the registry |
757 | * metadata key assignment and the creation on the | |
758 | * consumer. The session daemon can concurrently push | |
759 | * metadata for this registry while being created on the | |
760 | * consumer since the metadata key of the registry is | |
761 | * assigned *before* it is setup to avoid the consumer | |
762 | * to ask for metadata that could possibly be not found | |
763 | * in the session daemon. | |
000baf6a | 764 | * |
dc2bbdae MD |
765 | * The metadata will get pushed either by the session |
766 | * being stopped or the consumer requesting metadata if | |
767 | * that race is triggered. | |
000baf6a DG |
768 | */ |
769 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { | |
770 | ret = 0; | |
c585821b MD |
771 | } else { |
772 | ERR("Error pushing metadata to consumer"); | |
000baf6a | 773 | } |
331744e3 JD |
774 | ret_val = ret; |
775 | goto error_push; | |
c585821b MD |
776 | } else { |
777 | /* | |
778 | * Metadata may have been concurrently pushed, since | |
779 | * we're not holding the registry lock while pushing to | |
780 | * consumer. This is handled by the fact that we send | |
781 | * the metadata content, size, and the offset at which | |
782 | * that metadata belongs. This may arrive out of order | |
783 | * on the consumer side, and the consumer is able to | |
784 | * deal with overlapping fragments. The consumer | |
785 | * supports overlapping fragments, which must be | |
786 | * contiguous starting from offset 0. We keep the | |
787 | * largest metadata_len_sent value of the concurrent | |
788 | * send. | |
789 | */ | |
d7bfb9b0 JG |
790 | locked_registry->_metadata_len_sent = |
791 | std::max(locked_registry->_metadata_len_sent, | |
c585821b | 792 | new_metadata_len_sent); |
331744e3 | 793 | } |
331744e3 JD |
794 | free(metadata_str); |
795 | return len; | |
796 | ||
797 | end: | |
798 | error: | |
ce34fcd0 MD |
799 | if (ret_val) { |
800 | /* | |
dc2bbdae MD |
801 | * On error, flag the registry that the metadata is |
802 | * closed. We were unable to push anything and this | |
803 | * means that either the consumer is not responding or | |
804 | * the metadata cache has been destroyed on the | |
805 | * consumer. | |
ce34fcd0 | 806 | */ |
d7bfb9b0 | 807 | locked_registry->_metadata_closed = true; |
ce34fcd0 | 808 | } |
331744e3 JD |
809 | error_push: |
810 | free(metadata_str); | |
811 | return ret_val; | |
812 | } | |
813 | ||
d88aee68 | 814 | /* |
ce34fcd0 | 815 | * For a given application and session, push metadata to consumer. |
331744e3 JD |
816 | * Either sock or consumer is required : if sock is NULL, the default |
817 | * socket to send the metadata is retrieved from consumer, if sock | |
818 | * is not NULL we use it to send the metadata. | |
ce34fcd0 | 819 | * RCU read-side lock must be held while calling this function, |
0f1b1d25 | 820 | * therefore ensuring existence of registry. It also ensures existence |
dc2bbdae | 821 | * of socket throughout this function. |
d88aee68 DG |
822 | * |
823 | * Return 0 on success else a negative error. | |
2c57e06d MD |
824 | * Returning a -EPIPE return value means we could not send the metadata, |
825 | * but it can be caused by recoverable errors (e.g. the application has | |
826 | * terminated concurrently). | |
d88aee68 | 827 | */ |
b0f2e8db | 828 | static int push_metadata(const lsu::registry_session::locked_ptr& locked_registry, |
7972aab2 | 829 | struct consumer_output *consumer) |
d88aee68 | 830 | { |
331744e3 JD |
831 | int ret_val; |
832 | ssize_t ret; | |
d88aee68 DG |
833 | struct consumer_socket *socket; |
834 | ||
d7bfb9b0 | 835 | LTTNG_ASSERT(locked_registry); |
a0377dfe | 836 | LTTNG_ASSERT(consumer); |
48b7cdc2 | 837 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 | 838 | |
d7bfb9b0 | 839 | if (locked_registry->_metadata_closed) { |
dc2bbdae MD |
840 | ret_val = -EPIPE; |
841 | goto error; | |
d88aee68 DG |
842 | } |
843 | ||
d88aee68 | 844 | /* Get consumer socket to use to push the metadata.*/ |
d7bfb9b0 | 845 | socket = consumer_find_socket_by_bitness(locked_registry->abi.bits_per_long, |
7972aab2 | 846 | consumer); |
d88aee68 | 847 | if (!socket) { |
331744e3 | 848 | ret_val = -1; |
ce34fcd0 | 849 | goto error; |
d88aee68 DG |
850 | } |
851 | ||
d7bfb9b0 | 852 | ret = ust_app_push_metadata(locked_registry, socket, 0); |
d88aee68 | 853 | if (ret < 0) { |
331744e3 | 854 | ret_val = ret; |
ce34fcd0 | 855 | goto error; |
d88aee68 | 856 | } |
d88aee68 DG |
857 | return 0; |
858 | ||
ce34fcd0 | 859 | error: |
331744e3 | 860 | return ret_val; |
d88aee68 DG |
861 | } |
862 | ||
863 | /* | |
864 | * Send to the consumer a close metadata command for the given session. Once | |
865 | * done, the metadata channel is deleted and the session metadata pointer is | |
dc2bbdae | 866 | * nullified. The session lock MUST be held unless the application is |
d88aee68 DG |
867 | * in the destroy path. |
868 | * | |
a70ac2f4 MD |
869 | * Do not hold the registry lock while communicating with the consumerd, because |
870 | * doing so causes inter-process deadlocks between consumerd and sessiond with | |
871 | * the metadata request notification. | |
872 | * | |
d88aee68 DG |
873 | * Return 0 on success else a negative value. |
874 | */ | |
d7bfb9b0 | 875 | static int close_metadata(uint64_t metadata_key, unsigned int consumer_bitness, |
7972aab2 | 876 | struct consumer_output *consumer) |
d88aee68 DG |
877 | { |
878 | int ret; | |
879 | struct consumer_socket *socket; | |
d7bfb9b0 | 880 | lttng::urcu::read_lock_guard read_lock_guard; |
d88aee68 | 881 | |
a0377dfe | 882 | LTTNG_ASSERT(consumer); |
d88aee68 | 883 | |
d7bfb9b0 JG |
884 | /* Get consumer socket to use to push the metadata. */ |
885 | socket = consumer_find_socket_by_bitness(consumer_bitness, | |
7972aab2 | 886 | consumer); |
d88aee68 DG |
887 | if (!socket) { |
888 | ret = -1; | |
a70ac2f4 | 889 | goto end; |
d88aee68 DG |
890 | } |
891 | ||
a70ac2f4 | 892 | ret = consumer_close_metadata(socket, metadata_key); |
d88aee68 | 893 | if (ret < 0) { |
a70ac2f4 | 894 | goto end; |
d88aee68 DG |
895 | } |
896 | ||
1b532a60 | 897 | end: |
d88aee68 DG |
898 | return ret; |
899 | } | |
900 | ||
36b588ed MD |
901 | static |
902 | void delete_ust_app_session_rcu(struct rcu_head *head) | |
903 | { | |
904 | struct ust_app_session *ua_sess = | |
0114db0e | 905 | lttng::utils::container_of(head, &ust_app_session::rcu_head); |
36b588ed | 906 | |
3c339053 | 907 | lttng_ht_destroy(ua_sess->channels); |
604f8e58 | 908 | delete (ua_sess); |
36b588ed MD |
909 | } |
910 | ||
d80a6244 DG |
911 | /* |
912 | * Delete ust app session safely. RCU read lock must be held before calling | |
913 | * this function. | |
82cac6d2 JG |
914 | * |
915 | * The session list lock must be held by the caller. | |
d80a6244 | 916 | */ |
8b366481 | 917 | static |
d0b96690 DG |
918 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
919 | struct ust_app *app) | |
d80a6244 DG |
920 | { |
921 | int ret; | |
bec39940 | 922 | struct lttng_ht_iter iter; |
d80a6244 DG |
923 | struct ust_app_channel *ua_chan; |
924 | ||
a0377dfe | 925 | LTTNG_ASSERT(ua_sess); |
48b7cdc2 | 926 | ASSERT_RCU_READ_LOCKED(); |
d88aee68 | 927 | |
1b532a60 DG |
928 | pthread_mutex_lock(&ua_sess->lock); |
929 | ||
a0377dfe | 930 | LTTNG_ASSERT(!ua_sess->deleted); |
b161602a MD |
931 | ua_sess->deleted = true; |
932 | ||
d7bfb9b0 | 933 | auto locked_registry = get_locked_session_registry(ua_sess); |
fad1ed2f | 934 | /* Registry can be null on error path during initialization. */ |
d7bfb9b0 | 935 | if (locked_registry) { |
d88aee68 | 936 | /* Push metadata for application before freeing the application. */ |
d7bfb9b0 JG |
937 | (void) push_metadata(locked_registry, ua_sess->consumer); |
938 | } | |
d88aee68 | 939 | |
d7bfb9b0 JG |
940 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
941 | node.node) { | |
942 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
943 | LTTNG_ASSERT(!ret); | |
944 | delete_ust_app_channel(sock, ua_chan, app, locked_registry); | |
945 | } | |
946 | ||
947 | if (locked_registry) { | |
7972aab2 DG |
948 | /* |
949 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
950 | * metadata only on destroy trace session in this case. Also, the |
951 | * previous push metadata could have flag the metadata registry to | |
952 | * close so don't send a close command if closed. | |
7972aab2 | 953 | */ |
ce34fcd0 | 954 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
d7bfb9b0 JG |
955 | const auto metadata_key = locked_registry->_metadata_key; |
956 | const auto consumer_bitness = locked_registry->abi.bits_per_long; | |
d80a6244 | 957 | |
d7bfb9b0 JG |
958 | if (!locked_registry->_metadata_closed && metadata_key != 0) { |
959 | locked_registry->_metadata_closed = true; | |
960 | } | |
961 | ||
962 | /* Release lock before communication, see comments in close_metadata(). */ | |
963 | locked_registry.reset(); | |
964 | (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer); | |
965 | } | |
d80a6244 | 966 | } |
d80a6244 | 967 | |
7972aab2 DG |
968 | /* In case of per PID, the registry is kept in the session. */ |
969 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
970 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
971 | if (reg_pid) { | |
fad1ed2f JR |
972 | /* |
973 | * Registry can be null on error path during | |
974 | * initialization. | |
975 | */ | |
7972aab2 DG |
976 | buffer_reg_pid_remove(reg_pid); |
977 | buffer_reg_pid_destroy(reg_pid); | |
978 | } | |
979 | } | |
d0b96690 | 980 | |
aee6bafd | 981 | if (ua_sess->handle != -1) { |
fb45065e | 982 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 983 | ret = lttng_ust_ctl_release_handle(sock, ua_sess->handle); |
fb45065e | 984 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
985 | if (ret < 0) { |
986 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
987 | DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d", | |
988 | app->pid, app->sock); | |
989 | } else if (ret == -EAGAIN) { | |
990 | WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d", | |
991 | app->pid, app->sock); | |
992 | } else { | |
993 | ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d", | |
994 | ret, app->pid, app->sock); | |
995 | } | |
ffe60014 | 996 | } |
be355079 | 997 | |
10b56aef MD |
998 | /* Remove session from application UST object descriptor. */ |
999 | iter.iter.node = &ua_sess->ust_objd_node.node; | |
1000 | ret = lttng_ht_del(app->ust_sessions_objd, &iter); | |
a0377dfe | 1001 | LTTNG_ASSERT(!ret); |
aee6bafd | 1002 | } |
10b56aef | 1003 | |
1b532a60 DG |
1004 | pthread_mutex_unlock(&ua_sess->lock); |
1005 | ||
6addfa37 MD |
1006 | consumer_output_put(ua_sess->consumer); |
1007 | ||
36b588ed | 1008 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
d80a6244 | 1009 | } |
91d76f53 DG |
1010 | |
1011 | /* | |
284d8f55 DG |
1012 | * Delete a traceable application structure from the global list. Never call |
1013 | * this function outside of a call_rcu call. | |
91d76f53 | 1014 | */ |
8b366481 DG |
1015 | static |
1016 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 1017 | { |
8b366481 | 1018 | int ret, sock; |
d42f20df | 1019 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
993578ff JR |
1020 | struct lttng_ht_iter iter; |
1021 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5e2abfaf | 1022 | bool event_notifier_write_fd_is_open; |
44d3bd01 | 1023 | |
82cac6d2 JG |
1024 | /* |
1025 | * The session list lock must be held during this function to guarantee | |
1026 | * the existence of ua_sess. | |
1027 | */ | |
1028 | session_lock_list(); | |
d80a6244 | 1029 | /* Delete ust app sessions info */ |
852d0037 DG |
1030 | sock = app->sock; |
1031 | app->sock = -1; | |
d80a6244 | 1032 | |
8b366481 | 1033 | /* Wipe sessions */ |
d42f20df DG |
1034 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
1035 | teardown_node) { | |
1036 | /* Free every object in the session and the session. */ | |
36b588ed | 1037 | rcu_read_lock(); |
d0b96690 | 1038 | delete_ust_app_session(sock, ua_sess, app); |
36b588ed | 1039 | rcu_read_unlock(); |
d80a6244 | 1040 | } |
36b588ed | 1041 | |
993578ff JR |
1042 | /* Remove the event notifier rules associated with this app. */ |
1043 | rcu_read_lock(); | |
1044 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
1045 | &iter.iter, event_notifier_rule, node.node) { | |
1046 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter); | |
a0377dfe | 1047 | LTTNG_ASSERT(!ret); |
993578ff JR |
1048 | |
1049 | delete_ust_app_event_notifier_rule( | |
1050 | app->sock, event_notifier_rule, app); | |
1051 | } | |
1052 | ||
1053 | rcu_read_unlock(); | |
1054 | ||
3c339053 FD |
1055 | lttng_ht_destroy(app->sessions); |
1056 | lttng_ht_destroy(app->ust_sessions_objd); | |
1057 | lttng_ht_destroy(app->ust_objd); | |
1058 | lttng_ht_destroy(app->token_to_event_notifier_rule_ht); | |
d80a6244 | 1059 | |
da873412 JR |
1060 | /* |
1061 | * This could be NULL if the event notifier setup failed (e.g the app | |
1062 | * was killed or the tracer does not support this feature). | |
1063 | */ | |
1064 | if (app->event_notifier_group.object) { | |
1065 | enum lttng_error_code ret_code; | |
533a90fb FD |
1066 | enum event_notifier_error_accounting_status status; |
1067 | ||
da873412 JR |
1068 | const int event_notifier_read_fd = lttng_pipe_get_readfd( |
1069 | app->event_notifier_group.event_pipe); | |
1070 | ||
1071 | ret_code = notification_thread_command_remove_tracer_event_source( | |
412d7227 | 1072 | the_notification_thread_handle, |
da873412 JR |
1073 | event_notifier_read_fd); |
1074 | if (ret_code != LTTNG_OK) { | |
1075 | ERR("Failed to remove application tracer event source from notification thread"); | |
1076 | } | |
1077 | ||
533a90fb FD |
1078 | status = event_notifier_error_accounting_unregister_app(app); |
1079 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
1080 | ERR("Error unregistering app from event notifier error accounting"); | |
1081 | } | |
1082 | ||
b623cb6a | 1083 | lttng_ust_ctl_release_object(sock, app->event_notifier_group.object); |
da873412 JR |
1084 | free(app->event_notifier_group.object); |
1085 | } | |
1086 | ||
5e2abfaf JG |
1087 | event_notifier_write_fd_is_open = lttng_pipe_is_write_open( |
1088 | app->event_notifier_group.event_pipe); | |
da873412 | 1089 | lttng_pipe_destroy(app->event_notifier_group.event_pipe); |
5e2abfaf JG |
1090 | /* |
1091 | * Release the file descriptors reserved for the event notifier pipe. | |
1092 | * The app could be destroyed before the write end of the pipe could be | |
1093 | * passed to the application (and closed). In that case, both file | |
1094 | * descriptors must be released. | |
1095 | */ | |
1096 | lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1); | |
da873412 | 1097 | |
6414a713 | 1098 | /* |
852d0037 DG |
1099 | * Wait until we have deleted the application from the sock hash table |
1100 | * before closing this socket, otherwise an application could re-use the | |
1101 | * socket ID and race with the teardown, using the same hash table entry. | |
1102 | * | |
1103 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
1104 | * all RCU readers that could run concurrently with unregister app, | |
1105 | * therefore we _need_ to only close that socket after a grace period. So | |
1106 | * it should stay in this RCU callback. | |
1107 | * | |
1108 | * This close() is a very important step of the synchronization model so | |
1109 | * every modification to this function must be carefully reviewed. | |
6414a713 | 1110 | */ |
799e2c4f MD |
1111 | ret = close(sock); |
1112 | if (ret) { | |
1113 | PERROR("close"); | |
1114 | } | |
4063050c | 1115 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 1116 | |
852d0037 | 1117 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 1118 | free(app); |
82cac6d2 | 1119 | session_unlock_list(); |
099e26bd DG |
1120 | } |
1121 | ||
1122 | /* | |
f6a9efaa | 1123 | * URCU intermediate call to delete an UST app. |
099e26bd | 1124 | */ |
8b366481 DG |
1125 | static |
1126 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 1127 | { |
bec39940 | 1128 | struct lttng_ht_node_ulong *node = |
0114db0e | 1129 | lttng::utils::container_of(head, <tng_ht_node_ulong::head); |
f6a9efaa | 1130 | struct ust_app *app = |
0114db0e | 1131 | lttng::utils::container_of(node, &ust_app::pid_n); |
f6a9efaa | 1132 | |
852d0037 | 1133 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 1134 | delete_ust_app(app); |
099e26bd DG |
1135 | } |
1136 | ||
ffe60014 DG |
1137 | /* |
1138 | * Delete the session from the application ht and delete the data structure by | |
1139 | * freeing every object inside and releasing them. | |
82cac6d2 JG |
1140 | * |
1141 | * The session list lock must be held by the caller. | |
ffe60014 | 1142 | */ |
d0b96690 | 1143 | static void destroy_app_session(struct ust_app *app, |
ffe60014 DG |
1144 | struct ust_app_session *ua_sess) |
1145 | { | |
1146 | int ret; | |
1147 | struct lttng_ht_iter iter; | |
1148 | ||
a0377dfe FD |
1149 | LTTNG_ASSERT(app); |
1150 | LTTNG_ASSERT(ua_sess); | |
ffe60014 DG |
1151 | |
1152 | iter.iter.node = &ua_sess->node.node; | |
1153 | ret = lttng_ht_del(app->sessions, &iter); | |
1154 | if (ret) { | |
1155 | /* Already scheduled for teardown. */ | |
1156 | goto end; | |
1157 | } | |
1158 | ||
1159 | /* Once deleted, free the data structure. */ | |
d0b96690 | 1160 | delete_ust_app_session(app->sock, ua_sess, app); |
ffe60014 DG |
1161 | |
1162 | end: | |
1163 | return; | |
1164 | } | |
1165 | ||
8b366481 DG |
1166 | /* |
1167 | * Alloc new UST app session. | |
1168 | */ | |
1169 | static | |
40bbd087 | 1170 | struct ust_app_session *alloc_ust_app_session(void) |
8b366481 DG |
1171 | { |
1172 | struct ust_app_session *ua_sess; | |
1173 | ||
1174 | /* Init most of the default value by allocating and zeroing */ | |
604f8e58 | 1175 | ua_sess = new ust_app_session(); |
8b366481 DG |
1176 | if (ua_sess == NULL) { |
1177 | PERROR("malloc"); | |
ffe60014 | 1178 | goto error_free; |
8b366481 DG |
1179 | } |
1180 | ||
fc4b93fa | 1181 | ua_sess->metadata_attr.type = LTTNG_UST_ABI_CHAN_METADATA; |
84ad93e8 | 1182 | pthread_mutex_init(&ua_sess->lock, NULL); |
ad7a9107 | 1183 | |
8b366481 DG |
1184 | return ua_sess; |
1185 | ||
ffe60014 | 1186 | error_free: |
8b366481 DG |
1187 | return NULL; |
1188 | } | |
1189 | ||
1190 | /* | |
1191 | * Alloc new UST app channel. | |
1192 | */ | |
1193 | static | |
b53d4e59 | 1194 | struct ust_app_channel *alloc_ust_app_channel(const char *name, |
d0b96690 | 1195 | struct ust_app_session *ua_sess, |
fc4b93fa | 1196 | struct lttng_ust_abi_channel_attr *attr) |
8b366481 DG |
1197 | { |
1198 | struct ust_app_channel *ua_chan; | |
1199 | ||
1200 | /* Init most of the default value by allocating and zeroing */ | |
64803277 | 1201 | ua_chan = zmalloc<ust_app_channel>(); |
8b366481 DG |
1202 | if (ua_chan == NULL) { |
1203 | PERROR("malloc"); | |
1204 | goto error; | |
1205 | } | |
1206 | ||
1207 | /* Setup channel name */ | |
1208 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
1209 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
1210 | ||
1211 | ua_chan->enabled = 1; | |
1212 | ua_chan->handle = -1; | |
45893984 | 1213 | ua_chan->session = ua_sess; |
ffe60014 | 1214 | ua_chan->key = get_next_channel_key(); |
bec39940 DG |
1215 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
1216 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
1217 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
1218 | |
1219 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
31746f93 | 1220 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
8b366481 DG |
1221 | |
1222 | /* Copy attributes */ | |
1223 | if (attr) { | |
b623cb6a | 1224 | /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */ |
2fe6e7f5 DG |
1225 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
1226 | ua_chan->attr.num_subbuf = attr->num_subbuf; | |
1227 | ua_chan->attr.overwrite = attr->overwrite; | |
1228 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; | |
1229 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; | |
7966af57 | 1230 | ua_chan->attr.output = (lttng_ust_abi_output) attr->output; |
491d1539 | 1231 | ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout; |
8b366481 | 1232 | } |
ffe60014 | 1233 | /* By default, the channel is a per cpu channel. */ |
fc4b93fa | 1234 | ua_chan->attr.type = LTTNG_UST_ABI_CHAN_PER_CPU; |
8b366481 DG |
1235 | |
1236 | DBG3("UST app channel %s allocated", ua_chan->name); | |
1237 | ||
1238 | return ua_chan; | |
1239 | ||
1240 | error: | |
1241 | return NULL; | |
1242 | } | |
1243 | ||
37f1c236 DG |
1244 | /* |
1245 | * Allocate and initialize a UST app stream. | |
1246 | * | |
1247 | * Return newly allocated stream pointer or NULL on error. | |
1248 | */ | |
ffe60014 | 1249 | struct ust_app_stream *ust_app_alloc_stream(void) |
37f1c236 DG |
1250 | { |
1251 | struct ust_app_stream *stream = NULL; | |
1252 | ||
64803277 | 1253 | stream = zmalloc<ust_app_stream>(); |
37f1c236 DG |
1254 | if (stream == NULL) { |
1255 | PERROR("zmalloc ust app stream"); | |
1256 | goto error; | |
1257 | } | |
1258 | ||
1259 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
1260 | stream->handle = -1; | |
1261 | ||
1262 | error: | |
1263 | return stream; | |
1264 | } | |
1265 | ||
8b366481 DG |
1266 | /* |
1267 | * Alloc new UST app event. | |
1268 | */ | |
1269 | static | |
1270 | struct ust_app_event *alloc_ust_app_event(char *name, | |
fc4b93fa | 1271 | struct lttng_ust_abi_event *attr) |
8b366481 DG |
1272 | { |
1273 | struct ust_app_event *ua_event; | |
1274 | ||
1275 | /* Init most of the default value by allocating and zeroing */ | |
64803277 | 1276 | ua_event = zmalloc<ust_app_event>(); |
8b366481 | 1277 | if (ua_event == NULL) { |
20533947 | 1278 | PERROR("Failed to allocate ust_app_event structure"); |
8b366481 DG |
1279 | goto error; |
1280 | } | |
1281 | ||
1282 | ua_event->enabled = 1; | |
1283 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
1284 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 | 1285 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
8b366481 DG |
1286 | |
1287 | /* Copy attributes */ | |
1288 | if (attr) { | |
1289 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
1290 | } | |
1291 | ||
1292 | DBG3("UST app event %s allocated", ua_event->name); | |
1293 | ||
1294 | return ua_event; | |
1295 | ||
1296 | error: | |
1297 | return NULL; | |
1298 | } | |
1299 | ||
993578ff JR |
1300 | /* |
1301 | * Allocate a new UST app event notifier rule. | |
1302 | */ | |
1303 | static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule( | |
267d66aa | 1304 | struct lttng_trigger *trigger) |
993578ff JR |
1305 | { |
1306 | enum lttng_event_rule_generate_exclusions_status | |
1307 | generate_exclusion_status; | |
cc3b9644 | 1308 | enum lttng_condition_status cond_status; |
993578ff | 1309 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; |
267d66aa JR |
1310 | struct lttng_condition *condition = NULL; |
1311 | const struct lttng_event_rule *event_rule = NULL; | |
993578ff | 1312 | |
64803277 | 1313 | ua_event_notifier_rule = zmalloc<ust_app_event_notifier_rule>(); |
993578ff JR |
1314 | if (ua_event_notifier_rule == NULL) { |
1315 | PERROR("Failed to allocate ust_app_event_notifier_rule structure"); | |
1316 | goto error; | |
1317 | } | |
1318 | ||
1319 | ua_event_notifier_rule->enabled = 1; | |
267d66aa JR |
1320 | ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger); |
1321 | lttng_ht_node_init_u64(&ua_event_notifier_rule->node, | |
1322 | ua_event_notifier_rule->token); | |
993578ff | 1323 | |
267d66aa | 1324 | condition = lttng_trigger_get_condition(trigger); |
a0377dfe FD |
1325 | LTTNG_ASSERT(condition); |
1326 | LTTNG_ASSERT(lttng_condition_get_type(condition) == | |
8dbb86b8 | 1327 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
267d66aa | 1328 | |
cc3b9644 FD |
1329 | cond_status = lttng_condition_event_rule_matches_get_rule( |
1330 | condition, &event_rule); | |
a0377dfe FD |
1331 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
1332 | LTTNG_ASSERT(event_rule); | |
993578ff | 1333 | |
46e9a5fb | 1334 | ua_event_notifier_rule->error_counter_index = |
8dbb86b8 | 1335 | lttng_condition_event_rule_matches_get_error_counter_index(condition); |
267d66aa JR |
1336 | /* Acquire the event notifier's reference to the trigger. */ |
1337 | lttng_trigger_get(trigger); | |
1338 | ||
1339 | ua_event_notifier_rule->trigger = trigger; | |
993578ff JR |
1340 | ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
1341 | generate_exclusion_status = lttng_event_rule_generate_exclusions( | |
1342 | event_rule, &ua_event_notifier_rule->exclusion); | |
1343 | switch (generate_exclusion_status) { | |
1344 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK: | |
1345 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE: | |
1346 | break; | |
1347 | default: | |
8f0646a0 | 1348 | /* Error occurred. */ |
267d66aa JR |
1349 | ERR("Failed to generate exclusions from trigger while allocating an event notifier rule"); |
1350 | goto error_put_trigger; | |
993578ff JR |
1351 | } |
1352 | ||
1353 | DBG3("UST app event notifier rule allocated: token = %" PRIu64, | |
1354 | ua_event_notifier_rule->token); | |
1355 | ||
1356 | return ua_event_notifier_rule; | |
1357 | ||
267d66aa JR |
1358 | error_put_trigger: |
1359 | lttng_trigger_put(trigger); | |
993578ff JR |
1360 | error: |
1361 | free(ua_event_notifier_rule); | |
1362 | return NULL; | |
1363 | } | |
1364 | ||
8b366481 DG |
1365 | /* |
1366 | * Alloc new UST app context. | |
1367 | */ | |
1368 | static | |
bdf64013 | 1369 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) |
8b366481 DG |
1370 | { |
1371 | struct ust_app_ctx *ua_ctx; | |
1372 | ||
64803277 | 1373 | ua_ctx = zmalloc<ust_app_ctx>(); |
8b366481 DG |
1374 | if (ua_ctx == NULL) { |
1375 | goto error; | |
1376 | } | |
1377 | ||
31746f93 DG |
1378 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
1379 | ||
8b366481 DG |
1380 | if (uctx) { |
1381 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
fc4b93fa | 1382 | if (uctx->ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) { |
f3db82be | 1383 | char *provider_name = NULL, *ctx_name = NULL; |
bdf64013 JG |
1384 | |
1385 | provider_name = strdup(uctx->u.app_ctx.provider_name); | |
1386 | ctx_name = strdup(uctx->u.app_ctx.ctx_name); | |
1387 | if (!provider_name || !ctx_name) { | |
1388 | free(provider_name); | |
1389 | free(ctx_name); | |
1390 | goto error; | |
1391 | } | |
1392 | ||
1393 | ua_ctx->ctx.u.app_ctx.provider_name = provider_name; | |
1394 | ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name; | |
1395 | } | |
8b366481 DG |
1396 | } |
1397 | ||
1398 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
8b366481 | 1399 | return ua_ctx; |
bdf64013 JG |
1400 | error: |
1401 | free(ua_ctx); | |
1402 | return NULL; | |
8b366481 DG |
1403 | } |
1404 | ||
51755dc8 JG |
1405 | /* |
1406 | * Create a liblttng-ust filter bytecode from given bytecode. | |
1407 | * | |
1408 | * Return allocated filter or NULL on error. | |
1409 | */ | |
fc4b93fa MD |
1410 | static struct lttng_ust_abi_filter_bytecode *create_ust_filter_bytecode_from_bytecode( |
1411 | const struct lttng_bytecode *orig_f) | |
51755dc8 | 1412 | { |
fc4b93fa | 1413 | struct lttng_ust_abi_filter_bytecode *filter = NULL; |
51755dc8 | 1414 | |
f2eafd2d | 1415 | /* Copy filter bytecode. */ |
64803277 | 1416 | filter = zmalloc<lttng_ust_abi_filter_bytecode>(sizeof(*filter) + orig_f->len); |
51755dc8 | 1417 | if (!filter) { |
f2eafd2d | 1418 | PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); |
51755dc8 JG |
1419 | goto error; |
1420 | } | |
1421 | ||
a0377dfe | 1422 | LTTNG_ASSERT(sizeof(struct lttng_bytecode) == |
fc4b93fa | 1423 | sizeof(struct lttng_ust_abi_filter_bytecode)); |
51755dc8 JG |
1424 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); |
1425 | error: | |
1426 | return filter; | |
1427 | } | |
1428 | ||
f2eafd2d JR |
1429 | /* |
1430 | * Create a liblttng-ust capture bytecode from given bytecode. | |
1431 | * | |
1432 | * Return allocated filter or NULL on error. | |
1433 | */ | |
fc4b93fa | 1434 | static struct lttng_ust_abi_capture_bytecode * |
f2eafd2d JR |
1435 | create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) |
1436 | { | |
fc4b93fa | 1437 | struct lttng_ust_abi_capture_bytecode *capture = NULL; |
f2eafd2d JR |
1438 | |
1439 | /* Copy capture bytecode. */ | |
64803277 | 1440 | capture = zmalloc<lttng_ust_abi_capture_bytecode>(sizeof(*capture) + orig_f->len); |
f2eafd2d | 1441 | if (!capture) { |
fc4b93fa | 1442 | PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); |
f2eafd2d JR |
1443 | goto error; |
1444 | } | |
1445 | ||
a0377dfe | 1446 | LTTNG_ASSERT(sizeof(struct lttng_bytecode) == |
fc4b93fa | 1447 | sizeof(struct lttng_ust_abi_capture_bytecode)); |
f2eafd2d JR |
1448 | memcpy(capture, orig_f, sizeof(*capture) + orig_f->len); |
1449 | error: | |
1450 | return capture; | |
1451 | } | |
1452 | ||
099e26bd | 1453 | /* |
421cb601 DG |
1454 | * Find an ust_app using the sock and return it. RCU read side lock must be |
1455 | * held before calling this helper function. | |
099e26bd | 1456 | */ |
f20baf8e | 1457 | struct ust_app *ust_app_find_by_sock(int sock) |
099e26bd | 1458 | { |
bec39940 | 1459 | struct lttng_ht_node_ulong *node; |
bec39940 | 1460 | struct lttng_ht_iter iter; |
f6a9efaa | 1461 | |
48b7cdc2 FD |
1462 | ASSERT_RCU_READ_LOCKED(); |
1463 | ||
852d0037 | 1464 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1465 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
1466 | if (node == NULL) { |
1467 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
1468 | goto error; |
1469 | } | |
852d0037 | 1470 | |
0114db0e | 1471 | return lttng::utils::container_of(node, &ust_app::sock_n); |
f6a9efaa DG |
1472 | |
1473 | error: | |
1474 | return NULL; | |
099e26bd DG |
1475 | } |
1476 | ||
d0b96690 DG |
1477 | /* |
1478 | * Find an ust_app using the notify sock and return it. RCU read side lock must | |
1479 | * be held before calling this helper function. | |
1480 | */ | |
1481 | static struct ust_app *find_app_by_notify_sock(int sock) | |
1482 | { | |
1483 | struct lttng_ht_node_ulong *node; | |
1484 | struct lttng_ht_iter iter; | |
1485 | ||
48b7cdc2 FD |
1486 | ASSERT_RCU_READ_LOCKED(); |
1487 | ||
d0b96690 DG |
1488 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), |
1489 | &iter); | |
1490 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1491 | if (node == NULL) { | |
1492 | DBG2("UST app find by notify sock %d not found", sock); | |
1493 | goto error; | |
1494 | } | |
1495 | ||
0114db0e | 1496 | return lttng::utils::container_of(node, &ust_app::notify_sock_n); |
d0b96690 DG |
1497 | |
1498 | error: | |
1499 | return NULL; | |
1500 | } | |
1501 | ||
025faf73 DG |
1502 | /* |
1503 | * Lookup for an ust app event based on event name, filter bytecode and the | |
1504 | * event loglevel. | |
1505 | * | |
1506 | * Return an ust_app_event object or NULL on error. | |
1507 | */ | |
18eace3b | 1508 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
2b00d462 | 1509 | const char *name, const struct lttng_bytecode *filter, |
2106efa0 | 1510 | int loglevel_value, |
39c5a3a7 | 1511 | const struct lttng_event_exclusion *exclusion) |
18eace3b DG |
1512 | { |
1513 | struct lttng_ht_iter iter; | |
1514 | struct lttng_ht_node_str *node; | |
1515 | struct ust_app_event *event = NULL; | |
1516 | struct ust_app_ht_key key; | |
18eace3b | 1517 | |
a0377dfe FD |
1518 | LTTNG_ASSERT(name); |
1519 | LTTNG_ASSERT(ht); | |
18eace3b DG |
1520 | |
1521 | /* Setup key for event lookup. */ | |
1522 | key.name = name; | |
1523 | key.filter = filter; | |
7966af57 | 1524 | key.loglevel_type = (lttng_ust_abi_loglevel_type) loglevel_value; |
39c5a3a7 | 1525 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
51755dc8 | 1526 | key.exclusion = exclusion; |
18eace3b | 1527 | |
025faf73 DG |
1528 | /* Lookup using the event name as hash and a custom match fct. */ |
1529 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), | |
1530 | ht_match_ust_app_event, &key, &iter.iter); | |
18eace3b DG |
1531 | node = lttng_ht_iter_get_node_str(&iter); |
1532 | if (node == NULL) { | |
1533 | goto end; | |
1534 | } | |
1535 | ||
0114db0e | 1536 | event = lttng::utils::container_of(node, &ust_app_event::node); |
18eace3b DG |
1537 | |
1538 | end: | |
18eace3b DG |
1539 | return event; |
1540 | } | |
1541 | ||
993578ff JR |
1542 | /* |
1543 | * Look-up an event notifier rule based on its token id. | |
1544 | * | |
1545 | * Must be called with the RCU read lock held. | |
1546 | * Return an ust_app_event_notifier_rule object or NULL on error. | |
1547 | */ | |
1548 | static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule( | |
1549 | struct lttng_ht *ht, uint64_t token) | |
1550 | { | |
1551 | struct lttng_ht_iter iter; | |
1552 | struct lttng_ht_node_u64 *node; | |
1553 | struct ust_app_event_notifier_rule *event_notifier_rule = NULL; | |
1554 | ||
a0377dfe | 1555 | LTTNG_ASSERT(ht); |
48b7cdc2 | 1556 | ASSERT_RCU_READ_LOCKED(); |
993578ff JR |
1557 | |
1558 | lttng_ht_lookup(ht, &token, &iter); | |
1559 | node = lttng_ht_iter_get_node_u64(&iter); | |
1560 | if (node == NULL) { | |
1561 | DBG2("UST app event notifier rule token not found: token = %" PRIu64, | |
1562 | token); | |
1563 | goto end; | |
1564 | } | |
1565 | ||
0114db0e JG |
1566 | event_notifier_rule = lttng::utils::container_of( |
1567 | node, &ust_app_event_notifier_rule::node); | |
993578ff JR |
1568 | end: |
1569 | return event_notifier_rule; | |
1570 | } | |
1571 | ||
55cc08a6 DG |
1572 | /* |
1573 | * Create the channel context on the tracer. | |
d0b96690 DG |
1574 | * |
1575 | * Called with UST app session lock held. | |
55cc08a6 DG |
1576 | */ |
1577 | static | |
1578 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
1579 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
1580 | { | |
1581 | int ret; | |
1582 | ||
840cb59c | 1583 | health_code_update(); |
86acf0da | 1584 | |
fb45065e | 1585 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1586 | ret = lttng_ust_ctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 | 1587 | ua_chan->obj, &ua_ctx->obj); |
fb45065e | 1588 | pthread_mutex_unlock(&app->sock_lock); |
55cc08a6 | 1589 | if (ret < 0) { |
be355079 JR |
1590 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1591 | ret = 0; | |
1592 | DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d", | |
1593 | app->pid, app->sock); | |
1594 | } else if (ret == -EAGAIN) { | |
3757b385 | 1595 | ret = 0; |
be355079 JR |
1596 | WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d", |
1597 | app->pid, app->sock); | |
1598 | } else { | |
1599 | ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d", | |
1600 | ret, app->pid, app->sock); | |
ffe60014 | 1601 | } |
55cc08a6 DG |
1602 | goto error; |
1603 | } | |
1604 | ||
1605 | ua_ctx->handle = ua_ctx->obj->handle; | |
1606 | ||
d0b96690 DG |
1607 | DBG2("UST app context handle %d created successfully for channel %s", |
1608 | ua_ctx->handle, ua_chan->name); | |
55cc08a6 DG |
1609 | |
1610 | error: | |
840cb59c | 1611 | health_code_update(); |
55cc08a6 DG |
1612 | return ret; |
1613 | } | |
1614 | ||
53a80697 MD |
1615 | /* |
1616 | * Set the filter on the tracer. | |
1617 | */ | |
a154c7b8 | 1618 | static int set_ust_object_filter(struct ust_app *app, |
2b00d462 | 1619 | const struct lttng_bytecode *bytecode, |
fc4b93fa | 1620 | struct lttng_ust_abi_object_data *ust_object) |
53a80697 MD |
1621 | { |
1622 | int ret; | |
fc4b93fa | 1623 | struct lttng_ust_abi_filter_bytecode *ust_bytecode = NULL; |
53a80697 | 1624 | |
840cb59c | 1625 | health_code_update(); |
86acf0da | 1626 | |
f2eafd2d | 1627 | ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode); |
51755dc8 JG |
1628 | if (!ust_bytecode) { |
1629 | ret = -LTTNG_ERR_NOMEM; | |
1630 | goto error; | |
1631 | } | |
fb45065e | 1632 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1633 | ret = lttng_ust_ctl_set_filter(app->sock, ust_bytecode, |
a154c7b8 | 1634 | ust_object); |
fb45065e | 1635 | pthread_mutex_unlock(&app->sock_lock); |
53a80697 | 1636 | if (ret < 0) { |
be355079 | 1637 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3757b385 | 1638 | ret = 0; |
be355079 JR |
1639 | DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d", |
1640 | app->pid, app->sock); | |
1641 | } else if (ret == -EAGAIN) { | |
1642 | ret = 0; | |
1643 | WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d", | |
1644 | app->pid, app->sock); | |
1645 | } else { | |
1646 | ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p", | |
1647 | ret, app->pid, app->sock, ust_object); | |
ffe60014 | 1648 | } |
53a80697 MD |
1649 | goto error; |
1650 | } | |
1651 | ||
f2eafd2d JR |
1652 | DBG2("UST filter successfully set: object = %p", ust_object); |
1653 | ||
1654 | error: | |
1655 | health_code_update(); | |
1656 | free(ust_bytecode); | |
1657 | return ret; | |
1658 | } | |
1659 | ||
1660 | /* | |
1661 | * Set a capture bytecode for the passed object. | |
11f6ce94 JR |
1662 | * The sequence number enforces the ordering at runtime and on reception of |
1663 | * the captured payloads. | |
f2eafd2d JR |
1664 | */ |
1665 | static int set_ust_capture(struct ust_app *app, | |
1666 | const struct lttng_bytecode *bytecode, | |
11f6ce94 | 1667 | unsigned int capture_seqnum, |
3d1384a4 | 1668 | struct lttng_ust_abi_object_data *ust_object) |
f2eafd2d JR |
1669 | { |
1670 | int ret; | |
fc4b93fa | 1671 | struct lttng_ust_abi_capture_bytecode *ust_bytecode = NULL; |
f2eafd2d JR |
1672 | |
1673 | health_code_update(); | |
1674 | ||
1675 | ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode); | |
1676 | if (!ust_bytecode) { | |
1677 | ret = -LTTNG_ERR_NOMEM; | |
1678 | goto error; | |
1679 | } | |
1680 | ||
11f6ce94 JR |
1681 | /* |
1682 | * Set the sequence number to ensure the capture of fields is ordered. | |
1683 | */ | |
1684 | ust_bytecode->seqnum = capture_seqnum; | |
1685 | ||
f2eafd2d | 1686 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1687 | ret = lttng_ust_ctl_set_capture(app->sock, ust_bytecode, |
f2eafd2d JR |
1688 | ust_object); |
1689 | pthread_mutex_unlock(&app->sock_lock); | |
1690 | if (ret < 0) { | |
be355079 JR |
1691 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1692 | ret = 0; | |
1693 | DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d", | |
1694 | app->pid, app->sock); | |
1695 | } else if (ret == -EAGAIN) { | |
f2eafd2d | 1696 | ret = 0; |
be355079 JR |
1697 | DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d", |
1698 | app->pid, app->sock); | |
1699 | } else { | |
1700 | ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d", | |
1701 | ret, app->pid, | |
1702 | app->sock); | |
f2eafd2d JR |
1703 | } |
1704 | ||
1705 | goto error; | |
1706 | } | |
1707 | ||
1708 | DBG2("UST capture successfully set: object = %p", ust_object); | |
53a80697 MD |
1709 | |
1710 | error: | |
840cb59c | 1711 | health_code_update(); |
51755dc8 | 1712 | free(ust_bytecode); |
53a80697 MD |
1713 | return ret; |
1714 | } | |
1715 | ||
51755dc8 | 1716 | static |
fc4b93fa | 1717 | struct lttng_ust_abi_event_exclusion *create_ust_exclusion_from_exclusion( |
c0901ffa | 1718 | const struct lttng_event_exclusion *exclusion) |
51755dc8 | 1719 | { |
fc4b93fa MD |
1720 | struct lttng_ust_abi_event_exclusion *ust_exclusion = NULL; |
1721 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_abi_event_exclusion) + | |
1722 | LTTNG_UST_ABI_SYM_NAME_LEN * exclusion->count; | |
51755dc8 | 1723 | |
64803277 | 1724 | ust_exclusion = zmalloc<lttng_ust_abi_event_exclusion>(exclusion_alloc_size); |
51755dc8 JG |
1725 | if (!ust_exclusion) { |
1726 | PERROR("malloc"); | |
1727 | goto end; | |
1728 | } | |
1729 | ||
a0377dfe | 1730 | LTTNG_ASSERT(sizeof(struct lttng_event_exclusion) == |
fc4b93fa | 1731 | sizeof(struct lttng_ust_abi_event_exclusion)); |
51755dc8 JG |
1732 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); |
1733 | end: | |
1734 | return ust_exclusion; | |
1735 | } | |
1736 | ||
7cc9a73c JI |
1737 | /* |
1738 | * Set event exclusions on the tracer. | |
1739 | */ | |
c0901ffa JR |
1740 | static int set_ust_object_exclusions(struct ust_app *app, |
1741 | const struct lttng_event_exclusion *exclusions, | |
fc4b93fa | 1742 | struct lttng_ust_abi_object_data *ust_object) |
7cc9a73c JI |
1743 | { |
1744 | int ret; | |
fc4b93fa | 1745 | struct lttng_ust_abi_event_exclusion *ust_exclusions = NULL; |
7cc9a73c | 1746 | |
a0377dfe | 1747 | LTTNG_ASSERT(exclusions && exclusions->count > 0); |
7cc9a73c | 1748 | |
c0901ffa | 1749 | health_code_update(); |
7cc9a73c | 1750 | |
c0901ffa JR |
1751 | ust_exclusions = create_ust_exclusion_from_exclusion( |
1752 | exclusions); | |
1753 | if (!ust_exclusions) { | |
51755dc8 JG |
1754 | ret = -LTTNG_ERR_NOMEM; |
1755 | goto error; | |
1756 | } | |
fb45065e | 1757 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1758 | ret = lttng_ust_ctl_set_exclusion(app->sock, ust_exclusions, ust_object); |
fb45065e | 1759 | pthread_mutex_unlock(&app->sock_lock); |
7cc9a73c | 1760 | if (ret < 0) { |
be355079 JR |
1761 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1762 | ret = 0; | |
1763 | DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d", | |
1764 | app->pid, app->sock); | |
1765 | } else if (ret == -EAGAIN) { | |
7cc9a73c | 1766 | ret = 0; |
be355079 JR |
1767 | WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d", |
1768 | app->pid, app->sock); | |
1769 | } else { | |
1770 | ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p", | |
1771 | ret, app->pid, app->sock, ust_object); | |
7cc9a73c JI |
1772 | } |
1773 | goto error; | |
1774 | } | |
1775 | ||
c0901ffa | 1776 | DBG2("UST exclusions set successfully for object %p", ust_object); |
7cc9a73c JI |
1777 | |
1778 | error: | |
1779 | health_code_update(); | |
c0901ffa | 1780 | free(ust_exclusions); |
7cc9a73c JI |
1781 | return ret; |
1782 | } | |
1783 | ||
9730260e DG |
1784 | /* |
1785 | * Disable the specified event on to UST tracer for the UST session. | |
1786 | */ | |
e2456d0a | 1787 | static int disable_ust_object(struct ust_app *app, |
fc4b93fa | 1788 | struct lttng_ust_abi_object_data *object) |
9730260e DG |
1789 | { |
1790 | int ret; | |
1791 | ||
840cb59c | 1792 | health_code_update(); |
86acf0da | 1793 | |
fb45065e | 1794 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1795 | ret = lttng_ust_ctl_disable(app->sock, object); |
fb45065e | 1796 | pthread_mutex_unlock(&app->sock_lock); |
9730260e | 1797 | if (ret < 0) { |
be355079 | 1798 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3757b385 | 1799 | ret = 0; |
be355079 JR |
1800 | DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d", |
1801 | app->pid, app->sock); | |
1802 | } else if (ret == -EAGAIN) { | |
1803 | ret = 0; | |
1804 | WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d", | |
1805 | app->pid, app->sock); | |
1806 | } else { | |
1807 | ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p", | |
1808 | ret, app->pid, app->sock, object); | |
ffe60014 | 1809 | } |
9730260e DG |
1810 | goto error; |
1811 | } | |
1812 | ||
be355079 | 1813 | DBG2("UST app object %p disabled successfully for app: pid = %d", |
e2456d0a | 1814 | object, app->pid); |
9730260e DG |
1815 | |
1816 | error: | |
840cb59c | 1817 | health_code_update(); |
9730260e DG |
1818 | return ret; |
1819 | } | |
1820 | ||
78f0bacd DG |
1821 | /* |
1822 | * Disable the specified channel on to UST tracer for the UST session. | |
1823 | */ | |
1824 | static int disable_ust_channel(struct ust_app *app, | |
1825 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1826 | { | |
1827 | int ret; | |
1828 | ||
840cb59c | 1829 | health_code_update(); |
86acf0da | 1830 | |
fb45065e | 1831 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1832 | ret = lttng_ust_ctl_disable(app->sock, ua_chan->obj); |
fb45065e | 1833 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1834 | if (ret < 0) { |
be355079 JR |
1835 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1836 | ret = 0; | |
1837 | DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d", | |
1838 | app->pid, app->sock); | |
1839 | } else if (ret == -EAGAIN) { | |
3757b385 | 1840 | ret = 0; |
be355079 JR |
1841 | WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d", |
1842 | app->pid, app->sock); | |
1843 | } else { | |
1844 | ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d", | |
1845 | ua_chan->name, ua_sess->handle, ret, | |
1846 | app->pid, app->sock); | |
ffe60014 | 1847 | } |
78f0bacd DG |
1848 | goto error; |
1849 | } | |
1850 | ||
be355079 | 1851 | DBG2("UST app channel %s disabled successfully for app: pid = %d", |
852d0037 | 1852 | ua_chan->name, app->pid); |
78f0bacd DG |
1853 | |
1854 | error: | |
840cb59c | 1855 | health_code_update(); |
78f0bacd DG |
1856 | return ret; |
1857 | } | |
1858 | ||
1859 | /* | |
1860 | * Enable the specified channel on to UST tracer for the UST session. | |
1861 | */ | |
1862 | static int enable_ust_channel(struct ust_app *app, | |
1863 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1864 | { | |
1865 | int ret; | |
1866 | ||
840cb59c | 1867 | health_code_update(); |
86acf0da | 1868 | |
fb45065e | 1869 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1870 | ret = lttng_ust_ctl_enable(app->sock, ua_chan->obj); |
fb45065e | 1871 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1872 | if (ret < 0) { |
be355079 JR |
1873 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1874 | ret = 0; | |
1875 | DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d", | |
1876 | ua_chan->name, app->pid, app->sock); | |
1877 | } else if (ret == -EAGAIN) { | |
3757b385 | 1878 | ret = 0; |
be355079 JR |
1879 | WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d", |
1880 | ua_chan->name, app->pid, app->sock); | |
1881 | } else { | |
1882 | ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d", | |
1883 | ua_chan->name, ua_sess->handle, ret, | |
1884 | app->pid, app->sock); | |
ffe60014 | 1885 | } |
78f0bacd DG |
1886 | goto error; |
1887 | } | |
1888 | ||
1889 | ua_chan->enabled = 1; | |
1890 | ||
be355079 | 1891 | DBG2("UST app channel %s enabled successfully for app: pid = %d", |
852d0037 | 1892 | ua_chan->name, app->pid); |
78f0bacd DG |
1893 | |
1894 | error: | |
840cb59c | 1895 | health_code_update(); |
78f0bacd DG |
1896 | return ret; |
1897 | } | |
1898 | ||
edb67388 DG |
1899 | /* |
1900 | * Enable the specified event on to UST tracer for the UST session. | |
1901 | */ | |
3428a1b7 | 1902 | static int enable_ust_object( |
fc4b93fa | 1903 | struct ust_app *app, struct lttng_ust_abi_object_data *ust_object) |
edb67388 DG |
1904 | { |
1905 | int ret; | |
1906 | ||
840cb59c | 1907 | health_code_update(); |
86acf0da | 1908 | |
fb45065e | 1909 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 1910 | ret = lttng_ust_ctl_enable(app->sock, ust_object); |
fb45065e | 1911 | pthread_mutex_unlock(&app->sock_lock); |
edb67388 | 1912 | if (ret < 0) { |
be355079 | 1913 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3757b385 | 1914 | ret = 0; |
be355079 JR |
1915 | DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d", |
1916 | app->pid, app->sock); | |
1917 | } else if (ret == -EAGAIN) { | |
1918 | ret = 0; | |
1919 | WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d", | |
1920 | app->pid, app->sock); | |
1921 | } else { | |
1922 | ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p", | |
1923 | ret, app->pid, app->sock, ust_object); | |
ffe60014 | 1924 | } |
edb67388 DG |
1925 | goto error; |
1926 | } | |
1927 | ||
be355079 | 1928 | DBG2("UST app object %p enabled successfully for app: pid = %d", |
3428a1b7 | 1929 | ust_object, app->pid); |
edb67388 DG |
1930 | |
1931 | error: | |
840cb59c | 1932 | health_code_update(); |
edb67388 DG |
1933 | return ret; |
1934 | } | |
1935 | ||
099e26bd | 1936 | /* |
7972aab2 | 1937 | * Send channel and stream buffer to application. |
4f3ab6ee | 1938 | * |
ffe60014 | 1939 | * Return 0 on success. On error, a negative value is returned. |
4f3ab6ee | 1940 | */ |
7972aab2 DG |
1941 | static int send_channel_pid_to_ust(struct ust_app *app, |
1942 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
4f3ab6ee DG |
1943 | { |
1944 | int ret; | |
ffe60014 | 1945 | struct ust_app_stream *stream, *stmp; |
4f3ab6ee | 1946 | |
a0377dfe FD |
1947 | LTTNG_ASSERT(app); |
1948 | LTTNG_ASSERT(ua_sess); | |
1949 | LTTNG_ASSERT(ua_chan); | |
4f3ab6ee | 1950 | |
840cb59c | 1951 | health_code_update(); |
4f3ab6ee | 1952 | |
7972aab2 DG |
1953 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
1954 | app->sock); | |
86acf0da | 1955 | |
ffe60014 DG |
1956 | /* Send channel to the application. */ |
1957 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
1958 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1959 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1960 | goto error; | |
be355079 JR |
1961 | } else if (ret == -EAGAIN) { |
1962 | /* Caused by timeout. */ | |
1963 | WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".", | |
1964 | app->pid, ua_chan->name, ua_sess->tracing_id); | |
1965 | /* Treat this the same way as an application that is exiting. */ | |
1966 | ret = -ENOTCONN; | |
1967 | goto error; | |
a7169585 | 1968 | } else if (ret < 0) { |
b551a063 DG |
1969 | goto error; |
1970 | } | |
1971 | ||
d88aee68 DG |
1972 | health_code_update(); |
1973 | ||
ffe60014 DG |
1974 | /* Send all streams to application. */ |
1975 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
1976 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); | |
a7169585 | 1977 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
be355079 | 1978 | ret = -ENOTCONN; /* Caused by app exiting. */ |
a7169585 | 1979 | goto error; |
be355079 JR |
1980 | } else if (ret == -EAGAIN) { |
1981 | /* Caused by timeout. */ | |
1982 | WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64 "\".", | |
1983 | app->pid, stream->name, ua_chan->name, | |
1984 | ua_sess->tracing_id); | |
acfb63a8 JR |
1985 | /* |
1986 | * Treat this the same way as an application that is | |
1987 | * exiting. | |
1988 | */ | |
be355079 | 1989 | ret = -ENOTCONN; |
a7169585 | 1990 | } else if (ret < 0) { |
ffe60014 DG |
1991 | goto error; |
1992 | } | |
1993 | /* We don't need the stream anymore once sent to the tracer. */ | |
1994 | cds_list_del(&stream->list); | |
fb45065e | 1995 | delete_ust_app_stream(-1, stream, app); |
ffe60014 | 1996 | } |
ffe60014 | 1997 | |
b551a063 | 1998 | error: |
840cb59c | 1999 | health_code_update(); |
b551a063 DG |
2000 | return ret; |
2001 | } | |
2002 | ||
91d76f53 | 2003 | /* |
5b4a0ec0 | 2004 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
2005 | * |
2006 | * Should be called with session mutex held. | |
91d76f53 | 2007 | */ |
edb67388 | 2008 | static |
f46376a1 | 2009 | int create_ust_event(struct ust_app *app, |
edb67388 | 2010 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
91d76f53 | 2011 | { |
5b4a0ec0 | 2012 | int ret = 0; |
284d8f55 | 2013 | |
840cb59c | 2014 | health_code_update(); |
86acf0da | 2015 | |
5b4a0ec0 | 2016 | /* Create UST event on tracer */ |
fb45065e | 2017 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 2018 | ret = lttng_ust_ctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 | 2019 | &ua_event->obj); |
fb45065e | 2020 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 | 2021 | if (ret < 0) { |
be355079 JR |
2022 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2023 | ret = 0; | |
2024 | DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d", | |
2025 | app->pid, app->sock); | |
2026 | } else if (ret == -EAGAIN) { | |
3757b385 | 2027 | ret = 0; |
be355079 JR |
2028 | WARN("UST app create event failed. Communication time out: pid = %d, sock = %d", |
2029 | app->pid, app->sock); | |
2030 | } else { | |
2031 | ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d", | |
2032 | ua_event->attr.name, ret, app->pid, | |
2033 | app->sock); | |
ffe60014 | 2034 | } |
5b4a0ec0 | 2035 | goto error; |
91d76f53 | 2036 | } |
f6a9efaa | 2037 | |
5b4a0ec0 | 2038 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 2039 | |
be355079 | 2040 | DBG2("UST app event %s created successfully for pid:%d object = %p", |
3428a1b7 | 2041 | ua_event->attr.name, app->pid, ua_event->obj); |
f6a9efaa | 2042 | |
840cb59c | 2043 | health_code_update(); |
86acf0da | 2044 | |
025faf73 DG |
2045 | /* Set filter if one is present. */ |
2046 | if (ua_event->filter) { | |
a154c7b8 | 2047 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
025faf73 DG |
2048 | if (ret < 0) { |
2049 | goto error; | |
2050 | } | |
2051 | } | |
2052 | ||
7cc9a73c JI |
2053 | /* Set exclusions for the event */ |
2054 | if (ua_event->exclusion) { | |
c0901ffa | 2055 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
7cc9a73c JI |
2056 | if (ret < 0) { |
2057 | goto error; | |
2058 | } | |
2059 | } | |
2060 | ||
8535a6d9 | 2061 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
2062 | if (ua_event->enabled) { |
2063 | /* | |
2064 | * We now need to explicitly enable the event, since it | |
2065 | * is now disabled at creation. | |
2066 | */ | |
3428a1b7 | 2067 | ret = enable_ust_object(app, ua_event->obj); |
40113787 MD |
2068 | if (ret < 0) { |
2069 | /* | |
2070 | * If we hit an EPERM, something is wrong with our enable call. If | |
2071 | * we get an EEXIST, there is a problem on the tracer side since we | |
2072 | * just created it. | |
2073 | */ | |
2074 | switch (ret) { | |
2075 | case -LTTNG_UST_ERR_PERM: | |
2076 | /* Code flow problem */ | |
a0377dfe | 2077 | abort(); |
40113787 MD |
2078 | case -LTTNG_UST_ERR_EXIST: |
2079 | /* It's OK for our use case. */ | |
2080 | ret = 0; | |
2081 | break; | |
2082 | default: | |
2083 | break; | |
2084 | } | |
2085 | goto error; | |
2086 | } | |
8535a6d9 DG |
2087 | } |
2088 | ||
5b4a0ec0 | 2089 | error: |
840cb59c | 2090 | health_code_update(); |
5b4a0ec0 | 2091 | return ret; |
91d76f53 | 2092 | } |
48842b30 | 2093 | |
993578ff JR |
2094 | static int init_ust_event_notifier_from_event_rule( |
2095 | const struct lttng_event_rule *rule, | |
fc4b93fa | 2096 | struct lttng_ust_abi_event_notifier *event_notifier) |
993578ff JR |
2097 | { |
2098 | enum lttng_event_rule_status status; | |
fc4b93fa | 2099 | enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
993578ff JR |
2100 | int loglevel = -1, ret = 0; |
2101 | const char *pattern; | |
2102 | ||
993578ff JR |
2103 | |
2104 | memset(event_notifier, 0, sizeof(*event_notifier)); | |
2105 | ||
44760c20 JR |
2106 | if (lttng_event_rule_targets_agent_domain(rule)) { |
2107 | /* | |
2108 | * Special event for agents | |
2109 | * The actual meat of the event is in the filter that will be | |
2110 | * attached later on. | |
2111 | * Set the default values for the agent event. | |
2112 | */ | |
2113 | pattern = event_get_default_agent_ust_name( | |
2114 | lttng_event_rule_get_domain_type(rule)); | |
2115 | loglevel = 0; | |
fc4b93fa | 2116 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
44760c20 | 2117 | } else { |
85b05318 | 2118 | const struct lttng_log_level_rule *log_level_rule; |
993578ff | 2119 | |
a0377dfe | 2120 | LTTNG_ASSERT(lttng_event_rule_get_type(rule) == |
695f7044 JR |
2121 | LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT); |
2122 | ||
2123 | status = lttng_event_rule_user_tracepoint_get_name_pattern(rule, &pattern); | |
44760c20 JR |
2124 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
2125 | /* At this point, this is a fatal error. */ | |
2126 | abort(); | |
2127 | } | |
993578ff | 2128 | |
695f7044 | 2129 | status = lttng_event_rule_user_tracepoint_get_log_level_rule( |
85b05318 JR |
2130 | rule, &log_level_rule); |
2131 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { | |
fc4b93fa | 2132 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
85b05318 JR |
2133 | } else if (status == LTTNG_EVENT_RULE_STATUS_OK) { |
2134 | enum lttng_log_level_rule_status llr_status; | |
2135 | ||
2136 | switch (lttng_log_level_rule_get_type(log_level_rule)) { | |
2137 | case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY: | |
2138 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE; | |
2139 | llr_status = lttng_log_level_rule_exactly_get_level( | |
2140 | log_level_rule, &loglevel); | |
2141 | break; | |
2142 | case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS: | |
2143 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE; | |
2144 | llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level( | |
2145 | log_level_rule, &loglevel); | |
2146 | break; | |
2147 | default: | |
2148 | abort(); | |
2149 | } | |
993578ff | 2150 | |
a0377dfe | 2151 | LTTNG_ASSERT(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK); |
85b05318 JR |
2152 | } else { |
2153 | /* At this point this is a fatal error. */ | |
2154 | abort(); | |
44760c20 | 2155 | } |
993578ff JR |
2156 | } |
2157 | ||
fc4b93fa | 2158 | event_notifier->event.instrumentation = LTTNG_UST_ABI_TRACEPOINT; |
993578ff | 2159 | ret = lttng_strncpy(event_notifier->event.name, pattern, |
4ab5469c | 2160 | sizeof(event_notifier->event.name)); |
993578ff JR |
2161 | if (ret) { |
2162 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", | |
2163 | pattern); | |
2164 | goto end; | |
2165 | } | |
2166 | ||
2167 | event_notifier->event.loglevel_type = ust_loglevel_type; | |
2168 | event_notifier->event.loglevel = loglevel; | |
2169 | end: | |
2170 | return ret; | |
2171 | } | |
2172 | ||
2173 | /* | |
2174 | * Create the specified event notifier against the user space tracer of a | |
2175 | * given application. | |
2176 | */ | |
2177 | static int create_ust_event_notifier(struct ust_app *app, | |
2178 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) | |
2179 | { | |
2180 | int ret = 0; | |
267d66aa JR |
2181 | enum lttng_condition_status condition_status; |
2182 | const struct lttng_condition *condition = NULL; | |
fc4b93fa | 2183 | struct lttng_ust_abi_event_notifier event_notifier; |
267d66aa | 2184 | const struct lttng_event_rule *event_rule = NULL; |
f83be61d JR |
2185 | unsigned int capture_bytecode_count = 0, i; |
2186 | enum lttng_condition_status cond_status; | |
695f7044 | 2187 | enum lttng_event_rule_type event_rule_type; |
993578ff JR |
2188 | |
2189 | health_code_update(); | |
a0377dfe | 2190 | LTTNG_ASSERT(app->event_notifier_group.object); |
993578ff | 2191 | |
267d66aa JR |
2192 | condition = lttng_trigger_get_const_condition( |
2193 | ua_event_notifier_rule->trigger); | |
a0377dfe FD |
2194 | LTTNG_ASSERT(condition); |
2195 | LTTNG_ASSERT(lttng_condition_get_type(condition) == | |
8dbb86b8 | 2196 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
993578ff | 2197 | |
8dbb86b8 | 2198 | condition_status = lttng_condition_event_rule_matches_get_rule( |
d602bd6a | 2199 | condition, &event_rule); |
a0377dfe | 2200 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
d602bd6a | 2201 | |
a0377dfe | 2202 | LTTNG_ASSERT(event_rule); |
695f7044 JR |
2203 | |
2204 | event_rule_type = lttng_event_rule_get_type(event_rule); | |
a0377dfe | 2205 | LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT || |
695f7044 JR |
2206 | event_rule_type == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING || |
2207 | event_rule_type == | |
2208 | LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING || | |
2209 | event_rule_type == | |
2210 | LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING); | |
267d66aa JR |
2211 | |
2212 | init_ust_event_notifier_from_event_rule(event_rule, &event_notifier); | |
993578ff | 2213 | event_notifier.event.token = ua_event_notifier_rule->token; |
533a90fb | 2214 | event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index; |
993578ff JR |
2215 | |
2216 | /* Create UST event notifier against the tracer. */ | |
2217 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 2218 | ret = lttng_ust_ctl_create_event_notifier(app->sock, &event_notifier, |
993578ff JR |
2219 | app->event_notifier_group.object, |
2220 | &ua_event_notifier_rule->obj); | |
2221 | pthread_mutex_unlock(&app->sock_lock); | |
2222 | if (ret < 0) { | |
be355079 | 2223 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
993578ff | 2224 | ret = 0; |
be355079 JR |
2225 | DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d", |
2226 | app->pid, app->sock); | |
2227 | } else if (ret == -EAGAIN) { | |
2228 | ret = 0; | |
2229 | WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d", | |
2230 | app->pid, app->sock); | |
2231 | } else { | |
2232 | ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d", | |
2233 | event_notifier.event.name, ret, app->pid, | |
2234 | app->sock); | |
993578ff | 2235 | } |
993578ff JR |
2236 | goto error; |
2237 | } | |
2238 | ||
2239 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; | |
2240 | ||
9324443a | 2241 | DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p", |
be355079 | 2242 | event_notifier.event.name, app->name, app->pid, |
993578ff JR |
2243 | ua_event_notifier_rule->obj); |
2244 | ||
2245 | health_code_update(); | |
2246 | ||
2247 | /* Set filter if one is present. */ | |
2248 | if (ua_event_notifier_rule->filter) { | |
2249 | ret = set_ust_object_filter(app, ua_event_notifier_rule->filter, | |
2250 | ua_event_notifier_rule->obj); | |
2251 | if (ret < 0) { | |
2252 | goto error; | |
2253 | } | |
2254 | } | |
2255 | ||
2256 | /* Set exclusions for the event. */ | |
2257 | if (ua_event_notifier_rule->exclusion) { | |
2258 | ret = set_ust_object_exclusions(app, | |
2259 | ua_event_notifier_rule->exclusion, | |
2260 | ua_event_notifier_rule->obj); | |
2261 | if (ret < 0) { | |
2262 | goto error; | |
2263 | } | |
2264 | } | |
f83be61d JR |
2265 | |
2266 | /* Set the capture bytecodes. */ | |
8dbb86b8 | 2267 | cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count( |
f83be61d | 2268 | condition, &capture_bytecode_count); |
a0377dfe | 2269 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
f83be61d JR |
2270 | |
2271 | for (i = 0; i < capture_bytecode_count; i++) { | |
2272 | const struct lttng_bytecode *capture_bytecode = | |
8dbb86b8 | 2273 | lttng_condition_event_rule_matches_get_capture_bytecode_at_index( |
f83be61d JR |
2274 | condition, i); |
2275 | ||
11f6ce94 | 2276 | ret = set_ust_capture(app, capture_bytecode, i, |
f83be61d JR |
2277 | ua_event_notifier_rule->obj); |
2278 | if (ret < 0) { | |
2279 | goto error; | |
2280 | } | |
2281 | } | |
993578ff JR |
2282 | |
2283 | /* | |
2284 | * We now need to explicitly enable the event, since it | |
2285 | * is disabled at creation. | |
2286 | */ | |
2287 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); | |
2288 | if (ret < 0) { | |
2289 | /* | |
2290 | * If we hit an EPERM, something is wrong with our enable call. | |
2291 | * If we get an EEXIST, there is a problem on the tracer side | |
2292 | * since we just created it. | |
2293 | */ | |
2294 | switch (ret) { | |
2295 | case -LTTNG_UST_ERR_PERM: | |
2296 | /* Code flow problem. */ | |
2297 | abort(); | |
2298 | case -LTTNG_UST_ERR_EXIST: | |
2299 | /* It's OK for our use case. */ | |
2300 | ret = 0; | |
2301 | break; | |
2302 | default: | |
2303 | break; | |
2304 | } | |
2305 | ||
2306 | goto error; | |
2307 | } | |
2308 | ||
2309 | ua_event_notifier_rule->enabled = true; | |
2310 | ||
2311 | error: | |
2312 | health_code_update(); | |
2313 | return ret; | |
2314 | } | |
2315 | ||
5b4a0ec0 DG |
2316 | /* |
2317 | * Copy data between an UST app event and a LTT event. | |
2318 | */ | |
421cb601 | 2319 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
2320 | struct ltt_ust_event *uevent) |
2321 | { | |
b4ffad32 JI |
2322 | size_t exclusion_alloc_size; |
2323 | ||
48842b30 DG |
2324 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
2325 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
2326 | ||
fc34caaa DG |
2327 | ua_event->enabled = uevent->enabled; |
2328 | ||
5b4a0ec0 DG |
2329 | /* Copy event attributes */ |
2330 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
2331 | ||
53a80697 MD |
2332 | /* Copy filter bytecode */ |
2333 | if (uevent->filter) { | |
2b00d462 | 2334 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
025faf73 | 2335 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 2336 | } |
b4ffad32 JI |
2337 | |
2338 | /* Copy exclusion data */ | |
2339 | if (uevent->exclusion) { | |
51755dc8 | 2340 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
fc4b93fa | 2341 | LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count; |
64803277 | 2342 | ua_event->exclusion = zmalloc<lttng_event_exclusion>(exclusion_alloc_size); |
5f8df26c JI |
2343 | if (ua_event->exclusion == NULL) { |
2344 | PERROR("malloc"); | |
2345 | } else { | |
2346 | memcpy(ua_event->exclusion, uevent->exclusion, | |
2347 | exclusion_alloc_size); | |
b4ffad32 JI |
2348 | } |
2349 | } | |
48842b30 DG |
2350 | } |
2351 | ||
5b4a0ec0 DG |
2352 | /* |
2353 | * Copy data between an UST app channel and a LTT channel. | |
2354 | */ | |
421cb601 | 2355 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
2356 | struct ltt_ust_channel *uchan) |
2357 | { | |
fc34caaa | 2358 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
2359 | |
2360 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
2361 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 2362 | |
1624d5b7 JD |
2363 | ua_chan->tracefile_size = uchan->tracefile_size; |
2364 | ua_chan->tracefile_count = uchan->tracefile_count; | |
2365 | ||
ffe60014 DG |
2366 | /* Copy event attributes since the layout is different. */ |
2367 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
2368 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
2369 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
2370 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
2371 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
e9404c27 | 2372 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
7966af57 | 2373 | ua_chan->attr.output = (lttng_ust_abi_output) uchan->attr.output; |
491d1539 MD |
2374 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
2375 | ||
ffe60014 DG |
2376 | /* |
2377 | * Note that the attribute channel type is not set since the channel on the | |
2378 | * tracing registry side does not have this information. | |
2379 | */ | |
48842b30 | 2380 | |
fc34caaa | 2381 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 2382 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 2383 | |
fc34caaa | 2384 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
2385 | } |
2386 | ||
5b4a0ec0 DG |
2387 | /* |
2388 | * Copy data between a UST app session and a regular LTT session. | |
2389 | */ | |
421cb601 | 2390 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 2391 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2392 | { |
477d7741 MD |
2393 | struct tm *timeinfo; |
2394 | char datetime[16]; | |
2395 | int ret; | |
d7ba1388 | 2396 | char tmp_shm_path[PATH_MAX]; |
477d7741 | 2397 | |
940c4592 | 2398 | timeinfo = localtime(&app->registration_time); |
477d7741 | 2399 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
48842b30 | 2400 | |
421cb601 | 2401 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 2402 | |
7972aab2 DG |
2403 | ua_sess->tracing_id = usess->id; |
2404 | ua_sess->id = get_next_session_id(); | |
ff588497 JR |
2405 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
2406 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); | |
2407 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); | |
2408 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); | |
7972aab2 | 2409 | ua_sess->buffer_type = usess->buffer_type; |
d7bfb9b0 | 2410 | ua_sess->bits_per_long = app->abi.bits_per_long; |
6addfa37 | 2411 | |
7972aab2 | 2412 | /* There is only one consumer object per session possible. */ |
6addfa37 | 2413 | consumer_output_get(usess->consumer); |
7972aab2 | 2414 | ua_sess->consumer = usess->consumer; |
6addfa37 | 2415 | |
2bba9e53 | 2416 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 2417 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
2418 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
2419 | &usess->metadata_attr); | |
7972aab2 DG |
2420 | |
2421 | switch (ua_sess->buffer_type) { | |
2422 | case LTTNG_BUFFER_PER_PID: | |
2423 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 2424 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
2425 | datetime); |
2426 | break; | |
2427 | case LTTNG_BUFFER_PER_UID: | |
2428 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
470cc211 | 2429 | DEFAULT_UST_TRACE_UID_PATH, |
ff588497 | 2430 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
d7bfb9b0 | 2431 | app->abi.bits_per_long); |
7972aab2 DG |
2432 | break; |
2433 | default: | |
a0377dfe | 2434 | abort(); |
7972aab2 DG |
2435 | goto error; |
2436 | } | |
477d7741 MD |
2437 | if (ret < 0) { |
2438 | PERROR("asprintf UST shadow copy session"); | |
a0377dfe | 2439 | abort(); |
7972aab2 | 2440 | goto error; |
477d7741 MD |
2441 | } |
2442 | ||
3d071855 MD |
2443 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
2444 | sizeof(ua_sess->root_shm_path)); | |
2445 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2446 | strncpy(ua_sess->shm_path, usess->shm_path, |
2447 | sizeof(ua_sess->shm_path)); | |
2448 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2449 | if (ua_sess->shm_path[0]) { | |
2450 | switch (ua_sess->buffer_type) { | |
2451 | case LTTNG_BUFFER_PER_PID: | |
2452 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2453 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
d7ba1388 MD |
2454 | app->name, app->pid, datetime); |
2455 | break; | |
2456 | case LTTNG_BUFFER_PER_UID: | |
2457 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2458 | "/" DEFAULT_UST_TRACE_UID_PATH, |
d7bfb9b0 | 2459 | app->uid, app->abi.bits_per_long); |
d7ba1388 MD |
2460 | break; |
2461 | default: | |
a0377dfe | 2462 | abort(); |
d7ba1388 MD |
2463 | goto error; |
2464 | } | |
2465 | if (ret < 0) { | |
2466 | PERROR("sprintf UST shadow copy session"); | |
a0377dfe | 2467 | abort(); |
d7ba1388 MD |
2468 | goto error; |
2469 | } | |
2470 | strncat(ua_sess->shm_path, tmp_shm_path, | |
2471 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
2472 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2473 | } | |
6addfa37 | 2474 | return; |
7972aab2 DG |
2475 | |
2476 | error: | |
6addfa37 | 2477 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
2478 | } |
2479 | ||
78f0bacd DG |
2480 | /* |
2481 | * Lookup sesison wrapper. | |
2482 | */ | |
84cd17c6 | 2483 | static |
fb9a95c4 | 2484 | void __lookup_session_by_app(const struct ltt_ust_session *usess, |
bec39940 | 2485 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
2486 | { |
2487 | /* Get right UST app session from app */ | |
d9bf3ca4 | 2488 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
2489 | } |
2490 | ||
421cb601 DG |
2491 | /* |
2492 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 2493 | * id. |
421cb601 | 2494 | */ |
48842b30 | 2495 | static struct ust_app_session *lookup_session_by_app( |
fb9a95c4 | 2496 | const struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2497 | { |
bec39940 | 2498 | struct lttng_ht_iter iter; |
d9bf3ca4 | 2499 | struct lttng_ht_node_u64 *node; |
48842b30 | 2500 | |
84cd17c6 | 2501 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 2502 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
2503 | if (node == NULL) { |
2504 | goto error; | |
2505 | } | |
2506 | ||
0114db0e | 2507 | return lttng::utils::container_of(node, &ust_app_session::node); |
48842b30 DG |
2508 | |
2509 | error: | |
2510 | return NULL; | |
2511 | } | |
2512 | ||
7972aab2 DG |
2513 | /* |
2514 | * Setup buffer registry per PID for the given session and application. If none | |
2515 | * is found, a new one is created, added to the global registry and | |
2516 | * initialized. If regp is valid, it's set with the newly created object. | |
2517 | * | |
2518 | * Return 0 on success or else a negative value. | |
2519 | */ | |
2520 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
2521 | struct ust_app *app, struct buffer_reg_pid **regp) | |
2522 | { | |
2523 | int ret = 0; | |
2524 | struct buffer_reg_pid *reg_pid; | |
2525 | ||
a0377dfe FD |
2526 | LTTNG_ASSERT(ua_sess); |
2527 | LTTNG_ASSERT(app); | |
7972aab2 DG |
2528 | |
2529 | rcu_read_lock(); | |
2530 | ||
2531 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
2532 | if (!reg_pid) { | |
2533 | /* | |
2534 | * This is the create channel path meaning that if there is NO | |
2535 | * registry available, we have to create one for this session. | |
2536 | */ | |
d7ba1388 | 2537 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 2538 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
2539 | if (ret < 0) { |
2540 | goto error; | |
2541 | } | |
7972aab2 DG |
2542 | } else { |
2543 | goto end; | |
2544 | } | |
2545 | ||
2546 | /* Initialize registry. */ | |
d7bfb9b0 JG |
2547 | reg_pid->registry->reg.ust = ust_registry_session_per_pid_create(app, app->abi, |
2548 | app->version.major, app->version.minor, reg_pid->root_shm_path, | |
2549 | reg_pid->shm_path, | |
ff588497 JR |
2550 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
2551 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
aeeb48c6 JG |
2552 | ua_sess->tracing_id); |
2553 | if (!reg_pid->registry->reg.ust) { | |
286c991a MD |
2554 | /* |
2555 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
2556 | * destroy the buffer registry, because it is always expected | |
2557 | * that if the buffer registry can be found, its ust registry is | |
2558 | * non-NULL. | |
2559 | */ | |
2560 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
2561 | goto error; |
2562 | } | |
2563 | ||
286c991a MD |
2564 | buffer_reg_pid_add(reg_pid); |
2565 | ||
7972aab2 DG |
2566 | DBG3("UST app buffer registry per PID created successfully"); |
2567 | ||
2568 | end: | |
2569 | if (regp) { | |
2570 | *regp = reg_pid; | |
2571 | } | |
2572 | error: | |
2573 | rcu_read_unlock(); | |
2574 | return ret; | |
2575 | } | |
2576 | ||
2577 | /* | |
2578 | * Setup buffer registry per UID for the given session and application. If none | |
2579 | * is found, a new one is created, added to the global registry and | |
2580 | * initialized. If regp is valid, it's set with the newly created object. | |
2581 | * | |
2582 | * Return 0 on success or else a negative value. | |
2583 | */ | |
2584 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 2585 | struct ust_app_session *ua_sess, |
7972aab2 DG |
2586 | struct ust_app *app, struct buffer_reg_uid **regp) |
2587 | { | |
2588 | int ret = 0; | |
2589 | struct buffer_reg_uid *reg_uid; | |
2590 | ||
a0377dfe FD |
2591 | LTTNG_ASSERT(usess); |
2592 | LTTNG_ASSERT(app); | |
7972aab2 DG |
2593 | |
2594 | rcu_read_lock(); | |
2595 | ||
d7bfb9b0 | 2596 | reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid); |
7972aab2 DG |
2597 | if (!reg_uid) { |
2598 | /* | |
2599 | * This is the create channel path meaning that if there is NO | |
2600 | * registry available, we have to create one for this session. | |
2601 | */ | |
d7bfb9b0 JG |
2602 | ret = buffer_reg_uid_create(usess->id, app->abi.bits_per_long, app->uid, |
2603 | LTTNG_DOMAIN_UST, ®_uid, ua_sess->root_shm_path, | |
2604 | ua_sess->shm_path); | |
7972aab2 DG |
2605 | if (ret < 0) { |
2606 | goto error; | |
2607 | } | |
7972aab2 DG |
2608 | } else { |
2609 | goto end; | |
2610 | } | |
2611 | ||
2612 | /* Initialize registry. */ | |
d7bfb9b0 JG |
2613 | reg_uid->registry->reg.ust = ust_registry_session_per_uid_create(app->abi, |
2614 | app->version.major, app->version.minor, reg_uid->root_shm_path, | |
2615 | reg_uid->shm_path, usess->uid, usess->gid, ua_sess->tracing_id, app->uid); | |
aeeb48c6 | 2616 | if (!reg_uid->registry->reg.ust) { |
286c991a MD |
2617 | /* |
2618 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
2619 | * destroy the buffer registry, because it is always expected | |
2620 | * that if the buffer registry can be found, its ust registry is | |
2621 | * non-NULL. | |
2622 | */ | |
2623 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
2624 | goto error; |
2625 | } | |
aeeb48c6 | 2626 | |
7972aab2 DG |
2627 | /* Add node to teardown list of the session. */ |
2628 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
2629 | ||
286c991a | 2630 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 2631 | |
286c991a | 2632 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
2633 | end: |
2634 | if (regp) { | |
2635 | *regp = reg_uid; | |
2636 | } | |
2637 | error: | |
2638 | rcu_read_unlock(); | |
2639 | return ret; | |
2640 | } | |
2641 | ||
421cb601 | 2642 | /* |
3d8ca23b | 2643 | * Create a session on the tracer side for the given app. |
421cb601 | 2644 | * |
3d8ca23b DG |
2645 | * On success, ua_sess_ptr is populated with the session pointer or else left |
2646 | * untouched. If the session was created, is_created is set to 1. On error, | |
2647 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
2648 | * be NULL. | |
2649 | * | |
2650 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
b623cb6a | 2651 | * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails. |
421cb601 | 2652 | */ |
03f91eaa | 2653 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
3d8ca23b DG |
2654 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
2655 | int *is_created) | |
421cb601 | 2656 | { |
3d8ca23b | 2657 | int ret, created = 0; |
421cb601 DG |
2658 | struct ust_app_session *ua_sess; |
2659 | ||
a0377dfe FD |
2660 | LTTNG_ASSERT(usess); |
2661 | LTTNG_ASSERT(app); | |
2662 | LTTNG_ASSERT(ua_sess_ptr); | |
3d8ca23b | 2663 | |
840cb59c | 2664 | health_code_update(); |
86acf0da | 2665 | |
421cb601 DG |
2666 | ua_sess = lookup_session_by_app(usess, app); |
2667 | if (ua_sess == NULL) { | |
d9bf3ca4 | 2668 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 2669 | app->pid, usess->id); |
40bbd087 | 2670 | ua_sess = alloc_ust_app_session(); |
421cb601 DG |
2671 | if (ua_sess == NULL) { |
2672 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
2673 | ret = -ENOMEM; |
2674 | goto error; | |
421cb601 | 2675 | } |
477d7741 | 2676 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 2677 | created = 1; |
421cb601 DG |
2678 | } |
2679 | ||
7972aab2 DG |
2680 | switch (usess->buffer_type) { |
2681 | case LTTNG_BUFFER_PER_PID: | |
2682 | /* Init local registry. */ | |
2683 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 2684 | if (ret < 0) { |
e64207cf | 2685 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2686 | goto error; |
2687 | } | |
2688 | break; | |
2689 | case LTTNG_BUFFER_PER_UID: | |
2690 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 2691 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 2692 | if (ret < 0) { |
e64207cf | 2693 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2694 | goto error; |
2695 | } | |
2696 | break; | |
2697 | default: | |
a0377dfe | 2698 | abort(); |
7972aab2 DG |
2699 | ret = -EINVAL; |
2700 | goto error; | |
2701 | } | |
2702 | ||
2703 | health_code_update(); | |
2704 | ||
2705 | if (ua_sess->handle == -1) { | |
fb45065e | 2706 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 2707 | ret = lttng_ust_ctl_create_session(app->sock); |
fb45065e | 2708 | pthread_mutex_unlock(&app->sock_lock); |
7972aab2 | 2709 | if (ret < 0) { |
be355079 JR |
2710 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2711 | DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d", | |
2712 | app->pid, app->sock); | |
2713 | ret = 0; | |
2714 | } else if (ret == -EAGAIN) { | |
2715 | DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d", | |
2716 | app->pid, app->sock); | |
3757b385 | 2717 | ret = 0; |
be355079 JR |
2718 | } else { |
2719 | ERR("UST app creating session failed with ret %d: pid = %d, sock =%d", | |
2720 | ret, app->pid, app->sock); | |
ffe60014 | 2721 | } |
d0b96690 | 2722 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
2723 | if (ret != -ENOMEM) { |
2724 | /* | |
2725 | * Tracer is probably gone or got an internal error so let's | |
2726 | * behave like it will soon unregister or not usable. | |
2727 | */ | |
2728 | ret = -ENOTCONN; | |
2729 | } | |
2730 | goto error; | |
421cb601 DG |
2731 | } |
2732 | ||
7972aab2 DG |
2733 | ua_sess->handle = ret; |
2734 | ||
2735 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
2736 | lttng_ht_node_init_u64(&ua_sess->node, |
2737 | ua_sess->tracing_id); | |
2738 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
10b56aef MD |
2739 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
2740 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, | |
2741 | &ua_sess->ust_objd_node); | |
7972aab2 DG |
2742 | |
2743 | DBG2("UST app session created successfully with handle %d", ret); | |
2744 | } | |
2745 | ||
2746 | *ua_sess_ptr = ua_sess; | |
2747 | if (is_created) { | |
2748 | *is_created = created; | |
2749 | } | |
2750 | ||
2751 | /* Everything went well. */ | |
2752 | ret = 0; | |
2753 | ||
2754 | error: | |
2755 | health_code_update(); | |
2756 | return ret; | |
2757 | } | |
2758 | ||
6a6b2068 JG |
2759 | /* |
2760 | * Match function for a hash table lookup of ust_app_ctx. | |
2761 | * | |
2762 | * It matches an ust app context based on the context type and, in the case | |
2763 | * of perf counters, their name. | |
2764 | */ | |
2765 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
2766 | { | |
2767 | struct ust_app_ctx *ctx; | |
bdf64013 | 2768 | const struct lttng_ust_context_attr *key; |
6a6b2068 | 2769 | |
a0377dfe FD |
2770 | LTTNG_ASSERT(node); |
2771 | LTTNG_ASSERT(_key); | |
6a6b2068 JG |
2772 | |
2773 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
7966af57 | 2774 | key = (lttng_ust_context_attr *) _key; |
6a6b2068 JG |
2775 | |
2776 | /* Context type */ | |
2777 | if (ctx->ctx.ctx != key->ctx) { | |
2778 | goto no_match; | |
2779 | } | |
2780 | ||
bdf64013 | 2781 | switch(key->ctx) { |
fc4b93fa | 2782 | case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER: |
6a6b2068 | 2783 | if (strncmp(key->u.perf_counter.name, |
bdf64013 JG |
2784 | ctx->ctx.u.perf_counter.name, |
2785 | sizeof(key->u.perf_counter.name))) { | |
2786 | goto no_match; | |
2787 | } | |
2788 | break; | |
fc4b93fa | 2789 | case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT: |
bdf64013 JG |
2790 | if (strcmp(key->u.app_ctx.provider_name, |
2791 | ctx->ctx.u.app_ctx.provider_name) || | |
2792 | strcmp(key->u.app_ctx.ctx_name, | |
2793 | ctx->ctx.u.app_ctx.ctx_name)) { | |
6a6b2068 JG |
2794 | goto no_match; |
2795 | } | |
bdf64013 JG |
2796 | break; |
2797 | default: | |
2798 | break; | |
6a6b2068 JG |
2799 | } |
2800 | ||
2801 | /* Match. */ | |
2802 | return 1; | |
2803 | ||
2804 | no_match: | |
2805 | return 0; | |
2806 | } | |
2807 | ||
2808 | /* | |
2809 | * Lookup for an ust app context from an lttng_ust_context. | |
2810 | * | |
be184a0f | 2811 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
2812 | * Return an ust_app_ctx object or NULL on error. |
2813 | */ | |
2814 | static | |
2815 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
bdf64013 | 2816 | struct lttng_ust_context_attr *uctx) |
6a6b2068 JG |
2817 | { |
2818 | struct lttng_ht_iter iter; | |
2819 | struct lttng_ht_node_ulong *node; | |
2820 | struct ust_app_ctx *app_ctx = NULL; | |
2821 | ||
a0377dfe FD |
2822 | LTTNG_ASSERT(uctx); |
2823 | LTTNG_ASSERT(ht); | |
48b7cdc2 | 2824 | ASSERT_RCU_READ_LOCKED(); |
6a6b2068 JG |
2825 | |
2826 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
2827 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
2828 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
2829 | node = lttng_ht_iter_get_node_ulong(&iter); | |
2830 | if (!node) { | |
2831 | goto end; | |
2832 | } | |
2833 | ||
0114db0e | 2834 | app_ctx = lttng::utils::container_of(node, &ust_app_ctx::node); |
6a6b2068 JG |
2835 | |
2836 | end: | |
2837 | return app_ctx; | |
2838 | } | |
2839 | ||
7972aab2 DG |
2840 | /* |
2841 | * Create a context for the channel on the tracer. | |
2842 | * | |
2843 | * Called with UST app session lock held and a RCU read side lock. | |
2844 | */ | |
2845 | static | |
c9edf082 | 2846 | int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
f3db82be | 2847 | struct lttng_ust_context_attr *uctx, |
7972aab2 DG |
2848 | struct ust_app *app) |
2849 | { | |
2850 | int ret = 0; | |
7972aab2 DG |
2851 | struct ust_app_ctx *ua_ctx; |
2852 | ||
48b7cdc2 FD |
2853 | ASSERT_RCU_READ_LOCKED(); |
2854 | ||
7972aab2 DG |
2855 | DBG2("UST app adding context to channel %s", ua_chan->name); |
2856 | ||
6a6b2068 JG |
2857 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
2858 | if (ua_ctx) { | |
7972aab2 DG |
2859 | ret = -EEXIST; |
2860 | goto error; | |
2861 | } | |
2862 | ||
2863 | ua_ctx = alloc_ust_app_ctx(uctx); | |
2864 | if (ua_ctx == NULL) { | |
2865 | /* malloc failed */ | |
7682f304 | 2866 | ret = -ENOMEM; |
7972aab2 DG |
2867 | goto error; |
2868 | } | |
2869 | ||
2870 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 2871 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 2872 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
2873 | |
2874 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2875 | if (ret < 0) { | |
2876 | goto error; | |
2877 | } | |
2878 | ||
2879 | error: | |
2880 | return ret; | |
2881 | } | |
2882 | ||
2883 | /* | |
2884 | * Enable on the tracer side a ust app event for the session and channel. | |
2885 | * | |
2886 | * Called with UST app session lock held. | |
2887 | */ | |
2888 | static | |
f46376a1 MJ |
2889 | int enable_ust_app_event(struct ust_app_event *ua_event, |
2890 | struct ust_app *app) | |
7972aab2 DG |
2891 | { |
2892 | int ret; | |
2893 | ||
3428a1b7 | 2894 | ret = enable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2895 | if (ret < 0) { |
2896 | goto error; | |
2897 | } | |
2898 | ||
2899 | ua_event->enabled = 1; | |
2900 | ||
2901 | error: | |
2902 | return ret; | |
2903 | } | |
2904 | ||
2905 | /* | |
2906 | * Disable on the tracer side a ust app event for the session and channel. | |
2907 | */ | |
f46376a1 MJ |
2908 | static int disable_ust_app_event(struct ust_app_event *ua_event, |
2909 | struct ust_app *app) | |
7972aab2 DG |
2910 | { |
2911 | int ret; | |
2912 | ||
e2456d0a | 2913 | ret = disable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2914 | if (ret < 0) { |
2915 | goto error; | |
2916 | } | |
2917 | ||
2918 | ua_event->enabled = 0; | |
2919 | ||
2920 | error: | |
2921 | return ret; | |
2922 | } | |
2923 | ||
2924 | /* | |
2925 | * Lookup ust app channel for session and disable it on the tracer side. | |
2926 | */ | |
2927 | static | |
2928 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
2929 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
2930 | { | |
2931 | int ret; | |
2932 | ||
2933 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2934 | if (ret < 0) { | |
2935 | goto error; | |
2936 | } | |
2937 | ||
2938 | ua_chan->enabled = 0; | |
2939 | ||
2940 | error: | |
2941 | return ret; | |
2942 | } | |
2943 | ||
2944 | /* | |
2945 | * Lookup ust app channel for session and enable it on the tracer side. This | |
2946 | * MUST be called with a RCU read side lock acquired. | |
2947 | */ | |
2948 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
2949 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
2950 | { | |
2951 | int ret = 0; | |
2952 | struct lttng_ht_iter iter; | |
2953 | struct lttng_ht_node_str *ua_chan_node; | |
2954 | struct ust_app_channel *ua_chan; | |
2955 | ||
48b7cdc2 FD |
2956 | ASSERT_RCU_READ_LOCKED(); |
2957 | ||
7972aab2 DG |
2958 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2959 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
2960 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 2961 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
2962 | uchan->name, ua_sess->tracing_id); |
2963 | goto error; | |
2964 | } | |
2965 | ||
0114db0e | 2966 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
7972aab2 DG |
2967 | |
2968 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
2969 | if (ret < 0) { | |
2970 | goto error; | |
2971 | } | |
2972 | ||
2973 | error: | |
2974 | return ret; | |
2975 | } | |
2976 | ||
2977 | /* | |
2978 | * Ask the consumer to create a channel and get it if successful. | |
2979 | * | |
fad1ed2f JR |
2980 | * Called with UST app session lock held. |
2981 | * | |
7972aab2 DG |
2982 | * Return 0 on success or else a negative value. |
2983 | */ | |
2984 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
2985 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
b0f2e8db | 2986 | int bitness, lsu::registry_session *registry) |
7972aab2 DG |
2987 | { |
2988 | int ret; | |
2989 | unsigned int nb_fd = 0; | |
2990 | struct consumer_socket *socket; | |
2991 | ||
a0377dfe FD |
2992 | LTTNG_ASSERT(usess); |
2993 | LTTNG_ASSERT(ua_sess); | |
2994 | LTTNG_ASSERT(ua_chan); | |
2995 | LTTNG_ASSERT(registry); | |
7972aab2 DG |
2996 | |
2997 | rcu_read_lock(); | |
2998 | health_code_update(); | |
2999 | ||
3000 | /* Get the right consumer socket for the application. */ | |
3001 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
3002 | if (!socket) { | |
3003 | ret = -EINVAL; | |
3004 | goto error; | |
3005 | } | |
3006 | ||
3007 | health_code_update(); | |
3008 | ||
3009 | /* Need one fd for the channel. */ | |
3010 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3011 | if (ret < 0) { | |
3012 | ERR("Exhausted number of available FD upon create channel"); | |
3013 | goto error; | |
3014 | } | |
3015 | ||
3016 | /* | |
3017 | * Ask consumer to create channel. The consumer will return the number of | |
3018 | * stream we have to expect. | |
3019 | */ | |
3020 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
d2956687 | 3021 | registry, usess->current_trace_chunk); |
7972aab2 DG |
3022 | if (ret < 0) { |
3023 | goto error_ask; | |
3024 | } | |
3025 | ||
3026 | /* | |
3027 | * Compute the number of fd needed before receiving them. It must be 2 per | |
3028 | * stream (2 being the default value here). | |
3029 | */ | |
3030 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
3031 | ||
3032 | /* Reserve the amount of file descriptor we need. */ | |
3033 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
3034 | if (ret < 0) { | |
3035 | ERR("Exhausted number of available FD upon create channel"); | |
3036 | goto error_fd_get_stream; | |
3037 | } | |
3038 | ||
3039 | health_code_update(); | |
3040 | ||
3041 | /* | |
db786d44 | 3042 | * Now get the channel from the consumer. This call will populate the stream |
7972aab2 DG |
3043 | * list of that channel and set the ust objects. |
3044 | */ | |
d9078d0c DG |
3045 | if (usess->consumer->enabled) { |
3046 | ret = ust_consumer_get_channel(socket, ua_chan); | |
3047 | if (ret < 0) { | |
3048 | goto error_destroy; | |
3049 | } | |
7972aab2 DG |
3050 | } |
3051 | ||
3052 | rcu_read_unlock(); | |
3053 | return 0; | |
3054 | ||
3055 | error_destroy: | |
3056 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
3057 | error_fd_get_stream: | |
3058 | /* | |
3059 | * Initiate a destroy channel on the consumer since we had an error | |
3060 | * handling it on our side. The return value is of no importance since we | |
3061 | * already have a ret value set by the previous error that we need to | |
3062 | * return. | |
3063 | */ | |
3064 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
3065 | error_ask: | |
3066 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3067 | error: | |
3068 | health_code_update(); | |
3069 | rcu_read_unlock(); | |
3070 | return ret; | |
3071 | } | |
3072 | ||
3073 | /* | |
3074 | * Duplicate the ust data object of the ust app stream and save it in the | |
3075 | * buffer registry stream. | |
3076 | * | |
3077 | * Return 0 on success or else a negative value. | |
3078 | */ | |
3079 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
3080 | struct ust_app_stream *stream) | |
3081 | { | |
3082 | int ret; | |
3083 | ||
a0377dfe FD |
3084 | LTTNG_ASSERT(reg_stream); |
3085 | LTTNG_ASSERT(stream); | |
7972aab2 | 3086 | |
86ba1e22 | 3087 | /* Duplicating a stream requires 2 new fds. Reserve them. */ |
7972aab2 DG |
3088 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
3089 | if (ret < 0) { | |
3090 | ERR("Exhausted number of available FD upon duplicate stream"); | |
3091 | goto error; | |
3092 | } | |
3093 | ||
3094 | /* Duplicate object for stream once the original is in the registry. */ | |
b623cb6a | 3095 | ret = lttng_ust_ctl_duplicate_ust_object_data(&stream->obj, |
7972aab2 DG |
3096 | reg_stream->obj.ust); |
3097 | if (ret < 0) { | |
3098 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
3099 | reg_stream->obj.ust, stream->obj, ret); | |
3100 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
3101 | goto error; | |
3102 | } | |
3103 | stream->handle = stream->obj->handle; | |
3104 | ||
3105 | error: | |
3106 | return ret; | |
3107 | } | |
3108 | ||
3109 | /* | |
3110 | * Duplicate the ust data object of the ust app. channel and save it in the | |
3111 | * buffer registry channel. | |
3112 | * | |
3113 | * Return 0 on success or else a negative value. | |
3114 | */ | |
3273699d | 3115 | static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
3116 | struct ust_app_channel *ua_chan) |
3117 | { | |
3118 | int ret; | |
3119 | ||
a0377dfe FD |
3120 | LTTNG_ASSERT(buf_reg_chan); |
3121 | LTTNG_ASSERT(ua_chan); | |
7972aab2 | 3122 | |
86ba1e22 | 3123 | /* Duplicating a channel requires 1 new fd. Reserve it. */ |
7972aab2 DG |
3124 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
3125 | if (ret < 0) { | |
3126 | ERR("Exhausted number of available FD upon duplicate channel"); | |
3127 | goto error_fd_get; | |
3128 | } | |
3129 | ||
3130 | /* Duplicate object for stream once the original is in the registry. */ | |
b623cb6a | 3131 | ret = lttng_ust_ctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust); |
7972aab2 DG |
3132 | if (ret < 0) { |
3133 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
3273699d | 3134 | buf_reg_chan->obj.ust, ua_chan->obj, ret); |
7972aab2 DG |
3135 | goto error; |
3136 | } | |
3137 | ua_chan->handle = ua_chan->obj->handle; | |
3138 | ||
3139 | return 0; | |
3140 | ||
3141 | error: | |
3142 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3143 | error_fd_get: | |
3144 | return ret; | |
3145 | } | |
3146 | ||
3147 | /* | |
3148 | * For a given channel buffer registry, setup all streams of the given ust | |
3149 | * application channel. | |
3150 | * | |
3151 | * Return 0 on success or else a negative value. | |
3152 | */ | |
3273699d | 3153 | static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan, |
fb45065e MD |
3154 | struct ust_app_channel *ua_chan, |
3155 | struct ust_app *app) | |
7972aab2 DG |
3156 | { |
3157 | int ret = 0; | |
3158 | struct ust_app_stream *stream, *stmp; | |
3159 | ||
a0377dfe FD |
3160 | LTTNG_ASSERT(buf_reg_chan); |
3161 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3162 | |
3163 | DBG2("UST app setup buffer registry stream"); | |
3164 | ||
3165 | /* Send all streams to application. */ | |
3166 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
3167 | struct buffer_reg_stream *reg_stream; | |
3168 | ||
3169 | ret = buffer_reg_stream_create(®_stream); | |
3170 | if (ret < 0) { | |
3171 | goto error; | |
3172 | } | |
3173 | ||
3174 | /* | |
3175 | * Keep original pointer and nullify it in the stream so the delete | |
3176 | * stream call does not release the object. | |
3177 | */ | |
3178 | reg_stream->obj.ust = stream->obj; | |
3179 | stream->obj = NULL; | |
3273699d | 3180 | buffer_reg_stream_add(reg_stream, buf_reg_chan); |
421cb601 | 3181 | |
7972aab2 DG |
3182 | /* We don't need the streams anymore. */ |
3183 | cds_list_del(&stream->list); | |
fb45065e | 3184 | delete_ust_app_stream(-1, stream, app); |
7972aab2 | 3185 | } |
421cb601 | 3186 | |
7972aab2 DG |
3187 | error: |
3188 | return ret; | |
3189 | } | |
3190 | ||
3191 | /* | |
3192 | * Create a buffer registry channel for the given session registry and | |
3193 | * application channel object. If regp pointer is valid, it's set with the | |
3194 | * created object. Important, the created object is NOT added to the session | |
3195 | * registry hash table. | |
3196 | * | |
3197 | * Return 0 on success else a negative value. | |
3198 | */ | |
3199 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
3200 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
3201 | { | |
3202 | int ret; | |
3273699d | 3203 | struct buffer_reg_channel *buf_reg_chan = NULL; |
7972aab2 | 3204 | |
a0377dfe FD |
3205 | LTTNG_ASSERT(reg_sess); |
3206 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3207 | |
3208 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
3209 | ||
3210 | /* Create buffer registry channel. */ | |
3273699d | 3211 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan); |
7972aab2 DG |
3212 | if (ret < 0) { |
3213 | goto error_create; | |
421cb601 | 3214 | } |
a0377dfe | 3215 | LTTNG_ASSERT(buf_reg_chan); |
3273699d FD |
3216 | buf_reg_chan->consumer_key = ua_chan->key; |
3217 | buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size; | |
3218 | buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf; | |
421cb601 | 3219 | |
7972aab2 | 3220 | /* Create and add a channel registry to session. */ |
d7bfb9b0 JG |
3221 | try { |
3222 | reg_sess->reg.ust->add_channel(ua_chan->tracing_channel_id); | |
3223 | } catch (const std::exception& ex) { | |
3224 | ERR("Failed to add a channel registry to userspace registry session: %s", ex.what()); | |
3225 | ret = -1; | |
7972aab2 | 3226 | goto error; |
d88aee68 | 3227 | } |
d7bfb9b0 | 3228 | |
3273699d | 3229 | buffer_reg_channel_add(reg_sess, buf_reg_chan); |
d88aee68 | 3230 | |
7972aab2 | 3231 | if (regp) { |
3273699d | 3232 | *regp = buf_reg_chan; |
3d8ca23b | 3233 | } |
d88aee68 | 3234 | |
7972aab2 | 3235 | return 0; |
3d8ca23b DG |
3236 | |
3237 | error: | |
7972aab2 | 3238 | /* Safe because the registry channel object was not added to any HT. */ |
3273699d | 3239 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
7972aab2 | 3240 | error_create: |
3d8ca23b | 3241 | return ret; |
421cb601 DG |
3242 | } |
3243 | ||
55cc08a6 | 3244 | /* |
7972aab2 DG |
3245 | * Setup buffer registry channel for the given session registry and application |
3246 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 3247 | * |
7972aab2 | 3248 | * Return 0 on success else a negative value. |
55cc08a6 | 3249 | */ |
7972aab2 | 3250 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
3273699d | 3251 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan, |
fb45065e | 3252 | struct ust_app *app) |
55cc08a6 | 3253 | { |
7972aab2 | 3254 | int ret; |
55cc08a6 | 3255 | |
a0377dfe FD |
3256 | LTTNG_ASSERT(reg_sess); |
3257 | LTTNG_ASSERT(buf_reg_chan); | |
3258 | LTTNG_ASSERT(ua_chan); | |
3259 | LTTNG_ASSERT(ua_chan->obj); | |
55cc08a6 | 3260 | |
7972aab2 | 3261 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 3262 | |
7972aab2 | 3263 | /* Setup all streams for the registry. */ |
3273699d | 3264 | ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app); |
7972aab2 | 3265 | if (ret < 0) { |
55cc08a6 DG |
3266 | goto error; |
3267 | } | |
3268 | ||
3273699d | 3269 | buf_reg_chan->obj.ust = ua_chan->obj; |
7972aab2 | 3270 | ua_chan->obj = NULL; |
55cc08a6 | 3271 | |
7972aab2 | 3272 | return 0; |
55cc08a6 DG |
3273 | |
3274 | error: | |
3273699d FD |
3275 | buffer_reg_channel_remove(reg_sess, buf_reg_chan); |
3276 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
3277 | return ret; |
3278 | } | |
3279 | ||
edb67388 | 3280 | /* |
7972aab2 | 3281 | * Send buffer registry channel to the application. |
d0b96690 | 3282 | * |
7972aab2 | 3283 | * Return 0 on success else a negative value. |
edb67388 | 3284 | */ |
3273699d | 3285 | static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
3286 | struct ust_app *app, struct ust_app_session *ua_sess, |
3287 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
3288 | { |
3289 | int ret; | |
7972aab2 | 3290 | struct buffer_reg_stream *reg_stream; |
edb67388 | 3291 | |
a0377dfe FD |
3292 | LTTNG_ASSERT(buf_reg_chan); |
3293 | LTTNG_ASSERT(app); | |
3294 | LTTNG_ASSERT(ua_sess); | |
3295 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3296 | |
3297 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
3298 | ||
3273699d | 3299 | ret = duplicate_channel_object(buf_reg_chan, ua_chan); |
edb67388 DG |
3300 | if (ret < 0) { |
3301 | goto error; | |
3302 | } | |
3303 | ||
7972aab2 DG |
3304 | /* Send channel to the application. */ |
3305 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
3306 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3307 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
3308 | goto error; | |
be355079 JR |
3309 | } else if (ret == -EAGAIN) { |
3310 | /* Caused by timeout. */ | |
3311 | WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".", | |
3312 | app->pid, ua_chan->name, ua_sess->tracing_id); | |
3313 | /* Treat this the same way as an application that is exiting. */ | |
3314 | ret = -ENOTCONN; | |
3315 | goto error; | |
a7169585 | 3316 | } else if (ret < 0) { |
7972aab2 DG |
3317 | goto error; |
3318 | } | |
3319 | ||
3320 | health_code_update(); | |
3321 | ||
3322 | /* Send all streams to application. */ | |
3273699d FD |
3323 | pthread_mutex_lock(&buf_reg_chan->stream_list_lock); |
3324 | cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) { | |
9ee61e74 | 3325 | struct ust_app_stream stream = {}; |
7972aab2 DG |
3326 | |
3327 | ret = duplicate_stream_object(reg_stream, &stream); | |
3328 | if (ret < 0) { | |
3329 | goto error_stream_unlock; | |
3330 | } | |
3331 | ||
3332 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
3333 | if (ret < 0) { | |
a7169585 MD |
3334 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3335 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
be355079 JR |
3336 | } else if (ret == -EAGAIN) { |
3337 | /* | |
3338 | * Caused by timeout. | |
3339 | * Treat this the same way as an application | |
3340 | * that is exiting. | |
3341 | */ | |
9ee61e74 JG |
3342 | WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64 "\".", |
3343 | app->pid, | |
be355079 JR |
3344 | ua_chan->name, |
3345 | ua_sess->tracing_id); | |
3346 | ret = -ENOTCONN; | |
a7169585 | 3347 | } |
be355079 | 3348 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
3349 | goto error_stream_unlock; |
3350 | } | |
edb67388 | 3351 | |
7972aab2 DG |
3352 | /* |
3353 | * The return value is not important here. This function will output an | |
3354 | * error if needed. | |
3355 | */ | |
fb45065e | 3356 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 | 3357 | } |
7972aab2 DG |
3358 | |
3359 | error_stream_unlock: | |
3273699d | 3360 | pthread_mutex_unlock(&buf_reg_chan->stream_list_lock); |
edb67388 DG |
3361 | error: |
3362 | return ret; | |
3363 | } | |
3364 | ||
9730260e | 3365 | /* |
7972aab2 DG |
3366 | * Create and send to the application the created buffers with per UID buffers. |
3367 | * | |
9acdc1d6 | 3368 | * This MUST be called with a RCU read side lock acquired. |
71e0a100 | 3369 | * The session list lock and the session's lock must be acquired. |
9acdc1d6 | 3370 | * |
7972aab2 | 3371 | * Return 0 on success else a negative value. |
9730260e | 3372 | */ |
7972aab2 DG |
3373 | static int create_channel_per_uid(struct ust_app *app, |
3374 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3375 | struct ust_app_channel *ua_chan) | |
9730260e DG |
3376 | { |
3377 | int ret; | |
7972aab2 | 3378 | struct buffer_reg_uid *reg_uid; |
3273699d | 3379 | struct buffer_reg_channel *buf_reg_chan; |
e32d7f27 | 3380 | struct ltt_session *session = NULL; |
e098433c | 3381 | enum lttng_error_code notification_ret; |
9730260e | 3382 | |
a0377dfe FD |
3383 | LTTNG_ASSERT(app); |
3384 | LTTNG_ASSERT(usess); | |
3385 | LTTNG_ASSERT(ua_sess); | |
3386 | LTTNG_ASSERT(ua_chan); | |
48b7cdc2 | 3387 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 DG |
3388 | |
3389 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
3390 | ||
d7bfb9b0 | 3391 | reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid); |
7972aab2 DG |
3392 | /* |
3393 | * The session creation handles the creation of this global registry | |
3394 | * object. If none can be find, there is a code flow problem or a | |
3395 | * teardown race. | |
3396 | */ | |
a0377dfe | 3397 | LTTNG_ASSERT(reg_uid); |
7972aab2 | 3398 | |
3273699d | 3399 | buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, |
7972aab2 | 3400 | reg_uid); |
3273699d | 3401 | if (buf_reg_chan) { |
2721f7ea JG |
3402 | goto send_channel; |
3403 | } | |
7972aab2 | 3404 | |
2721f7ea | 3405 | /* Create the buffer registry channel object. */ |
3273699d | 3406 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan); |
2721f7ea JG |
3407 | if (ret < 0) { |
3408 | ERR("Error creating the UST channel \"%s\" registry instance", | |
f14256d6 | 3409 | ua_chan->name); |
2721f7ea JG |
3410 | goto error; |
3411 | } | |
f14256d6 | 3412 | |
e098433c | 3413 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3414 | LTTNG_ASSERT(session); |
3130a40c JG |
3415 | ASSERT_LOCKED(session->lock); |
3416 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3417 | |
2721f7ea JG |
3418 | /* |
3419 | * Create the buffers on the consumer side. This call populates the | |
3420 | * ust app channel object with all streams and data object. | |
3421 | */ | |
3422 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
d7bfb9b0 | 3423 | app->abi.bits_per_long, reg_uid->registry->reg.ust); |
2721f7ea JG |
3424 | if (ret < 0) { |
3425 | ERR("Error creating UST channel \"%s\" on the consumer daemon", | |
3426 | ua_chan->name); | |
7972aab2 DG |
3427 | |
3428 | /* | |
2721f7ea JG |
3429 | * Let's remove the previously created buffer registry channel so |
3430 | * it's not visible anymore in the session registry. | |
7972aab2 | 3431 | */ |
d7bfb9b0 JG |
3432 | auto locked_registry = reg_uid->registry->reg.ust->lock(); |
3433 | try { | |
3434 | locked_registry->remove_channel(ua_chan->tracing_channel_id, false); | |
3435 | } catch (const std::exception &ex) { | |
3436 | DBG("Could not find channel for removal: %s", ex.what()); | |
3437 | } | |
3273699d FD |
3438 | buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan); |
3439 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
2721f7ea | 3440 | goto error; |
7972aab2 DG |
3441 | } |
3442 | ||
2721f7ea JG |
3443 | /* |
3444 | * Setup the streams and add it to the session registry. | |
3445 | */ | |
3446 | ret = setup_buffer_reg_channel(reg_uid->registry, | |
3273699d | 3447 | ua_chan, buf_reg_chan, app); |
2721f7ea JG |
3448 | if (ret < 0) { |
3449 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); | |
3450 | goto error; | |
3451 | } | |
3452 | ||
d7bfb9b0 JG |
3453 | { |
3454 | auto locked_registry = reg_uid->registry->reg.ust->lock(); | |
3455 | auto& ust_reg_chan = locked_registry->get_channel(ua_chan->tracing_channel_id); | |
e9404c27 | 3456 | |
d7bfb9b0 JG |
3457 | ust_reg_chan._consumer_key = ua_chan->key; |
3458 | } | |
3459 | ||
3460 | /* Notify the notification subsystem of the channel's creation. */ | |
e098433c | 3461 | notification_ret = notification_thread_command_add_channel( |
139a8d25 | 3462 | the_notification_thread_handle, session->id, |
412d7227 | 3463 | ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, |
e098433c JG |
3464 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3465 | if (notification_ret != LTTNG_OK) { | |
3466 | ret = - (int) notification_ret; | |
3467 | ERR("Failed to add channel to notification thread"); | |
3468 | goto error; | |
e9404c27 JG |
3469 | } |
3470 | ||
2721f7ea | 3471 | send_channel: |
66ff8e3f | 3472 | /* Send buffers to the application. */ |
3273699d | 3473 | ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan); |
66ff8e3f JG |
3474 | if (ret < 0) { |
3475 | if (ret != -ENOTCONN) { | |
3476 | ERR("Error sending channel to application"); | |
3477 | } | |
3478 | goto error; | |
3479 | } | |
3480 | ||
9730260e | 3481 | error: |
e32d7f27 JG |
3482 | if (session) { |
3483 | session_put(session); | |
3484 | } | |
9730260e DG |
3485 | return ret; |
3486 | } | |
3487 | ||
78f0bacd | 3488 | /* |
7972aab2 DG |
3489 | * Create and send to the application the created buffers with per PID buffers. |
3490 | * | |
fad1ed2f | 3491 | * Called with UST app session lock held. |
71e0a100 | 3492 | * The session list lock and the session's lock must be acquired. |
fad1ed2f | 3493 | * |
7972aab2 | 3494 | * Return 0 on success else a negative value. |
78f0bacd | 3495 | */ |
7972aab2 DG |
3496 | static int create_channel_per_pid(struct ust_app *app, |
3497 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3498 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3499 | { |
8535a6d9 | 3500 | int ret; |
b0f2e8db | 3501 | lsu::registry_session *registry; |
e9404c27 | 3502 | enum lttng_error_code cmd_ret; |
e32d7f27 | 3503 | struct ltt_session *session = NULL; |
e9404c27 | 3504 | uint64_t chan_reg_key; |
78f0bacd | 3505 | |
a0377dfe FD |
3506 | LTTNG_ASSERT(app); |
3507 | LTTNG_ASSERT(usess); | |
3508 | LTTNG_ASSERT(ua_sess); | |
3509 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3510 | |
3511 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
3512 | ||
3513 | rcu_read_lock(); | |
3514 | ||
3515 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 3516 | /* The UST app session lock is held, registry shall not be null. */ |
a0377dfe | 3517 | LTTNG_ASSERT(registry); |
7972aab2 DG |
3518 | |
3519 | /* Create and add a new channel registry to session. */ | |
d7bfb9b0 JG |
3520 | try { |
3521 | registry->add_channel(ua_chan->key); | |
3522 | } catch (const std::exception& ex) { | |
3523 | ERR("Error creating the UST channel \"%s\" registry instance: %s", ua_chan->name, | |
3524 | ex.what()); | |
3525 | ret = -1; | |
78f0bacd DG |
3526 | goto error; |
3527 | } | |
3528 | ||
e098433c | 3529 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3530 | LTTNG_ASSERT(session); |
3130a40c JG |
3531 | ASSERT_LOCKED(session->lock); |
3532 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3533 | |
7972aab2 DG |
3534 | /* Create and get channel on the consumer side. */ |
3535 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
d7bfb9b0 | 3536 | app->abi.bits_per_long, registry); |
7972aab2 | 3537 | if (ret < 0) { |
f14256d6 MD |
3538 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
3539 | ua_chan->name); | |
5b951542 | 3540 | goto error_remove_from_registry; |
7972aab2 DG |
3541 | } |
3542 | ||
3543 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
3544 | if (ret < 0) { | |
a7169585 MD |
3545 | if (ret != -ENOTCONN) { |
3546 | ERR("Error sending channel to application"); | |
3547 | } | |
5b951542 | 3548 | goto error_remove_from_registry; |
7972aab2 | 3549 | } |
8535a6d9 | 3550 | |
e9404c27 | 3551 | chan_reg_key = ua_chan->key; |
d7bfb9b0 JG |
3552 | { |
3553 | auto locked_registry = registry->lock(); | |
3554 | ||
3555 | auto& ust_reg_chan = locked_registry->get_channel(chan_reg_key); | |
3556 | ust_reg_chan._consumer_key = ua_chan->key; | |
3557 | } | |
e9404c27 JG |
3558 | |
3559 | cmd_ret = notification_thread_command_add_channel( | |
139a8d25 | 3560 | the_notification_thread_handle, session->id, |
412d7227 | 3561 | ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, |
e9404c27 JG |
3562 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3563 | if (cmd_ret != LTTNG_OK) { | |
3564 | ret = - (int) cmd_ret; | |
3565 | ERR("Failed to add channel to notification thread"); | |
5b951542 | 3566 | goto error_remove_from_registry; |
e9404c27 JG |
3567 | } |
3568 | ||
5b951542 MD |
3569 | error_remove_from_registry: |
3570 | if (ret) { | |
d7bfb9b0 JG |
3571 | try { |
3572 | auto locked_registry = registry->lock(); | |
3573 | locked_registry->remove_channel(ua_chan->key, false); | |
3574 | } catch (const std::exception& ex) { | |
3575 | DBG("Could not find channel for removal: %s", ex.what()); | |
3576 | } | |
5b951542 | 3577 | } |
78f0bacd | 3578 | error: |
7972aab2 | 3579 | rcu_read_unlock(); |
e32d7f27 JG |
3580 | if (session) { |
3581 | session_put(session); | |
3582 | } | |
78f0bacd DG |
3583 | return ret; |
3584 | } | |
3585 | ||
3586 | /* | |
7972aab2 | 3587 | * From an already allocated ust app channel, create the channel buffers if |
88e3c2f5 | 3588 | * needed and send them to the application. This MUST be called with a RCU read |
7972aab2 DG |
3589 | * side lock acquired. |
3590 | * | |
fad1ed2f JR |
3591 | * Called with UST app session lock held. |
3592 | * | |
a7169585 MD |
3593 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
3594 | * the application exited concurrently. | |
78f0bacd | 3595 | */ |
88e3c2f5 | 3596 | static int ust_app_channel_send(struct ust_app *app, |
7972aab2 DG |
3597 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
3598 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3599 | { |
7972aab2 | 3600 | int ret; |
78f0bacd | 3601 | |
a0377dfe FD |
3602 | LTTNG_ASSERT(app); |
3603 | LTTNG_ASSERT(usess); | |
3604 | LTTNG_ASSERT(usess->active); | |
3605 | LTTNG_ASSERT(ua_sess); | |
3606 | LTTNG_ASSERT(ua_chan); | |
48b7cdc2 | 3607 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 DG |
3608 | |
3609 | /* Handle buffer type before sending the channel to the application. */ | |
3610 | switch (usess->buffer_type) { | |
3611 | case LTTNG_BUFFER_PER_UID: | |
3612 | { | |
3613 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
3614 | if (ret < 0) { | |
3615 | goto error; | |
3616 | } | |
3617 | break; | |
3618 | } | |
3619 | case LTTNG_BUFFER_PER_PID: | |
3620 | { | |
3621 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
3622 | if (ret < 0) { | |
3623 | goto error; | |
3624 | } | |
3625 | break; | |
3626 | } | |
3627 | default: | |
a0377dfe | 3628 | abort(); |
7972aab2 | 3629 | ret = -EINVAL; |
78f0bacd DG |
3630 | goto error; |
3631 | } | |
3632 | ||
7972aab2 DG |
3633 | /* Initialize ust objd object using the received handle and add it. */ |
3634 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
3635 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 3636 | |
7972aab2 DG |
3637 | /* If channel is not enabled, disable it on the tracer */ |
3638 | if (!ua_chan->enabled) { | |
3639 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
3640 | if (ret < 0) { | |
3641 | goto error; | |
3642 | } | |
78f0bacd DG |
3643 | } |
3644 | ||
3645 | error: | |
3646 | return ret; | |
3647 | } | |
3648 | ||
284d8f55 | 3649 | /* |
88e3c2f5 | 3650 | * Create UST app channel and return it through ua_chanp if not NULL. |
d0b96690 | 3651 | * |
36b588ed | 3652 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 3653 | * |
88e3c2f5 | 3654 | * Return 0 on success or else a negative value. |
284d8f55 | 3655 | */ |
88e3c2f5 JG |
3656 | static int ust_app_channel_allocate(struct ust_app_session *ua_sess, |
3657 | struct ltt_ust_channel *uchan, | |
f46376a1 MJ |
3658 | enum lttng_ust_abi_chan_type type, |
3659 | struct ltt_ust_session *usess __attribute__((unused)), | |
4d710ac2 | 3660 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
3661 | { |
3662 | int ret = 0; | |
bec39940 DG |
3663 | struct lttng_ht_iter iter; |
3664 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
3665 | struct ust_app_channel *ua_chan; |
3666 | ||
48b7cdc2 FD |
3667 | ASSERT_RCU_READ_LOCKED(); |
3668 | ||
5b4a0ec0 | 3669 | /* Lookup channel in the ust app session */ |
bec39940 DG |
3670 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
3671 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 3672 | if (ua_chan_node != NULL) { |
0114db0e | 3673 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
fc34caaa | 3674 | goto end; |
5b4a0ec0 DG |
3675 | } |
3676 | ||
d0b96690 | 3677 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
3678 | if (ua_chan == NULL) { |
3679 | /* Only malloc can fail here */ | |
4d710ac2 | 3680 | ret = -ENOMEM; |
88e3c2f5 | 3681 | goto error; |
fc34caaa DG |
3682 | } |
3683 | shadow_copy_channel(ua_chan, uchan); | |
3684 | ||
ffe60014 DG |
3685 | /* Set channel type. */ |
3686 | ua_chan->attr.type = type; | |
3687 | ||
d0b96690 DG |
3688 | /* Only add the channel if successful on the tracer side. */ |
3689 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
fc34caaa | 3690 | end: |
4d710ac2 DG |
3691 | if (ua_chanp) { |
3692 | *ua_chanp = ua_chan; | |
3693 | } | |
3694 | ||
3695 | /* Everything went well. */ | |
3696 | return 0; | |
5b4a0ec0 DG |
3697 | |
3698 | error: | |
4d710ac2 | 3699 | return ret; |
5b4a0ec0 DG |
3700 | } |
3701 | ||
3702 | /* | |
3703 | * Create UST app event and create it on the tracer side. | |
d0b96690 | 3704 | * |
993578ff | 3705 | * Must be called with the RCU read side lock held. |
d0b96690 | 3706 | * Called with ust app session mutex held. |
5b4a0ec0 | 3707 | */ |
edb67388 | 3708 | static |
f46376a1 MJ |
3709 | int create_ust_app_event(struct ust_app_channel *ua_chan, |
3710 | struct ltt_ust_event *uevent, | |
edb67388 | 3711 | struct ust_app *app) |
284d8f55 | 3712 | { |
edb67388 | 3713 | int ret = 0; |
5b4a0ec0 | 3714 | struct ust_app_event *ua_event; |
284d8f55 | 3715 | |
48b7cdc2 FD |
3716 | ASSERT_RCU_READ_LOCKED(); |
3717 | ||
edb67388 DG |
3718 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
3719 | if (ua_event == NULL) { | |
20533947 | 3720 | /* Only failure mode of alloc_ust_app_event(). */ |
edb67388 | 3721 | ret = -ENOMEM; |
fc34caaa | 3722 | goto end; |
5b4a0ec0 | 3723 | } |
edb67388 | 3724 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 3725 | |
edb67388 | 3726 | /* Create it on the tracer side */ |
f46376a1 | 3727 | ret = create_ust_event(app, ua_chan, ua_event); |
284d8f55 | 3728 | if (ret < 0) { |
e9f11505 JG |
3729 | /* |
3730 | * Not found previously means that it does not exist on the | |
3731 | * tracer. If the application reports that the event existed, | |
3732 | * it means there is a bug in the sessiond or lttng-ust | |
3733 | * (or corruption, etc.) | |
3734 | */ | |
3735 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3736 | ERR("Tracer for application reported that an event being created already existed: " | |
3737 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
3738 | uevent->attr.name, | |
3739 | app->pid, app->ppid, app->uid, | |
3740 | app->gid); | |
3741 | } | |
284d8f55 DG |
3742 | goto error; |
3743 | } | |
3744 | ||
d0b96690 | 3745 | add_unique_ust_app_event(ua_chan, ua_event); |
284d8f55 | 3746 | |
be355079 JR |
3747 | DBG2("UST app create event completed: app = '%s' pid = %d", |
3748 | app->name, app->pid); | |
7f79d3a1 | 3749 | |
edb67388 | 3750 | end: |
fc34caaa DG |
3751 | return ret; |
3752 | ||
5b4a0ec0 | 3753 | error: |
fc34caaa | 3754 | /* Valid. Calling here is already in a read side lock */ |
fb45065e | 3755 | delete_ust_app_event(-1, ua_event, app); |
edb67388 | 3756 | return ret; |
5b4a0ec0 DG |
3757 | } |
3758 | ||
993578ff JR |
3759 | /* |
3760 | * Create UST app event notifier rule and create it on the tracer side. | |
3761 | * | |
3762 | * Must be called with the RCU read side lock held. | |
3763 | * Called with ust app session mutex held. | |
3764 | */ | |
3765 | static | |
267d66aa JR |
3766 | int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, |
3767 | struct ust_app *app) | |
993578ff JR |
3768 | { |
3769 | int ret = 0; | |
3770 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
3771 | ||
48b7cdc2 FD |
3772 | ASSERT_RCU_READ_LOCKED(); |
3773 | ||
267d66aa | 3774 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
993578ff JR |
3775 | if (ua_event_notifier_rule == NULL) { |
3776 | ret = -ENOMEM; | |
3777 | goto end; | |
3778 | } | |
3779 | ||
3780 | /* Create it on the tracer side. */ | |
3781 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); | |
3782 | if (ret < 0) { | |
3783 | /* | |
3784 | * Not found previously means that it does not exist on the | |
3785 | * tracer. If the application reports that the event existed, | |
3786 | * it means there is a bug in the sessiond or lttng-ust | |
3787 | * (or corruption, etc.) | |
3788 | */ | |
3789 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3790 | ERR("Tracer for application reported that an event notifier being created already exists: " | |
3791 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
267d66aa | 3792 | lttng_trigger_get_tracer_token(trigger), |
993578ff JR |
3793 | app->pid, app->ppid, app->uid, |
3794 | app->gid); | |
3795 | } | |
3796 | goto error; | |
3797 | } | |
3798 | ||
3799 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, | |
3800 | &ua_event_notifier_rule->node); | |
3801 | ||
9324443a | 3802 | DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64, |
be355079 | 3803 | app->name, app->pid, lttng_trigger_get_tracer_token(trigger)); |
993578ff | 3804 | |
533a90fb | 3805 | goto end; |
993578ff JR |
3806 | |
3807 | error: | |
3808 | /* The RCU read side lock is already being held by the caller. */ | |
3809 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); | |
533a90fb | 3810 | end: |
993578ff JR |
3811 | return ret; |
3812 | } | |
3813 | ||
5b4a0ec0 DG |
3814 | /* |
3815 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 3816 | * |
7972aab2 | 3817 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
3818 | */ |
3819 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 3820 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
3821 | { |
3822 | int ret = 0; | |
ffe60014 | 3823 | struct ust_app_channel *metadata; |
d88aee68 | 3824 | struct consumer_socket *socket; |
e32d7f27 | 3825 | struct ltt_session *session = NULL; |
5b4a0ec0 | 3826 | |
a0377dfe FD |
3827 | LTTNG_ASSERT(ua_sess); |
3828 | LTTNG_ASSERT(app); | |
3829 | LTTNG_ASSERT(consumer); | |
48b7cdc2 | 3830 | ASSERT_RCU_READ_LOCKED(); |
5b4a0ec0 | 3831 | |
d7bfb9b0 | 3832 | auto locked_registry = get_locked_session_registry(ua_sess); |
fad1ed2f | 3833 | /* The UST app session is held registry shall not be null. */ |
d7bfb9b0 | 3834 | LTTNG_ASSERT(locked_registry); |
ce34fcd0 | 3835 | |
1b532a60 | 3836 | /* Metadata already exists for this registry or it was closed previously */ |
d7bfb9b0 | 3837 | if (locked_registry->_metadata_key || locked_registry->_metadata_closed) { |
7972aab2 DG |
3838 | ret = 0; |
3839 | goto error; | |
5b4a0ec0 DG |
3840 | } |
3841 | ||
ffe60014 | 3842 | /* Allocate UST metadata */ |
d0b96690 | 3843 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
3844 | if (!metadata) { |
3845 | /* malloc() failed */ | |
3846 | ret = -ENOMEM; | |
3847 | goto error; | |
3848 | } | |
5b4a0ec0 | 3849 | |
ad7a9107 | 3850 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 3851 | |
7972aab2 DG |
3852 | /* Need one fd for the channel. */ |
3853 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3854 | if (ret < 0) { | |
3855 | ERR("Exhausted number of available FD upon create metadata"); | |
3856 | goto error; | |
3857 | } | |
3858 | ||
4dc3dfc5 | 3859 | /* Get the right consumer socket for the application. */ |
d7bfb9b0 | 3860 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, consumer); |
4dc3dfc5 DG |
3861 | if (!socket) { |
3862 | ret = -EINVAL; | |
3863 | goto error_consumer; | |
3864 | } | |
3865 | ||
331744e3 JD |
3866 | /* |
3867 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
3868 | * to the registry *before* we ask the consumer so we avoid the race of the | |
3869 | * consumer requesting the metadata and the ask_channel call on our side | |
3870 | * did not returned yet. | |
3871 | */ | |
d7bfb9b0 | 3872 | locked_registry->_metadata_key = metadata->key; |
331744e3 | 3873 | |
e098433c | 3874 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3875 | LTTNG_ASSERT(session); |
3130a40c JG |
3876 | ASSERT_LOCKED(session->lock); |
3877 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3878 | |
d88aee68 DG |
3879 | /* |
3880 | * Ask the metadata channel creation to the consumer. The metadata object | |
3881 | * will be created by the consumer and kept their. However, the stream is | |
3882 | * never added or monitored until we do a first push metadata to the | |
3883 | * consumer. | |
3884 | */ | |
7972aab2 | 3885 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
d7bfb9b0 | 3886 | locked_registry.get(), session->current_trace_chunk); |
d88aee68 | 3887 | if (ret < 0) { |
f2a444f1 | 3888 | /* Nullify the metadata key so we don't try to close it later on. */ |
d7bfb9b0 | 3889 | locked_registry->_metadata_key = 0; |
d88aee68 DG |
3890 | goto error_consumer; |
3891 | } | |
3892 | ||
3893 | /* | |
3894 | * The setup command will make the metadata stream be sent to the relayd, | |
3895 | * if applicable, and the thread managing the metadatas. This is important | |
3896 | * because after this point, if an error occurs, the only way the stream | |
3897 | * can be deleted is to be monitored in the consumer. | |
3898 | */ | |
7972aab2 | 3899 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 3900 | if (ret < 0) { |
f2a444f1 | 3901 | /* Nullify the metadata key so we don't try to close it later on. */ |
d7bfb9b0 | 3902 | locked_registry->_metadata_key = 0; |
d88aee68 | 3903 | goto error_consumer; |
5b4a0ec0 DG |
3904 | } |
3905 | ||
7972aab2 DG |
3906 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
3907 | metadata->key, app->pid); | |
5b4a0ec0 | 3908 | |
d88aee68 | 3909 | error_consumer: |
b80f0b6c | 3910 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d7bfb9b0 | 3911 | delete_ust_app_channel(-1, metadata, app, locked_registry); |
5b4a0ec0 | 3912 | error: |
e32d7f27 JG |
3913 | if (session) { |
3914 | session_put(session); | |
3915 | } | |
ffe60014 | 3916 | return ret; |
5b4a0ec0 DG |
3917 | } |
3918 | ||
5b4a0ec0 | 3919 | /* |
d88aee68 DG |
3920 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
3921 | * acquired before calling this function. | |
5b4a0ec0 DG |
3922 | */ |
3923 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
3924 | { | |
d88aee68 | 3925 | struct ust_app *app = NULL; |
bec39940 DG |
3926 | struct lttng_ht_node_ulong *node; |
3927 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 3928 | |
bec39940 DG |
3929 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
3930 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
3931 | if (node == NULL) { |
3932 | DBG2("UST app no found with pid %d", pid); | |
3933 | goto error; | |
3934 | } | |
5b4a0ec0 DG |
3935 | |
3936 | DBG2("Found UST app by pid %d", pid); | |
3937 | ||
0114db0e | 3938 | app = lttng::utils::container_of(node, &ust_app::pid_n); |
5b4a0ec0 DG |
3939 | |
3940 | error: | |
d88aee68 | 3941 | return app; |
5b4a0ec0 DG |
3942 | } |
3943 | ||
d88aee68 DG |
3944 | /* |
3945 | * Allocate and init an UST app object using the registration information and | |
3946 | * the command socket. This is called when the command socket connects to the | |
3947 | * session daemon. | |
3948 | * | |
3949 | * The object is returned on success or else NULL. | |
3950 | */ | |
d0b96690 | 3951 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 3952 | { |
5e2abfaf | 3953 | int ret; |
d0b96690 | 3954 | struct ust_app *lta = NULL; |
da873412 | 3955 | struct lttng_pipe *event_notifier_event_source_pipe = NULL; |
d0b96690 | 3956 | |
a0377dfe FD |
3957 | LTTNG_ASSERT(msg); |
3958 | LTTNG_ASSERT(sock >= 0); | |
d0b96690 DG |
3959 | |
3960 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 3961 | |
173af62f | 3962 | if ((msg->bits_per_long == 64 && |
412d7227 SM |
3963 | (uatomic_read(&the_ust_consumerd64_fd) == |
3964 | -EINVAL)) || | |
3965 | (msg->bits_per_long == 32 && | |
3966 | (uatomic_read(&the_ust_consumerd32_fd) == | |
3967 | -EINVAL))) { | |
f943b0fb | 3968 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
3969 | "%d-bit long, but no consumerd for this size is available.\n", |
3970 | msg->name, msg->pid, msg->bits_per_long); | |
3971 | goto error; | |
3f2c5fcc | 3972 | } |
d0b96690 | 3973 | |
5e2abfaf JG |
3974 | /* |
3975 | * Reserve the two file descriptors of the event source pipe. The write | |
3976 | * end will be closed once it is passed to the application, at which | |
3977 | * point a single 'put' will be performed. | |
3978 | */ | |
3979 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
3980 | if (ret) { | |
be355079 JR |
3981 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d", |
3982 | msg->name, (int) msg->pid); | |
5e2abfaf JG |
3983 | goto error; |
3984 | } | |
3985 | ||
da873412 JR |
3986 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
3987 | if (!event_notifier_event_source_pipe) { | |
be355079 JR |
3988 | PERROR("Failed to open application event source pipe: '%s' (pid = %d)", |
3989 | msg->name, msg->pid); | |
da873412 JR |
3990 | goto error; |
3991 | } | |
3992 | ||
64803277 | 3993 | lta = zmalloc<ust_app>(); |
5b4a0ec0 DG |
3994 | if (lta == NULL) { |
3995 | PERROR("malloc"); | |
da873412 | 3996 | goto error_free_pipe; |
5b4a0ec0 DG |
3997 | } |
3998 | ||
da873412 JR |
3999 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
4000 | ||
5b4a0ec0 DG |
4001 | lta->ppid = msg->ppid; |
4002 | lta->uid = msg->uid; | |
4003 | lta->gid = msg->gid; | |
d0b96690 | 4004 | |
d7bfb9b0 JG |
4005 | lta->abi = { |
4006 | .bits_per_long = msg->bits_per_long, | |
4007 | .long_alignment = msg->long_alignment, | |
4008 | .uint8_t_alignment = msg->uint8_t_alignment, | |
4009 | .uint16_t_alignment = msg->uint16_t_alignment, | |
4010 | .uint32_t_alignment = msg->uint32_t_alignment, | |
4011 | .uint64_t_alignment = msg->uint64_t_alignment, | |
4012 | .byte_order = msg->byte_order == LITTLE_ENDIAN ? | |
4013 | lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_ : | |
4014 | lttng::sessiond::trace::byte_order::BIG_ENDIAN_, | |
4015 | }; | |
d0b96690 | 4016 | |
5b4a0ec0 DG |
4017 | lta->v_major = msg->major; |
4018 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 4019 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d0b96690 | 4020 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
10b56aef | 4021 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
d0b96690 | 4022 | lta->notify_sock = -1; |
993578ff | 4023 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d88aee68 DG |
4024 | |
4025 | /* Copy name and make sure it's NULL terminated. */ | |
4026 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
4027 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
4028 | ||
4029 | /* | |
4030 | * Before this can be called, when receiving the registration information, | |
4031 | * the application compatibility is checked. So, at this point, the | |
4032 | * application can work with this session daemon. | |
4033 | */ | |
d0b96690 | 4034 | lta->compatible = 1; |
5b4a0ec0 | 4035 | |
852d0037 | 4036 | lta->pid = msg->pid; |
d0b96690 | 4037 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 4038 | lta->sock = sock; |
fb45065e | 4039 | pthread_mutex_init(<a->sock_lock, NULL); |
d0b96690 | 4040 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 4041 | |
d42f20df | 4042 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
d0b96690 | 4043 | return lta; |
da873412 JR |
4044 | |
4045 | error_free_pipe: | |
4046 | lttng_pipe_destroy(event_notifier_event_source_pipe); | |
5e2abfaf | 4047 | lttng_fd_put(LTTNG_FD_APPS, 2); |
da873412 JR |
4048 | error: |
4049 | return NULL; | |
d0b96690 DG |
4050 | } |
4051 | ||
d88aee68 DG |
4052 | /* |
4053 | * For a given application object, add it to every hash table. | |
4054 | */ | |
d0b96690 DG |
4055 | void ust_app_add(struct ust_app *app) |
4056 | { | |
a0377dfe FD |
4057 | LTTNG_ASSERT(app); |
4058 | LTTNG_ASSERT(app->notify_sock >= 0); | |
d0b96690 | 4059 | |
940c4592 JR |
4060 | app->registration_time = time(NULL); |
4061 | ||
5b4a0ec0 | 4062 | rcu_read_lock(); |
852d0037 DG |
4063 | |
4064 | /* | |
4065 | * On a re-registration, we want to kick out the previous registration of | |
4066 | * that pid | |
4067 | */ | |
d0b96690 | 4068 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
4069 | |
4070 | /* | |
4071 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
4072 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
4073 | * already in the table. | |
4074 | */ | |
d0b96690 | 4075 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 4076 | |
d0b96690 DG |
4077 | /* Add application to the notify socket hash table. */ |
4078 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
4079 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 4080 | |
be355079 JR |
4081 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s " |
4082 | "notify_sock =%d (version %d.%d)", app->pid, app->ppid, app->uid, | |
d88aee68 DG |
4083 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, |
4084 | app->v_minor); | |
5b4a0ec0 | 4085 | |
d0b96690 DG |
4086 | rcu_read_unlock(); |
4087 | } | |
4088 | ||
d88aee68 DG |
4089 | /* |
4090 | * Set the application version into the object. | |
4091 | * | |
4092 | * Return 0 on success else a negative value either an errno code or a | |
4093 | * LTTng-UST error code. | |
4094 | */ | |
d0b96690 DG |
4095 | int ust_app_version(struct ust_app *app) |
4096 | { | |
d88aee68 DG |
4097 | int ret; |
4098 | ||
a0377dfe | 4099 | LTTNG_ASSERT(app); |
d88aee68 | 4100 | |
fb45065e | 4101 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4102 | ret = lttng_ust_ctl_tracer_version(app->sock, &app->version); |
fb45065e | 4103 | pthread_mutex_unlock(&app->sock_lock); |
d88aee68 | 4104 | if (ret < 0) { |
be355079 JR |
4105 | if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) { |
4106 | DBG3("UST app version failed. Application is dead: pid = %d, sock = %d", | |
4107 | app->pid, app->sock); | |
4108 | } else if (ret == -EAGAIN) { | |
4109 | WARN("UST app version failed. Communication time out: pid = %d, sock = %d", | |
4110 | app->pid, app->sock); | |
d88aee68 | 4111 | } else { |
be355079 JR |
4112 | ERR("UST app version failed with ret %d: pid = %d, sock = %d", |
4113 | ret, app->pid, app->sock); | |
d88aee68 DG |
4114 | } |
4115 | } | |
4116 | ||
4117 | return ret; | |
5b4a0ec0 DG |
4118 | } |
4119 | ||
783db316 MD |
4120 | bool ust_app_supports_notifiers(const struct ust_app *app) |
4121 | { | |
4122 | return app->v_major >= 9; | |
4123 | } | |
4124 | ||
4125 | bool ust_app_supports_counters(const struct ust_app *app) | |
4126 | { | |
4127 | return app->v_major >= 9; | |
4128 | } | |
4129 | ||
da873412 JR |
4130 | /* |
4131 | * Setup the base event notifier group. | |
4132 | * | |
4133 | * Return 0 on success else a negative value either an errno code or a | |
4134 | * LTTng-UST error code. | |
4135 | */ | |
4136 | int ust_app_setup_event_notifier_group(struct ust_app *app) | |
4137 | { | |
4138 | int ret; | |
4139 | int event_pipe_write_fd; | |
fc4b93fa | 4140 | struct lttng_ust_abi_object_data *event_notifier_group = NULL; |
da873412 | 4141 | enum lttng_error_code lttng_ret; |
533a90fb | 4142 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
da873412 | 4143 | |
a0377dfe | 4144 | LTTNG_ASSERT(app); |
da873412 | 4145 | |
783db316 MD |
4146 | if (!ust_app_supports_notifiers(app)) { |
4147 | ret = -ENOSYS; | |
4148 | goto error; | |
4149 | } | |
4150 | ||
da873412 JR |
4151 | /* Get the write side of the pipe. */ |
4152 | event_pipe_write_fd = lttng_pipe_get_writefd( | |
4153 | app->event_notifier_group.event_pipe); | |
4154 | ||
4155 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 4156 | ret = lttng_ust_ctl_create_event_notifier_group(app->sock, |
da873412 JR |
4157 | event_pipe_write_fd, &event_notifier_group); |
4158 | pthread_mutex_unlock(&app->sock_lock); | |
4159 | if (ret < 0) { | |
be355079 JR |
4160 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
4161 | ret = 0; | |
4162 | DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d", | |
4163 | app->pid, app->sock); | |
4164 | } else if (ret == -EAGAIN) { | |
4165 | ret = 0; | |
4166 | WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d", | |
4167 | app->pid, app->sock); | |
da873412 | 4168 | } else { |
be355079 JR |
4169 | ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d", |
4170 | ret, app->pid, app->sock, event_pipe_write_fd); | |
da873412 | 4171 | } |
da873412 JR |
4172 | goto error; |
4173 | } | |
4174 | ||
5d4193fd JG |
4175 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
4176 | if (ret) { | |
be355079 JR |
4177 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)", |
4178 | app->name, app->pid); | |
5d4193fd JG |
4179 | goto error; |
4180 | } | |
4181 | ||
5e2abfaf JG |
4182 | /* |
4183 | * Release the file descriptor that was reserved for the write-end of | |
4184 | * the pipe. | |
4185 | */ | |
4186 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
4187 | ||
da873412 | 4188 | lttng_ret = notification_thread_command_add_tracer_event_source( |
412d7227 SM |
4189 | the_notification_thread_handle, |
4190 | lttng_pipe_get_readfd( | |
4191 | app->event_notifier_group.event_pipe), | |
da873412 JR |
4192 | LTTNG_DOMAIN_UST); |
4193 | if (lttng_ret != LTTNG_OK) { | |
4194 | ERR("Failed to add tracer event source to notification thread"); | |
4195 | ret = - 1; | |
4196 | goto error; | |
4197 | } | |
4198 | ||
4199 | /* Assign handle only when the complete setup is valid. */ | |
4200 | app->event_notifier_group.object = event_notifier_group; | |
533a90fb | 4201 | |
a5a21280 FD |
4202 | event_notifier_error_accounting_status = |
4203 | event_notifier_error_accounting_register_app(app); | |
783db316 MD |
4204 | switch (event_notifier_error_accounting_status) { |
4205 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK: | |
4206 | break; | |
4207 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED: | |
be355079 JR |
4208 | DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d", |
4209 | app->sock, app->name, (int) app->pid); | |
783db316 MD |
4210 | ret = 0; |
4211 | goto error_accounting; | |
4212 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD: | |
be355079 JR |
4213 | DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d", |
4214 | app->sock, app->name, (int) app->pid); | |
783db316 MD |
4215 | ret = 0; |
4216 | goto error_accounting; | |
4217 | default: | |
533a90fb FD |
4218 | ERR("Failed to setup event notifier error accounting for app"); |
4219 | ret = -1; | |
cd9c532c | 4220 | goto error_accounting; |
533a90fb FD |
4221 | } |
4222 | ||
da873412 JR |
4223 | return ret; |
4224 | ||
cd9c532c FD |
4225 | error_accounting: |
4226 | lttng_ret = notification_thread_command_remove_tracer_event_source( | |
4227 | the_notification_thread_handle, | |
4228 | lttng_pipe_get_readfd( | |
4229 | app->event_notifier_group.event_pipe)); | |
4230 | if (lttng_ret != LTTNG_OK) { | |
4231 | ERR("Failed to remove application tracer event source from notification thread"); | |
4232 | } | |
4233 | ||
da873412 | 4234 | error: |
b623cb6a | 4235 | lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object); |
da873412 | 4236 | free(app->event_notifier_group.object); |
88631abd | 4237 | app->event_notifier_group.object = NULL; |
da873412 JR |
4238 | return ret; |
4239 | } | |
4240 | ||
5b4a0ec0 DG |
4241 | /* |
4242 | * Unregister app by removing it from the global traceable app list and freeing | |
4243 | * the data struct. | |
4244 | * | |
4245 | * The socket is already closed at this point so no close to sock. | |
4246 | */ | |
4247 | void ust_app_unregister(int sock) | |
4248 | { | |
4249 | struct ust_app *lta; | |
bec39940 | 4250 | struct lttng_ht_node_ulong *node; |
c4b88406 | 4251 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 4252 | struct lttng_ht_iter iter; |
d42f20df | 4253 | struct ust_app_session *ua_sess; |
525b0740 | 4254 | int ret; |
5b4a0ec0 DG |
4255 | |
4256 | rcu_read_lock(); | |
886459c6 | 4257 | |
5b4a0ec0 | 4258 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
4259 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
4260 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
a0377dfe | 4261 | LTTNG_ASSERT(node); |
284d8f55 | 4262 | |
0114db0e | 4263 | lta = lttng::utils::container_of(node, &ust_app::sock_n); |
852d0037 DG |
4264 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
4265 | ||
d88aee68 | 4266 | /* |
ce34fcd0 MD |
4267 | * For per-PID buffers, perform "push metadata" and flush all |
4268 | * application streams before removing app from hash tables, | |
4269 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 4270 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 4271 | */ |
d42f20df DG |
4272 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
4273 | node.node) { | |
4274 | ret = lttng_ht_del(lta->sessions, &iter); | |
4275 | if (ret) { | |
4276 | /* The session was already removed so scheduled for teardown. */ | |
4277 | continue; | |
4278 | } | |
4279 | ||
ce34fcd0 MD |
4280 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
4281 | (void) ust_app_flush_app_session(lta, ua_sess); | |
4282 | } | |
c4b88406 | 4283 | |
d42f20df DG |
4284 | /* |
4285 | * Add session to list for teardown. This is safe since at this point we | |
4286 | * are the only one using this list. | |
4287 | */ | |
d88aee68 DG |
4288 | pthread_mutex_lock(&ua_sess->lock); |
4289 | ||
b161602a MD |
4290 | if (ua_sess->deleted) { |
4291 | pthread_mutex_unlock(&ua_sess->lock); | |
4292 | continue; | |
4293 | } | |
4294 | ||
d88aee68 DG |
4295 | /* |
4296 | * Normally, this is done in the delete session process which is | |
4297 | * executed in the call rcu below. However, upon registration we can't | |
4298 | * afford to wait for the grace period before pushing data or else the | |
4299 | * data pending feature can race between the unregistration and stop | |
4300 | * command where the data pending command is sent *before* the grace | |
4301 | * period ended. | |
4302 | * | |
4303 | * The close metadata below nullifies the metadata pointer in the | |
4304 | * session so the delete session will NOT push/close a second time. | |
4305 | */ | |
d7bfb9b0 JG |
4306 | auto locked_registry = get_locked_session_registry(ua_sess); |
4307 | if (locked_registry) { | |
7972aab2 | 4308 | /* Push metadata for application before freeing the application. */ |
d7bfb9b0 | 4309 | (void) push_metadata(locked_registry, ua_sess->consumer); |
7972aab2 DG |
4310 | |
4311 | /* | |
4312 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
4313 | * metadata only on destroy trace session in this case. Also, the |
4314 | * previous push metadata could have flag the metadata registry to | |
4315 | * close so don't send a close command if closed. | |
7972aab2 | 4316 | */ |
ce34fcd0 | 4317 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
d7bfb9b0 JG |
4318 | const auto metadata_key = locked_registry->_metadata_key; |
4319 | const auto consumer_bitness = locked_registry->abi.bits_per_long; | |
4320 | ||
4321 | if (!locked_registry->_metadata_closed && metadata_key != 0) { | |
4322 | locked_registry->_metadata_closed = true; | |
4323 | } | |
4324 | ||
4325 | /* Release lock before communication, see comments in close_metadata(). */ | |
4326 | locked_registry.reset(); | |
4327 | (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer); | |
4328 | } else { | |
4329 | locked_registry.reset(); | |
7972aab2 DG |
4330 | } |
4331 | } | |
d42f20df | 4332 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
c4b88406 | 4333 | |
d88aee68 | 4334 | pthread_mutex_unlock(&ua_sess->lock); |
d42f20df DG |
4335 | } |
4336 | ||
c4b88406 MD |
4337 | /* Remove application from PID hash table */ |
4338 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); | |
a0377dfe | 4339 | LTTNG_ASSERT(!ret); |
c4b88406 MD |
4340 | |
4341 | /* | |
4342 | * Remove application from notify hash table. The thread handling the | |
4343 | * notify socket could have deleted the node so ignore on error because | |
c48239ca JG |
4344 | * either way it's valid. The close of that socket is handled by the |
4345 | * apps_notify_thread. | |
c4b88406 MD |
4346 | */ |
4347 | iter.iter.node = <a->notify_sock_n.node; | |
4348 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
4349 | ||
4350 | /* | |
4351 | * Ignore return value since the node might have been removed before by an | |
4352 | * add replace during app registration because the PID can be reassigned by | |
4353 | * the OS. | |
4354 | */ | |
4355 | iter.iter.node = <a->pid_n.node; | |
4356 | ret = lttng_ht_del(ust_app_ht, &iter); | |
4357 | if (ret) { | |
4358 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
4359 | lta->pid); | |
4360 | } | |
4361 | ||
852d0037 DG |
4362 | /* Free memory */ |
4363 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
4364 | ||
5b4a0ec0 DG |
4365 | rcu_read_unlock(); |
4366 | return; | |
284d8f55 DG |
4367 | } |
4368 | ||
5b4a0ec0 DG |
4369 | /* |
4370 | * Fill events array with all events name of all registered apps. | |
4371 | */ | |
4372 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 4373 | { |
5b4a0ec0 DG |
4374 | int ret, handle; |
4375 | size_t nbmem, count = 0; | |
bec39940 | 4376 | struct lttng_ht_iter iter; |
5b4a0ec0 | 4377 | struct ust_app *app; |
c617c0c6 | 4378 | struct lttng_event *tmp_event; |
421cb601 | 4379 | |
5b4a0ec0 | 4380 | nbmem = UST_APP_EVENT_LIST_SIZE; |
64803277 | 4381 | tmp_event = calloc<lttng_event>(nbmem); |
c617c0c6 | 4382 | if (tmp_event == NULL) { |
5b4a0ec0 DG |
4383 | PERROR("zmalloc ust app events"); |
4384 | ret = -ENOMEM; | |
421cb601 DG |
4385 | goto error; |
4386 | } | |
4387 | ||
5b4a0ec0 | 4388 | rcu_read_lock(); |
421cb601 | 4389 | |
852d0037 | 4390 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
fc4b93fa | 4391 | struct lttng_ust_abi_tracepoint_iter uiter; |
ac3bd9c0 | 4392 | |
840cb59c | 4393 | health_code_update(); |
86acf0da | 4394 | |
e0c7ec2b DG |
4395 | if (!app->compatible) { |
4396 | /* | |
4397 | * TODO: In time, we should notice the caller of this error by | |
4398 | * telling him that this is a version error. | |
4399 | */ | |
4400 | continue; | |
4401 | } | |
fb45065e | 4402 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4403 | handle = lttng_ust_ctl_tracepoint_list(app->sock); |
5b4a0ec0 | 4404 | if (handle < 0) { |
ffe60014 DG |
4405 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4406 | ERR("UST app list events getting handle failed for app pid %d", | |
4407 | app->pid); | |
4408 | } | |
fb45065e | 4409 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 DG |
4410 | continue; |
4411 | } | |
421cb601 | 4412 | |
b623cb6a | 4413 | while ((ret = lttng_ust_ctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 4414 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4415 | /* Handle ustctl error. */ |
4416 | if (ret < 0) { | |
fb45065e MD |
4417 | int release_ret; |
4418 | ||
a2ba1ab0 | 4419 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4420 | ERR("UST app tp list get failed for app %d with ret %d", |
4421 | app->sock, ret); | |
4422 | } else { | |
4423 | DBG3("UST app tp list get failed. Application is dead"); | |
3757b385 | 4424 | break; |
ffe60014 | 4425 | } |
98f595d4 | 4426 | free(tmp_event); |
b623cb6a | 4427 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
68313703 JG |
4428 | if (release_ret < 0 && |
4429 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4430 | release_ret != -EPIPE) { | |
fb45065e MD |
4431 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4432 | } | |
4433 | pthread_mutex_unlock(&app->sock_lock); | |
ffe60014 DG |
4434 | goto rcu_error; |
4435 | } | |
4436 | ||
840cb59c | 4437 | health_code_update(); |
815564d8 | 4438 | if (count >= nbmem) { |
d7b3776f | 4439 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4440 | struct lttng_event *new_tmp_event; |
4441 | size_t new_nbmem; | |
4442 | ||
4443 | new_nbmem = nbmem << 1; | |
4444 | DBG2("Reallocating event list from %zu to %zu entries", | |
4445 | nbmem, new_nbmem); | |
7966af57 | 4446 | new_tmp_event = (lttng_event *) realloc(tmp_event, |
53efb85a MD |
4447 | new_nbmem * sizeof(struct lttng_event)); |
4448 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4449 | int release_ret; |
4450 | ||
5b4a0ec0 | 4451 | PERROR("realloc ust app events"); |
c617c0c6 | 4452 | free(tmp_event); |
5b4a0ec0 | 4453 | ret = -ENOMEM; |
b623cb6a | 4454 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
68313703 JG |
4455 | if (release_ret < 0 && |
4456 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4457 | release_ret != -EPIPE) { | |
fb45065e MD |
4458 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4459 | } | |
4460 | pthread_mutex_unlock(&app->sock_lock); | |
5b4a0ec0 DG |
4461 | goto rcu_error; |
4462 | } | |
53efb85a MD |
4463 | /* Zero the new memory */ |
4464 | memset(new_tmp_event + nbmem, 0, | |
4465 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
4466 | nbmem = new_nbmem; | |
4467 | tmp_event = new_tmp_event; | |
5b4a0ec0 | 4468 | } |
fc4b93fa | 4469 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN); |
c617c0c6 | 4470 | tmp_event[count].loglevel = uiter.loglevel; |
fc4b93fa | 4471 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT; |
c617c0c6 MD |
4472 | tmp_event[count].pid = app->pid; |
4473 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 4474 | count++; |
421cb601 | 4475 | } |
b623cb6a | 4476 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4477 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
4478 | if (ret < 0) { |
4479 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
4480 | DBG3("Error releasing app handle. Application died: pid = %d, sock = %d", | |
4481 | app->pid, app->sock); | |
4482 | } else if (ret == -EAGAIN) { | |
4483 | WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d", | |
4484 | app->pid, app->sock); | |
4485 | } else { | |
4486 | ERR("Error releasing app handle with ret %d: pid = %d, sock = %d", | |
4487 | ret, app->pid, app->sock); | |
4488 | } | |
fb45065e | 4489 | } |
421cb601 DG |
4490 | } |
4491 | ||
5b4a0ec0 | 4492 | ret = count; |
c617c0c6 | 4493 | *events = tmp_event; |
421cb601 | 4494 | |
5b4a0ec0 | 4495 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 4496 | |
5b4a0ec0 DG |
4497 | rcu_error: |
4498 | rcu_read_unlock(); | |
421cb601 | 4499 | error: |
840cb59c | 4500 | health_code_update(); |
5b4a0ec0 | 4501 | return ret; |
421cb601 DG |
4502 | } |
4503 | ||
f37d259d MD |
4504 | /* |
4505 | * Fill events array with all events name of all registered apps. | |
4506 | */ | |
4507 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
4508 | { | |
4509 | int ret, handle; | |
4510 | size_t nbmem, count = 0; | |
4511 | struct lttng_ht_iter iter; | |
4512 | struct ust_app *app; | |
c617c0c6 | 4513 | struct lttng_event_field *tmp_event; |
f37d259d MD |
4514 | |
4515 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
64803277 | 4516 | tmp_event = calloc<lttng_event_field>(nbmem); |
c617c0c6 | 4517 | if (tmp_event == NULL) { |
f37d259d MD |
4518 | PERROR("zmalloc ust app event fields"); |
4519 | ret = -ENOMEM; | |
4520 | goto error; | |
4521 | } | |
4522 | ||
4523 | rcu_read_lock(); | |
4524 | ||
4525 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
fc4b93fa | 4526 | struct lttng_ust_abi_field_iter uiter; |
f37d259d | 4527 | |
840cb59c | 4528 | health_code_update(); |
86acf0da | 4529 | |
f37d259d MD |
4530 | if (!app->compatible) { |
4531 | /* | |
4532 | * TODO: In time, we should notice the caller of this error by | |
4533 | * telling him that this is a version error. | |
4534 | */ | |
4535 | continue; | |
4536 | } | |
fb45065e | 4537 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4538 | handle = lttng_ust_ctl_tracepoint_field_list(app->sock); |
f37d259d | 4539 | if (handle < 0) { |
ffe60014 DG |
4540 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4541 | ERR("UST app list field getting handle failed for app pid %d", | |
4542 | app->pid); | |
4543 | } | |
fb45065e | 4544 | pthread_mutex_unlock(&app->sock_lock); |
f37d259d MD |
4545 | continue; |
4546 | } | |
4547 | ||
b623cb6a | 4548 | while ((ret = lttng_ust_ctl_tracepoint_field_list_get(app->sock, handle, |
fb54cdbf | 4549 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4550 | /* Handle ustctl error. */ |
4551 | if (ret < 0) { | |
fb45065e MD |
4552 | int release_ret; |
4553 | ||
a2ba1ab0 | 4554 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4555 | ERR("UST app tp list field failed for app %d with ret %d", |
4556 | app->sock, ret); | |
4557 | } else { | |
4558 | DBG3("UST app tp list field failed. Application is dead"); | |
3757b385 | 4559 | break; |
ffe60014 | 4560 | } |
98f595d4 | 4561 | free(tmp_event); |
b623cb6a | 4562 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4563 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4564 | if (release_ret < 0 && |
4565 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4566 | release_ret != -EPIPE) { | |
fb45065e MD |
4567 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4568 | } | |
ffe60014 DG |
4569 | goto rcu_error; |
4570 | } | |
4571 | ||
840cb59c | 4572 | health_code_update(); |
f37d259d | 4573 | if (count >= nbmem) { |
d7b3776f | 4574 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4575 | struct lttng_event_field *new_tmp_event; |
4576 | size_t new_nbmem; | |
4577 | ||
4578 | new_nbmem = nbmem << 1; | |
4579 | DBG2("Reallocating event field list from %zu to %zu entries", | |
4580 | nbmem, new_nbmem); | |
7966af57 | 4581 | new_tmp_event = (lttng_event_field *) realloc(tmp_event, |
53efb85a MD |
4582 | new_nbmem * sizeof(struct lttng_event_field)); |
4583 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4584 | int release_ret; |
4585 | ||
f37d259d | 4586 | PERROR("realloc ust app event fields"); |
c617c0c6 | 4587 | free(tmp_event); |
f37d259d | 4588 | ret = -ENOMEM; |
b623cb6a | 4589 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4590 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4591 | if (release_ret && |
4592 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4593 | release_ret != -EPIPE) { | |
fb45065e MD |
4594 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4595 | } | |
f37d259d MD |
4596 | goto rcu_error; |
4597 | } | |
53efb85a MD |
4598 | /* Zero the new memory */ |
4599 | memset(new_tmp_event + nbmem, 0, | |
4600 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); | |
4601 | nbmem = new_nbmem; | |
4602 | tmp_event = new_tmp_event; | |
f37d259d | 4603 | } |
f37d259d | 4604 | |
fc4b93fa | 4605 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
2e84128e DG |
4606 | /* Mapping between these enums matches 1 to 1. */ |
4607 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; | |
c617c0c6 | 4608 | tmp_event[count].nowrite = uiter.nowrite; |
f37d259d | 4609 | |
fc4b93fa | 4610 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
c617c0c6 | 4611 | tmp_event[count].event.loglevel = uiter.loglevel; |
2e84128e | 4612 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
c617c0c6 MD |
4613 | tmp_event[count].event.pid = app->pid; |
4614 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
4615 | count++; |
4616 | } | |
b623cb6a | 4617 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4618 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4619 | if (ret < 0 && |
4620 | ret != -LTTNG_UST_ERR_EXITING && | |
4621 | ret != -EPIPE) { | |
fb45065e MD |
4622 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
4623 | } | |
f37d259d MD |
4624 | } |
4625 | ||
4626 | ret = count; | |
c617c0c6 | 4627 | *fields = tmp_event; |
f37d259d MD |
4628 | |
4629 | DBG2("UST app list event fields done (%zu events)", count); | |
4630 | ||
4631 | rcu_error: | |
4632 | rcu_read_unlock(); | |
4633 | error: | |
840cb59c | 4634 | health_code_update(); |
f37d259d MD |
4635 | return ret; |
4636 | } | |
4637 | ||
5b4a0ec0 DG |
4638 | /* |
4639 | * Free and clean all traceable apps of the global list. | |
4640 | */ | |
4641 | void ust_app_clean_list(void) | |
421cb601 | 4642 | { |
5b4a0ec0 | 4643 | int ret; |
659ed79f | 4644 | struct ust_app *app; |
bec39940 | 4645 | struct lttng_ht_iter iter; |
421cb601 | 4646 | |
5b4a0ec0 | 4647 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 4648 | |
5b4a0ec0 | 4649 | rcu_read_lock(); |
421cb601 | 4650 | |
faadaa3a JG |
4651 | /* Cleanup notify socket hash table */ |
4652 | if (ust_app_ht_by_notify_sock) { | |
4653 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, | |
4654 | notify_sock_n.node) { | |
b69a1b40 JG |
4655 | /* |
4656 | * Assert that all notifiers are gone as all triggers | |
4657 | * are unregistered prior to this clean-up. | |
4658 | */ | |
a0377dfe | 4659 | LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); |
faadaa3a | 4660 | |
faadaa3a JG |
4661 | ust_app_notify_sock_unregister(app->notify_sock); |
4662 | } | |
4663 | } | |
4664 | ||
f1b711c4 MD |
4665 | if (ust_app_ht) { |
4666 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4667 | ret = lttng_ht_del(ust_app_ht, &iter); | |
a0377dfe | 4668 | LTTNG_ASSERT(!ret); |
f1b711c4 MD |
4669 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
4670 | } | |
421cb601 DG |
4671 | } |
4672 | ||
852d0037 | 4673 | /* Cleanup socket hash table */ |
f1b711c4 MD |
4674 | if (ust_app_ht_by_sock) { |
4675 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, | |
4676 | sock_n.node) { | |
4677 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); | |
a0377dfe | 4678 | LTTNG_ASSERT(!ret); |
f1b711c4 | 4679 | } |
bec39940 | 4680 | } |
852d0037 | 4681 | |
36b588ed | 4682 | rcu_read_unlock(); |
d88aee68 | 4683 | |
bec39940 | 4684 | /* Destroy is done only when the ht is empty */ |
f1b711c4 | 4685 | if (ust_app_ht) { |
3c339053 | 4686 | lttng_ht_destroy(ust_app_ht); |
f1b711c4 MD |
4687 | } |
4688 | if (ust_app_ht_by_sock) { | |
3c339053 | 4689 | lttng_ht_destroy(ust_app_ht_by_sock); |
f1b711c4 MD |
4690 | } |
4691 | if (ust_app_ht_by_notify_sock) { | |
3c339053 | 4692 | lttng_ht_destroy(ust_app_ht_by_notify_sock); |
f1b711c4 | 4693 | } |
5b4a0ec0 DG |
4694 | } |
4695 | ||
4696 | /* | |
4697 | * Init UST app hash table. | |
4698 | */ | |
57703f6e | 4699 | int ust_app_ht_alloc(void) |
5b4a0ec0 | 4700 | { |
bec39940 | 4701 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4702 | if (!ust_app_ht) { |
4703 | return -1; | |
4704 | } | |
852d0037 | 4705 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4706 | if (!ust_app_ht_by_sock) { |
4707 | return -1; | |
4708 | } | |
d0b96690 | 4709 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4710 | if (!ust_app_ht_by_notify_sock) { |
4711 | return -1; | |
4712 | } | |
4713 | return 0; | |
421cb601 DG |
4714 | } |
4715 | ||
78f0bacd DG |
4716 | /* |
4717 | * For a specific UST session, disable the channel for all registered apps. | |
4718 | */ | |
35a9059d | 4719 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4720 | struct ltt_ust_channel *uchan) |
4721 | { | |
4722 | int ret = 0; | |
bec39940 DG |
4723 | struct lttng_ht_iter iter; |
4724 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
4725 | struct ust_app *app; |
4726 | struct ust_app_session *ua_sess; | |
8535a6d9 | 4727 | struct ust_app_channel *ua_chan; |
78f0bacd | 4728 | |
a0377dfe | 4729 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4730 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
a991f516 | 4731 | uchan->name, usess->id); |
78f0bacd DG |
4732 | |
4733 | rcu_read_lock(); | |
4734 | ||
4735 | /* For every registered applications */ | |
852d0037 | 4736 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 4737 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
4738 | if (!app->compatible) { |
4739 | /* | |
4740 | * TODO: In time, we should notice the caller of this error by | |
4741 | * telling him that this is a version error. | |
4742 | */ | |
4743 | continue; | |
4744 | } | |
78f0bacd DG |
4745 | ua_sess = lookup_session_by_app(usess, app); |
4746 | if (ua_sess == NULL) { | |
4747 | continue; | |
4748 | } | |
4749 | ||
8535a6d9 | 4750 | /* Get channel */ |
bec39940 DG |
4751 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4752 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 | 4753 | /* If the session if found for the app, the channel must be there */ |
a0377dfe | 4754 | LTTNG_ASSERT(ua_chan_node); |
8535a6d9 | 4755 | |
0114db0e | 4756 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
8535a6d9 | 4757 | /* The channel must not be already disabled */ |
a0377dfe | 4758 | LTTNG_ASSERT(ua_chan->enabled == 1); |
8535a6d9 DG |
4759 | |
4760 | /* Disable channel onto application */ | |
4761 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
4762 | if (ret < 0) { |
4763 | /* XXX: We might want to report this error at some point... */ | |
4764 | continue; | |
4765 | } | |
4766 | } | |
4767 | ||
4768 | rcu_read_unlock(); | |
78f0bacd DG |
4769 | return ret; |
4770 | } | |
4771 | ||
4772 | /* | |
4773 | * For a specific UST session, enable the channel for all registered apps. | |
4774 | */ | |
35a9059d | 4775 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4776 | struct ltt_ust_channel *uchan) |
4777 | { | |
4778 | int ret = 0; | |
bec39940 | 4779 | struct lttng_ht_iter iter; |
78f0bacd DG |
4780 | struct ust_app *app; |
4781 | struct ust_app_session *ua_sess; | |
4782 | ||
a0377dfe | 4783 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4784 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
a991f516 | 4785 | uchan->name, usess->id); |
78f0bacd DG |
4786 | |
4787 | rcu_read_lock(); | |
4788 | ||
4789 | /* For every registered applications */ | |
852d0037 | 4790 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4791 | if (!app->compatible) { |
4792 | /* | |
4793 | * TODO: In time, we should notice the caller of this error by | |
4794 | * telling him that this is a version error. | |
4795 | */ | |
4796 | continue; | |
4797 | } | |
78f0bacd DG |
4798 | ua_sess = lookup_session_by_app(usess, app); |
4799 | if (ua_sess == NULL) { | |
4800 | continue; | |
4801 | } | |
4802 | ||
4803 | /* Enable channel onto application */ | |
4804 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
4805 | if (ret < 0) { | |
4806 | /* XXX: We might want to report this error at some point... */ | |
4807 | continue; | |
4808 | } | |
4809 | } | |
4810 | ||
4811 | rcu_read_unlock(); | |
78f0bacd DG |
4812 | return ret; |
4813 | } | |
4814 | ||
b0a40d28 DG |
4815 | /* |
4816 | * Disable an event in a channel and for a specific session. | |
4817 | */ | |
35a9059d DG |
4818 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
4819 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
4820 | { |
4821 | int ret = 0; | |
bec39940 | 4822 | struct lttng_ht_iter iter, uiter; |
700c5a9d | 4823 | struct lttng_ht_node_str *ua_chan_node; |
b0a40d28 DG |
4824 | struct ust_app *app; |
4825 | struct ust_app_session *ua_sess; | |
4826 | struct ust_app_channel *ua_chan; | |
4827 | struct ust_app_event *ua_event; | |
4828 | ||
a0377dfe | 4829 | LTTNG_ASSERT(usess->active); |
b0a40d28 | 4830 | DBG("UST app disabling event %s for all apps in channel " |
d9bf3ca4 MD |
4831 | "%s for session id %" PRIu64, |
4832 | uevent->attr.name, uchan->name, usess->id); | |
b0a40d28 DG |
4833 | |
4834 | rcu_read_lock(); | |
4835 | ||
4836 | /* For all registered applications */ | |
852d0037 | 4837 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4838 | if (!app->compatible) { |
4839 | /* | |
4840 | * TODO: In time, we should notice the caller of this error by | |
4841 | * telling him that this is a version error. | |
4842 | */ | |
4843 | continue; | |
4844 | } | |
b0a40d28 DG |
4845 | ua_sess = lookup_session_by_app(usess, app); |
4846 | if (ua_sess == NULL) { | |
4847 | /* Next app */ | |
4848 | continue; | |
4849 | } | |
4850 | ||
4851 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
4852 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4853 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 4854 | if (ua_chan_node == NULL) { |
d9bf3ca4 | 4855 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
852d0037 | 4856 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
4857 | continue; |
4858 | } | |
0114db0e | 4859 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
b0a40d28 | 4860 | |
700c5a9d JR |
4861 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
4862 | uevent->filter, uevent->attr.loglevel, | |
4863 | uevent->exclusion); | |
4864 | if (ua_event == NULL) { | |
b0a40d28 | 4865 | DBG2("Event %s not found in channel %s for app pid %d." |
852d0037 | 4866 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
4867 | continue; |
4868 | } | |
b0a40d28 | 4869 | |
f46376a1 | 4870 | ret = disable_ust_app_event(ua_event, app); |
b0a40d28 DG |
4871 | if (ret < 0) { |
4872 | /* XXX: Report error someday... */ | |
4873 | continue; | |
4874 | } | |
4875 | } | |
4876 | ||
4877 | rcu_read_unlock(); | |
88e3c2f5 JG |
4878 | return ret; |
4879 | } | |
4880 | ||
4881 | /* The ua_sess lock must be held by the caller. */ | |
4882 | static | |
4883 | int ust_app_channel_create(struct ltt_ust_session *usess, | |
4884 | struct ust_app_session *ua_sess, | |
4885 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
4886 | struct ust_app_channel **_ua_chan) | |
4887 | { | |
4888 | int ret = 0; | |
4889 | struct ust_app_channel *ua_chan = NULL; | |
4890 | ||
a0377dfe | 4891 | LTTNG_ASSERT(ua_sess); |
88e3c2f5 JG |
4892 | ASSERT_LOCKED(ua_sess->lock); |
4893 | ||
4894 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, | |
4895 | sizeof(uchan->name))) { | |
4896 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, | |
4897 | &uchan->attr); | |
4898 | ret = 0; | |
4899 | } else { | |
4900 | struct ltt_ust_context *uctx = NULL; | |
4901 | ||
4902 | /* | |
4903 | * Create channel onto application and synchronize its | |
4904 | * configuration. | |
4905 | */ | |
4906 | ret = ust_app_channel_allocate(ua_sess, uchan, | |
fc4b93fa | 4907 | LTTNG_UST_ABI_CHAN_PER_CPU, usess, |
88e3c2f5 | 4908 | &ua_chan); |
88ebf5a7 JR |
4909 | if (ret < 0) { |
4910 | goto error; | |
4911 | } | |
4912 | ||
4913 | ret = ust_app_channel_send(app, usess, | |
4914 | ua_sess, ua_chan); | |
4915 | if (ret) { | |
4916 | goto error; | |
88e3c2f5 JG |
4917 | } |
4918 | ||
4919 | /* Add contexts. */ | |
4920 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
4921 | ret = create_ust_app_channel_context(ua_chan, | |
4922 | &uctx->ctx, app); | |
4923 | if (ret) { | |
88ebf5a7 | 4924 | goto error; |
88e3c2f5 JG |
4925 | } |
4926 | } | |
4927 | } | |
88ebf5a7 JR |
4928 | |
4929 | error: | |
88e3c2f5 JG |
4930 | if (ret < 0) { |
4931 | switch (ret) { | |
4932 | case -ENOTCONN: | |
4933 | /* | |
4934 | * The application's socket is not valid. Either a bad socket | |
4935 | * or a timeout on it. We can't inform the caller that for a | |
4936 | * specific app, the session failed so lets continue here. | |
4937 | */ | |
4938 | ret = 0; /* Not an error. */ | |
4939 | break; | |
4940 | case -ENOMEM: | |
4941 | default: | |
4942 | break; | |
4943 | } | |
4944 | } | |
88ebf5a7 | 4945 | |
88e3c2f5 JG |
4946 | if (ret == 0 && _ua_chan) { |
4947 | /* | |
4948 | * Only return the application's channel on success. Note | |
4949 | * that the channel can still be part of the application's | |
4950 | * channel hashtable on error. | |
4951 | */ | |
4952 | *_ua_chan = ua_chan; | |
4953 | } | |
b0a40d28 DG |
4954 | return ret; |
4955 | } | |
4956 | ||
5b4a0ec0 | 4957 | /* |
edb67388 | 4958 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 4959 | */ |
35a9059d | 4960 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
4961 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4962 | { | |
4963 | int ret = 0; | |
bec39940 | 4964 | struct lttng_ht_iter iter, uiter; |
18eace3b | 4965 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
4966 | struct ust_app *app; |
4967 | struct ust_app_session *ua_sess; | |
4968 | struct ust_app_channel *ua_chan; | |
4969 | struct ust_app_event *ua_event; | |
48842b30 | 4970 | |
a0377dfe | 4971 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4972 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
a991f516 | 4973 | uevent->attr.name, usess->id); |
48842b30 | 4974 | |
edb67388 DG |
4975 | /* |
4976 | * NOTE: At this point, this function is called only if the session and | |
4977 | * channel passed are already created for all apps. and enabled on the | |
4978 | * tracer also. | |
4979 | */ | |
4980 | ||
48842b30 | 4981 | rcu_read_lock(); |
421cb601 DG |
4982 | |
4983 | /* For all registered applications */ | |
852d0037 | 4984 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4985 | if (!app->compatible) { |
4986 | /* | |
4987 | * TODO: In time, we should notice the caller of this error by | |
4988 | * telling him that this is a version error. | |
4989 | */ | |
4990 | continue; | |
4991 | } | |
edb67388 | 4992 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4993 | if (!ua_sess) { |
4994 | /* The application has problem or is probably dead. */ | |
4995 | continue; | |
4996 | } | |
ba767faf | 4997 | |
d0b96690 DG |
4998 | pthread_mutex_lock(&ua_sess->lock); |
4999 | ||
b161602a MD |
5000 | if (ua_sess->deleted) { |
5001 | pthread_mutex_unlock(&ua_sess->lock); | |
5002 | continue; | |
5003 | } | |
5004 | ||
edb67388 | 5005 | /* Lookup channel in the ust app session */ |
bec39940 DG |
5006 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
5007 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
a7169585 MD |
5008 | /* |
5009 | * It is possible that the channel cannot be found is | |
5010 | * the channel/event creation occurs concurrently with | |
5011 | * an application exit. | |
5012 | */ | |
5013 | if (!ua_chan_node) { | |
5014 | pthread_mutex_unlock(&ua_sess->lock); | |
5015 | continue; | |
5016 | } | |
edb67388 | 5017 | |
0114db0e | 5018 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
edb67388 | 5019 | |
18eace3b DG |
5020 | /* Get event node */ |
5021 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
39c5a3a7 | 5022 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 5023 | if (ua_event == NULL) { |
7f79d3a1 | 5024 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 5025 | "Skipping app", uevent->attr.name, app->pid); |
d0b96690 | 5026 | goto next_app; |
35a9059d | 5027 | } |
35a9059d | 5028 | |
f46376a1 | 5029 | ret = enable_ust_app_event(ua_event, app); |
35a9059d | 5030 | if (ret < 0) { |
d0b96690 | 5031 | pthread_mutex_unlock(&ua_sess->lock); |
7f79d3a1 | 5032 | goto error; |
48842b30 | 5033 | } |
d0b96690 DG |
5034 | next_app: |
5035 | pthread_mutex_unlock(&ua_sess->lock); | |
edb67388 DG |
5036 | } |
5037 | ||
7f79d3a1 | 5038 | error: |
edb67388 | 5039 | rcu_read_unlock(); |
edb67388 DG |
5040 | return ret; |
5041 | } | |
5042 | ||
5043 | /* | |
5044 | * For a specific existing UST session and UST channel, creates the event for | |
5045 | * all registered apps. | |
5046 | */ | |
35a9059d | 5047 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
5048 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
5049 | { | |
5050 | int ret = 0; | |
bec39940 DG |
5051 | struct lttng_ht_iter iter, uiter; |
5052 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
5053 | struct ust_app *app; |
5054 | struct ust_app_session *ua_sess; | |
5055 | struct ust_app_channel *ua_chan; | |
5056 | ||
a0377dfe | 5057 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 5058 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
a991f516 | 5059 | uevent->attr.name, usess->id); |
edb67388 | 5060 | |
edb67388 DG |
5061 | rcu_read_lock(); |
5062 | ||
5063 | /* For all registered applications */ | |
852d0037 | 5064 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
5065 | if (!app->compatible) { |
5066 | /* | |
5067 | * TODO: In time, we should notice the caller of this error by | |
5068 | * telling him that this is a version error. | |
5069 | */ | |
5070 | continue; | |
5071 | } | |
edb67388 | 5072 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
5073 | if (!ua_sess) { |
5074 | /* The application has problem or is probably dead. */ | |
5075 | continue; | |
5076 | } | |
48842b30 | 5077 | |
d0b96690 | 5078 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
5079 | |
5080 | if (ua_sess->deleted) { | |
5081 | pthread_mutex_unlock(&ua_sess->lock); | |
5082 | continue; | |
5083 | } | |
5084 | ||
48842b30 | 5085 | /* Lookup channel in the ust app session */ |
bec39940 DG |
5086 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
5087 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 | 5088 | /* If the channel is not found, there is a code flow error */ |
a0377dfe | 5089 | LTTNG_ASSERT(ua_chan_node); |
edb67388 | 5090 | |
0114db0e | 5091 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
48842b30 | 5092 | |
f46376a1 | 5093 | ret = create_ust_app_event(ua_chan, uevent, app); |
d0b96690 | 5094 | pthread_mutex_unlock(&ua_sess->lock); |
edb67388 | 5095 | if (ret < 0) { |
49c336c1 | 5096 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
5097 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
5098 | break; | |
5099 | } | |
5100 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 5101 | uevent->attr.name, app->pid); |
5b4a0ec0 | 5102 | continue; |
48842b30 | 5103 | } |
48842b30 | 5104 | } |
5b4a0ec0 | 5105 | |
48842b30 | 5106 | rcu_read_unlock(); |
48842b30 DG |
5107 | return ret; |
5108 | } | |
5109 | ||
5b4a0ec0 DG |
5110 | /* |
5111 | * Start tracing for a specific UST session and app. | |
fad1ed2f JR |
5112 | * |
5113 | * Called with UST app session lock held. | |
5114 | * | |
5b4a0ec0 | 5115 | */ |
b34cbebf | 5116 | static |
421cb601 | 5117 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
5118 | { |
5119 | int ret = 0; | |
48842b30 | 5120 | struct ust_app_session *ua_sess; |
48842b30 | 5121 | |
852d0037 | 5122 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 5123 | |
509cbaf8 MD |
5124 | rcu_read_lock(); |
5125 | ||
e0c7ec2b DG |
5126 | if (!app->compatible) { |
5127 | goto end; | |
5128 | } | |
5129 | ||
421cb601 DG |
5130 | ua_sess = lookup_session_by_app(usess, app); |
5131 | if (ua_sess == NULL) { | |
d42f20df DG |
5132 | /* The session is in teardown process. Ignore and continue. */ |
5133 | goto end; | |
421cb601 | 5134 | } |
48842b30 | 5135 | |
d0b96690 DG |
5136 | pthread_mutex_lock(&ua_sess->lock); |
5137 | ||
b161602a MD |
5138 | if (ua_sess->deleted) { |
5139 | pthread_mutex_unlock(&ua_sess->lock); | |
5140 | goto end; | |
5141 | } | |
5142 | ||
b0a1c741 JR |
5143 | if (ua_sess->enabled) { |
5144 | pthread_mutex_unlock(&ua_sess->lock); | |
5145 | goto end; | |
5146 | } | |
5147 | ||
aea829b3 DG |
5148 | /* Upon restart, we skip the setup, already done */ |
5149 | if (ua_sess->started) { | |
8be98f9a | 5150 | goto skip_setup; |
aea829b3 | 5151 | } |
8be98f9a | 5152 | |
840cb59c | 5153 | health_code_update(); |
86acf0da | 5154 | |
8be98f9a | 5155 | skip_setup: |
a945cdc7 | 5156 | /* This starts the UST tracing */ |
fb45065e | 5157 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5158 | ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle); |
fb45065e | 5159 | pthread_mutex_unlock(&app->sock_lock); |
421cb601 | 5160 | if (ret < 0) { |
be355079 JR |
5161 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
5162 | DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d", | |
5163 | app->pid, app->sock); | |
5164 | pthread_mutex_unlock(&ua_sess->lock); | |
5165 | goto end; | |
5166 | } else if (ret == -EAGAIN) { | |
5167 | WARN("UST app start session failed. Communication time out: pid = %d, sock = %d", | |
5168 | app->pid, app->sock); | |
3757b385 DG |
5169 | pthread_mutex_unlock(&ua_sess->lock); |
5170 | goto end; | |
be355079 JR |
5171 | |
5172 | } else { | |
5173 | ERR("UST app start session failed with ret %d: pid = %d, sock = %d", | |
5174 | ret, app->pid, app->sock); | |
ffe60014 | 5175 | } |
d0b96690 | 5176 | goto error_unlock; |
421cb601 | 5177 | } |
5b4a0ec0 | 5178 | |
55c3953d DG |
5179 | /* Indicate that the session has been started once */ |
5180 | ua_sess->started = 1; | |
b0a1c741 | 5181 | ua_sess->enabled = 1; |
55c3953d | 5182 | |
d0b96690 DG |
5183 | pthread_mutex_unlock(&ua_sess->lock); |
5184 | ||
840cb59c | 5185 | health_code_update(); |
86acf0da | 5186 | |
421cb601 | 5187 | /* Quiescent wait after starting trace */ |
fb45065e | 5188 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5189 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5190 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5191 | if (ret < 0) { |
5192 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
5193 | DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d", | |
5194 | app->pid, app->sock); | |
5195 | } else if (ret == -EAGAIN) { | |
5196 | WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d", | |
5197 | app->pid, app->sock); | |
5198 | } else { | |
5199 | ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d", | |
5200 | ret, app->pid, app->sock); | |
5201 | } | |
ffe60014 | 5202 | } |
48842b30 | 5203 | |
e0c7ec2b DG |
5204 | end: |
5205 | rcu_read_unlock(); | |
840cb59c | 5206 | health_code_update(); |
421cb601 | 5207 | return 0; |
48842b30 | 5208 | |
d0b96690 DG |
5209 | error_unlock: |
5210 | pthread_mutex_unlock(&ua_sess->lock); | |
509cbaf8 | 5211 | rcu_read_unlock(); |
840cb59c | 5212 | health_code_update(); |
421cb601 DG |
5213 | return -1; |
5214 | } | |
48842b30 | 5215 | |
8be98f9a MD |
5216 | /* |
5217 | * Stop tracing for a specific UST session and app. | |
5218 | */ | |
b34cbebf | 5219 | static |
8be98f9a MD |
5220 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
5221 | { | |
5222 | int ret = 0; | |
5223 | struct ust_app_session *ua_sess; | |
5224 | ||
852d0037 | 5225 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
5226 | |
5227 | rcu_read_lock(); | |
5228 | ||
e0c7ec2b | 5229 | if (!app->compatible) { |
d88aee68 | 5230 | goto end_no_session; |
e0c7ec2b DG |
5231 | } |
5232 | ||
8be98f9a MD |
5233 | ua_sess = lookup_session_by_app(usess, app); |
5234 | if (ua_sess == NULL) { | |
d88aee68 | 5235 | goto end_no_session; |
8be98f9a MD |
5236 | } |
5237 | ||
d88aee68 DG |
5238 | pthread_mutex_lock(&ua_sess->lock); |
5239 | ||
b161602a MD |
5240 | if (ua_sess->deleted) { |
5241 | pthread_mutex_unlock(&ua_sess->lock); | |
5242 | goto end_no_session; | |
5243 | } | |
5244 | ||
9bc07046 DG |
5245 | /* |
5246 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
5247 | * that was never started. It's possible since we can have a fail start |
5248 | * from either the application manager thread or the command thread. Simply | |
5249 | * indicate that this is a stop error. | |
9bc07046 | 5250 | */ |
f9dfc3d9 | 5251 | if (!ua_sess->started) { |
c45536e1 DG |
5252 | goto error_rcu_unlock; |
5253 | } | |
7db205b5 | 5254 | |
840cb59c | 5255 | health_code_update(); |
86acf0da | 5256 | |
9d6c7d3f | 5257 | /* This inhibits UST tracing */ |
fb45065e | 5258 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5259 | ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle); |
fb45065e | 5260 | pthread_mutex_unlock(&app->sock_lock); |
9d6c7d3f | 5261 | if (ret < 0) { |
be355079 JR |
5262 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
5263 | DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d", | |
5264 | app->pid, app->sock); | |
3757b385 | 5265 | goto end_unlock; |
be355079 JR |
5266 | } else if (ret == -EAGAIN) { |
5267 | WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d", | |
5268 | app->pid, app->sock); | |
5269 | goto end_unlock; | |
5270 | ||
5271 | } else { | |
5272 | ERR("UST app stop session failed with ret %d: pid = %d, sock = %d", | |
5273 | ret, app->pid, app->sock); | |
ffe60014 | 5274 | } |
9d6c7d3f DG |
5275 | goto error_rcu_unlock; |
5276 | } | |
5277 | ||
840cb59c | 5278 | health_code_update(); |
b0a1c741 | 5279 | ua_sess->enabled = 0; |
86acf0da | 5280 | |
9d6c7d3f | 5281 | /* Quiescent wait after stopping trace */ |
fb45065e | 5282 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5283 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5284 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5285 | if (ret < 0) { |
5286 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
9324443a | 5287 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
be355079 JR |
5288 | app->pid, app->sock); |
5289 | } else if (ret == -EAGAIN) { | |
9324443a | 5290 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
be355079 JR |
5291 | app->pid, app->sock); |
5292 | } else { | |
9324443a | 5293 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
be355079 JR |
5294 | ret, app->pid, app->sock); |
5295 | } | |
ffe60014 | 5296 | } |
9d6c7d3f | 5297 | |
840cb59c | 5298 | health_code_update(); |
86acf0da | 5299 | |
d7bfb9b0 JG |
5300 | { |
5301 | auto locked_registry = get_locked_session_registry(ua_sess); | |
fad1ed2f | 5302 | |
d7bfb9b0 JG |
5303 | /* The UST app session is held registry shall not be null. */ |
5304 | LTTNG_ASSERT(locked_registry); | |
1b532a60 | 5305 | |
d7bfb9b0 JG |
5306 | /* Push metadata for application before freeing the application. */ |
5307 | (void) push_metadata(locked_registry, ua_sess->consumer); | |
5308 | } | |
b34cbebf | 5309 | |
3757b385 | 5310 | end_unlock: |
b34cbebf MD |
5311 | pthread_mutex_unlock(&ua_sess->lock); |
5312 | end_no_session: | |
5313 | rcu_read_unlock(); | |
5314 | health_code_update(); | |
5315 | return 0; | |
5316 | ||
5317 | error_rcu_unlock: | |
5318 | pthread_mutex_unlock(&ua_sess->lock); | |
5319 | rcu_read_unlock(); | |
5320 | health_code_update(); | |
5321 | return -1; | |
5322 | } | |
5323 | ||
b34cbebf | 5324 | static |
c4b88406 MD |
5325 | int ust_app_flush_app_session(struct ust_app *app, |
5326 | struct ust_app_session *ua_sess) | |
b34cbebf | 5327 | { |
c4b88406 | 5328 | int ret, retval = 0; |
b34cbebf | 5329 | struct lttng_ht_iter iter; |
b34cbebf | 5330 | struct ust_app_channel *ua_chan; |
c4b88406 | 5331 | struct consumer_socket *socket; |
b34cbebf | 5332 | |
c4b88406 | 5333 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
b34cbebf MD |
5334 | |
5335 | rcu_read_lock(); | |
5336 | ||
5337 | if (!app->compatible) { | |
c4b88406 | 5338 | goto end_not_compatible; |
b34cbebf MD |
5339 | } |
5340 | ||
5341 | pthread_mutex_lock(&ua_sess->lock); | |
5342 | ||
b161602a MD |
5343 | if (ua_sess->deleted) { |
5344 | goto end_deleted; | |
5345 | } | |
5346 | ||
b34cbebf MD |
5347 | health_code_update(); |
5348 | ||
9d6c7d3f | 5349 | /* Flushing buffers */ |
d7bfb9b0 | 5350 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
c4b88406 | 5351 | ua_sess->consumer); |
ce34fcd0 MD |
5352 | |
5353 | /* Flush buffers and push metadata. */ | |
5354 | switch (ua_sess->buffer_type) { | |
5355 | case LTTNG_BUFFER_PER_PID: | |
5356 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, | |
5357 | node.node) { | |
5358 | health_code_update(); | |
ce34fcd0 MD |
5359 | ret = consumer_flush_channel(socket, ua_chan->key); |
5360 | if (ret) { | |
5361 | ERR("Error flushing consumer channel"); | |
5362 | retval = -1; | |
5363 | continue; | |
5364 | } | |
8be98f9a | 5365 | } |
ce34fcd0 MD |
5366 | break; |
5367 | case LTTNG_BUFFER_PER_UID: | |
5368 | default: | |
a0377dfe | 5369 | abort(); |
ce34fcd0 | 5370 | break; |
8be98f9a | 5371 | } |
8be98f9a | 5372 | |
840cb59c | 5373 | health_code_update(); |
86acf0da | 5374 | |
b161602a | 5375 | end_deleted: |
d88aee68 | 5376 | pthread_mutex_unlock(&ua_sess->lock); |
ce34fcd0 | 5377 | |
c4b88406 MD |
5378 | end_not_compatible: |
5379 | rcu_read_unlock(); | |
5380 | health_code_update(); | |
5381 | return retval; | |
5382 | } | |
5383 | ||
5384 | /* | |
ce34fcd0 MD |
5385 | * Flush buffers for all applications for a specific UST session. |
5386 | * Called with UST session lock held. | |
c4b88406 MD |
5387 | */ |
5388 | static | |
ce34fcd0 | 5389 | int ust_app_flush_session(struct ltt_ust_session *usess) |
c4b88406 MD |
5390 | |
5391 | { | |
99b1411c | 5392 | int ret = 0; |
c4b88406 | 5393 | |
ce34fcd0 | 5394 | DBG("Flushing session buffers for all ust apps"); |
c4b88406 MD |
5395 | |
5396 | rcu_read_lock(); | |
5397 | ||
ce34fcd0 MD |
5398 | /* Flush buffers and push metadata. */ |
5399 | switch (usess->buffer_type) { | |
5400 | case LTTNG_BUFFER_PER_UID: | |
5401 | { | |
5402 | struct buffer_reg_uid *reg; | |
5403 | struct lttng_ht_iter iter; | |
5404 | ||
5405 | /* Flush all per UID buffers associated to that session. */ | |
5406 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
b0f2e8db | 5407 | lsu::registry_session *ust_session_reg; |
3273699d | 5408 | struct buffer_reg_channel *buf_reg_chan; |
ce34fcd0 MD |
5409 | struct consumer_socket *socket; |
5410 | ||
5411 | /* Get consumer socket to use to push the metadata.*/ | |
5412 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
5413 | usess->consumer); | |
5414 | if (!socket) { | |
5415 | /* Ignore request if no consumer is found for the session. */ | |
5416 | continue; | |
5417 | } | |
5418 | ||
5419 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 5420 | buf_reg_chan, node.node) { |
ce34fcd0 MD |
5421 | /* |
5422 | * The following call will print error values so the return | |
5423 | * code is of little importance because whatever happens, we | |
5424 | * have to try them all. | |
5425 | */ | |
3273699d | 5426 | (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key); |
ce34fcd0 MD |
5427 | } |
5428 | ||
5429 | ust_session_reg = reg->registry->reg.ust; | |
5430 | /* Push metadata. */ | |
d7bfb9b0 JG |
5431 | auto locked_registry = ust_session_reg->lock(); |
5432 | (void) push_metadata(locked_registry, usess->consumer); | |
ce34fcd0 | 5433 | } |
ce34fcd0 MD |
5434 | break; |
5435 | } | |
5436 | case LTTNG_BUFFER_PER_PID: | |
5437 | { | |
5438 | struct ust_app_session *ua_sess; | |
5439 | struct lttng_ht_iter iter; | |
5440 | struct ust_app *app; | |
5441 | ||
5442 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5443 | ua_sess = lookup_session_by_app(usess, app); | |
5444 | if (ua_sess == NULL) { | |
5445 | continue; | |
5446 | } | |
5447 | (void) ust_app_flush_app_session(app, ua_sess); | |
5448 | } | |
5449 | break; | |
5450 | } | |
5451 | default: | |
99b1411c | 5452 | ret = -1; |
a0377dfe | 5453 | abort(); |
ce34fcd0 | 5454 | break; |
c4b88406 | 5455 | } |
c4b88406 | 5456 | |
7db205b5 | 5457 | rcu_read_unlock(); |
840cb59c | 5458 | health_code_update(); |
c4b88406 | 5459 | return ret; |
8be98f9a MD |
5460 | } |
5461 | ||
0dd01979 MD |
5462 | static |
5463 | int ust_app_clear_quiescent_app_session(struct ust_app *app, | |
5464 | struct ust_app_session *ua_sess) | |
5465 | { | |
5466 | int ret = 0; | |
5467 | struct lttng_ht_iter iter; | |
5468 | struct ust_app_channel *ua_chan; | |
5469 | struct consumer_socket *socket; | |
5470 | ||
5471 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); | |
5472 | ||
5473 | rcu_read_lock(); | |
5474 | ||
5475 | if (!app->compatible) { | |
5476 | goto end_not_compatible; | |
5477 | } | |
5478 | ||
5479 | pthread_mutex_lock(&ua_sess->lock); | |
5480 | ||
5481 | if (ua_sess->deleted) { | |
5482 | goto end_unlock; | |
5483 | } | |
5484 | ||
5485 | health_code_update(); | |
5486 | ||
d7bfb9b0 | 5487 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
0dd01979 MD |
5488 | ua_sess->consumer); |
5489 | if (!socket) { | |
5490 | ERR("Failed to find consumer (%" PRIu32 ") socket", | |
d7bfb9b0 | 5491 | app->abi.bits_per_long); |
0dd01979 MD |
5492 | ret = -1; |
5493 | goto end_unlock; | |
5494 | } | |
5495 | ||
5496 | /* Clear quiescent state. */ | |
5497 | switch (ua_sess->buffer_type) { | |
5498 | case LTTNG_BUFFER_PER_PID: | |
5499 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, | |
5500 | ua_chan, node.node) { | |
5501 | health_code_update(); | |
5502 | ret = consumer_clear_quiescent_channel(socket, | |
5503 | ua_chan->key); | |
5504 | if (ret) { | |
5505 | ERR("Error clearing quiescent state for consumer channel"); | |
5506 | ret = -1; | |
5507 | continue; | |
5508 | } | |
5509 | } | |
5510 | break; | |
5511 | case LTTNG_BUFFER_PER_UID: | |
5512 | default: | |
a0377dfe | 5513 | abort(); |
0dd01979 MD |
5514 | ret = -1; |
5515 | break; | |
5516 | } | |
5517 | ||
5518 | health_code_update(); | |
5519 | ||
5520 | end_unlock: | |
5521 | pthread_mutex_unlock(&ua_sess->lock); | |
5522 | ||
5523 | end_not_compatible: | |
5524 | rcu_read_unlock(); | |
5525 | health_code_update(); | |
5526 | return ret; | |
5527 | } | |
5528 | ||
5529 | /* | |
5530 | * Clear quiescent state in each stream for all applications for a | |
5531 | * specific UST session. | |
5532 | * Called with UST session lock held. | |
5533 | */ | |
5534 | static | |
5535 | int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) | |
5536 | ||
5537 | { | |
5538 | int ret = 0; | |
5539 | ||
5540 | DBG("Clearing stream quiescent state for all ust apps"); | |
5541 | ||
5542 | rcu_read_lock(); | |
5543 | ||
5544 | switch (usess->buffer_type) { | |
5545 | case LTTNG_BUFFER_PER_UID: | |
5546 | { | |
5547 | struct lttng_ht_iter iter; | |
5548 | struct buffer_reg_uid *reg; | |
5549 | ||
5550 | /* | |
5551 | * Clear quiescent for all per UID buffers associated to | |
5552 | * that session. | |
5553 | */ | |
5554 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
5555 | struct consumer_socket *socket; | |
3273699d | 5556 | struct buffer_reg_channel *buf_reg_chan; |
0dd01979 MD |
5557 | |
5558 | /* Get associated consumer socket.*/ | |
5559 | socket = consumer_find_socket_by_bitness( | |
5560 | reg->bits_per_long, usess->consumer); | |
5561 | if (!socket) { | |
5562 | /* | |
5563 | * Ignore request if no consumer is found for | |
5564 | * the session. | |
5565 | */ | |
5566 | continue; | |
5567 | } | |
5568 | ||
5569 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
3273699d | 5570 | &iter.iter, buf_reg_chan, node.node) { |
0dd01979 MD |
5571 | /* |
5572 | * The following call will print error values so | |
5573 | * the return code is of little importance | |
5574 | * because whatever happens, we have to try them | |
5575 | * all. | |
5576 | */ | |
5577 | (void) consumer_clear_quiescent_channel(socket, | |
3273699d | 5578 | buf_reg_chan->consumer_key); |
0dd01979 MD |
5579 | } |
5580 | } | |
5581 | break; | |
5582 | } | |
5583 | case LTTNG_BUFFER_PER_PID: | |
5584 | { | |
5585 | struct ust_app_session *ua_sess; | |
5586 | struct lttng_ht_iter iter; | |
5587 | struct ust_app *app; | |
5588 | ||
5589 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, | |
5590 | pid_n.node) { | |
5591 | ua_sess = lookup_session_by_app(usess, app); | |
5592 | if (ua_sess == NULL) { | |
5593 | continue; | |
5594 | } | |
5595 | (void) ust_app_clear_quiescent_app_session(app, | |
5596 | ua_sess); | |
5597 | } | |
5598 | break; | |
5599 | } | |
5600 | default: | |
5601 | ret = -1; | |
a0377dfe | 5602 | abort(); |
0dd01979 MD |
5603 | break; |
5604 | } | |
5605 | ||
5606 | rcu_read_unlock(); | |
5607 | health_code_update(); | |
5608 | return ret; | |
5609 | } | |
5610 | ||
84cd17c6 MD |
5611 | /* |
5612 | * Destroy a specific UST session in apps. | |
5613 | */ | |
3353de95 | 5614 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 | 5615 | { |
ffe60014 | 5616 | int ret; |
84cd17c6 | 5617 | struct ust_app_session *ua_sess; |
bec39940 | 5618 | struct lttng_ht_iter iter; |
d9bf3ca4 | 5619 | struct lttng_ht_node_u64 *node; |
84cd17c6 | 5620 | |
852d0037 | 5621 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
5622 | |
5623 | rcu_read_lock(); | |
5624 | ||
e0c7ec2b DG |
5625 | if (!app->compatible) { |
5626 | goto end; | |
5627 | } | |
5628 | ||
84cd17c6 | 5629 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 5630 | node = lttng_ht_iter_get_node_u64(&iter); |
84cd17c6 | 5631 | if (node == NULL) { |
d42f20df DG |
5632 | /* Session is being or is deleted. */ |
5633 | goto end; | |
84cd17c6 | 5634 | } |
0114db0e | 5635 | ua_sess = lttng::utils::container_of(node, &ust_app_session::node); |
c4a1715b | 5636 | |
840cb59c | 5637 | health_code_update(); |
d0b96690 | 5638 | destroy_app_session(app, ua_sess); |
84cd17c6 | 5639 | |
840cb59c | 5640 | health_code_update(); |
7db205b5 | 5641 | |
84cd17c6 | 5642 | /* Quiescent wait after stopping trace */ |
fb45065e | 5643 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5644 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5645 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5646 | if (ret < 0) { |
5647 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
9324443a | 5648 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
be355079 JR |
5649 | app->pid, app->sock); |
5650 | } else if (ret == -EAGAIN) { | |
9324443a | 5651 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
be355079 JR |
5652 | app->pid, app->sock); |
5653 | } else { | |
9324443a | 5654 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
be355079 JR |
5655 | ret, app->pid, app->sock); |
5656 | } | |
ffe60014 | 5657 | } |
e0c7ec2b DG |
5658 | end: |
5659 | rcu_read_unlock(); | |
840cb59c | 5660 | health_code_update(); |
84cd17c6 | 5661 | return 0; |
84cd17c6 MD |
5662 | } |
5663 | ||
5b4a0ec0 DG |
5664 | /* |
5665 | * Start tracing for the UST session. | |
5666 | */ | |
421cb601 DG |
5667 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
5668 | { | |
bec39940 | 5669 | struct lttng_ht_iter iter; |
421cb601 | 5670 | struct ust_app *app; |
48842b30 | 5671 | |
421cb601 DG |
5672 | DBG("Starting all UST traces"); |
5673 | ||
bb2452c8 MD |
5674 | /* |
5675 | * Even though the start trace might fail, flag this session active so | |
5676 | * other application coming in are started by default. | |
5677 | */ | |
5678 | usess->active = 1; | |
5679 | ||
421cb601 | 5680 | rcu_read_lock(); |
421cb601 | 5681 | |
0dd01979 MD |
5682 | /* |
5683 | * In a start-stop-start use-case, we need to clear the quiescent state | |
5684 | * of each channel set by the prior stop command, thus ensuring that a | |
5685 | * following stop or destroy is sure to grab a timestamp_end near those | |
5686 | * operations, even if the packet is empty. | |
5687 | */ | |
5688 | (void) ust_app_clear_quiescent_session(usess); | |
5689 | ||
0498a00c MD |
5690 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5691 | ust_app_global_update(usess, app); | |
5692 | } | |
5693 | ||
48842b30 DG |
5694 | rcu_read_unlock(); |
5695 | ||
5696 | return 0; | |
5697 | } | |
487cf67c | 5698 | |
8be98f9a MD |
5699 | /* |
5700 | * Start tracing for the UST session. | |
ce34fcd0 | 5701 | * Called with UST session lock held. |
8be98f9a MD |
5702 | */ |
5703 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
5704 | { | |
5705 | int ret = 0; | |
bec39940 | 5706 | struct lttng_ht_iter iter; |
8be98f9a MD |
5707 | struct ust_app *app; |
5708 | ||
5709 | DBG("Stopping all UST traces"); | |
5710 | ||
bb2452c8 MD |
5711 | /* |
5712 | * Even though the stop trace might fail, flag this session inactive so | |
5713 | * other application coming in are not started by default. | |
5714 | */ | |
5715 | usess->active = 0; | |
5716 | ||
8be98f9a MD |
5717 | rcu_read_lock(); |
5718 | ||
b34cbebf MD |
5719 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5720 | ret = ust_app_stop_trace(usess, app); | |
5721 | if (ret < 0) { | |
5722 | /* Continue to next apps even on error */ | |
5723 | continue; | |
5724 | } | |
5725 | } | |
5726 | ||
ce34fcd0 | 5727 | (void) ust_app_flush_session(usess); |
8be98f9a MD |
5728 | |
5729 | rcu_read_unlock(); | |
5730 | ||
5731 | return 0; | |
5732 | } | |
5733 | ||
84cd17c6 MD |
5734 | /* |
5735 | * Destroy app UST session. | |
5736 | */ | |
5737 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
5738 | { | |
5739 | int ret = 0; | |
bec39940 | 5740 | struct lttng_ht_iter iter; |
84cd17c6 MD |
5741 | struct ust_app *app; |
5742 | ||
5743 | DBG("Destroy all UST traces"); | |
5744 | ||
5745 | rcu_read_lock(); | |
5746 | ||
852d0037 | 5747 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 5748 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
5749 | if (ret < 0) { |
5750 | /* Continue to next apps even on error */ | |
5751 | continue; | |
5752 | } | |
5753 | } | |
5754 | ||
5755 | rcu_read_unlock(); | |
5756 | ||
5757 | return 0; | |
5758 | } | |
5759 | ||
88e3c2f5 | 5760 | /* The ua_sess lock must be held by the caller. */ |
a9ad0c8f | 5761 | static |
88e3c2f5 JG |
5762 | int find_or_create_ust_app_channel( |
5763 | struct ltt_ust_session *usess, | |
5764 | struct ust_app_session *ua_sess, | |
5765 | struct ust_app *app, | |
5766 | struct ltt_ust_channel *uchan, | |
5767 | struct ust_app_channel **ua_chan) | |
487cf67c | 5768 | { |
55c54cce | 5769 | int ret = 0; |
88e3c2f5 JG |
5770 | struct lttng_ht_iter iter; |
5771 | struct lttng_ht_node_str *ua_chan_node; | |
5772 | ||
5773 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); | |
5774 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
5775 | if (ua_chan_node) { | |
5776 | *ua_chan = caa_container_of(ua_chan_node, | |
5777 | struct ust_app_channel, node); | |
5778 | goto end; | |
5779 | } | |
5780 | ||
5781 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); | |
5782 | if (ret) { | |
5783 | goto end; | |
5784 | } | |
5785 | end: | |
5786 | return ret; | |
5787 | } | |
5788 | ||
5789 | static | |
5790 | int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, | |
f46376a1 | 5791 | struct ltt_ust_event *uevent, |
88e3c2f5 JG |
5792 | struct ust_app *app) |
5793 | { | |
5794 | int ret = 0; | |
5795 | struct ust_app_event *ua_event = NULL; | |
5796 | ||
5797 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
5798 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); | |
5799 | if (!ua_event) { | |
f46376a1 | 5800 | ret = create_ust_app_event(ua_chan, uevent, app); |
88e3c2f5 JG |
5801 | if (ret < 0) { |
5802 | goto end; | |
5803 | } | |
5804 | } else { | |
5805 | if (ua_event->enabled != uevent->enabled) { | |
5806 | ret = uevent->enabled ? | |
f46376a1 MJ |
5807 | enable_ust_app_event(ua_event, app) : |
5808 | disable_ust_app_event(ua_event, app); | |
88e3c2f5 JG |
5809 | } |
5810 | } | |
5811 | ||
5812 | end: | |
5813 | return ret; | |
5814 | } | |
5815 | ||
993578ff JR |
5816 | /* Called with RCU read-side lock held. */ |
5817 | static | |
5818 | void ust_app_synchronize_event_notifier_rules(struct ust_app *app) | |
5819 | { | |
5820 | int ret = 0; | |
5821 | enum lttng_error_code ret_code; | |
5822 | enum lttng_trigger_status t_status; | |
5823 | struct lttng_ht_iter app_trigger_iter; | |
5824 | struct lttng_triggers *triggers = NULL; | |
5825 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5826 | unsigned int count, i; | |
5827 | ||
48b7cdc2 FD |
5828 | ASSERT_RCU_READ_LOCKED(); |
5829 | ||
783db316 MD |
5830 | if (!ust_app_supports_notifiers(app)) { |
5831 | goto end; | |
5832 | } | |
5833 | ||
993578ff JR |
5834 | /* |
5835 | * Currrently, registering or unregistering a trigger with an | |
5836 | * event rule condition causes a full synchronization of the event | |
5837 | * notifiers. | |
5838 | * | |
5839 | * The first step attempts to add an event notifier for all registered | |
5840 | * triggers that apply to the user space tracers. Then, the | |
5841 | * application's event notifiers rules are all checked against the list | |
5842 | * of registered triggers. Any event notifier that doesn't have a | |
5843 | * matching trigger can be assumed to have been disabled. | |
5844 | * | |
5845 | * All of this is inefficient, but is put in place to get the feature | |
5846 | * rolling as it is simpler at this moment. It will be optimized Soon™ | |
5847 | * to allow the state of enabled | |
5848 | * event notifiers to be synchronized in a piece-wise way. | |
5849 | */ | |
5850 | ||
5851 | /* Get all triggers using uid 0 (root) */ | |
5852 | ret_code = notification_thread_command_list_triggers( | |
412d7227 | 5853 | the_notification_thread_handle, 0, &triggers); |
993578ff | 5854 | if (ret_code != LTTNG_OK) { |
993578ff JR |
5855 | goto end; |
5856 | } | |
5857 | ||
a0377dfe | 5858 | LTTNG_ASSERT(triggers); |
993578ff JR |
5859 | |
5860 | t_status = lttng_triggers_get_count(triggers, &count); | |
5861 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
993578ff JR |
5862 | goto end; |
5863 | } | |
5864 | ||
5865 | for (i = 0; i < count; i++) { | |
5866 | struct lttng_condition *condition; | |
5867 | struct lttng_event_rule *event_rule; | |
5868 | struct lttng_trigger *trigger; | |
5869 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; | |
5870 | enum lttng_condition_status condition_status; | |
5871 | uint64_t token; | |
5872 | ||
5873 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); | |
a0377dfe | 5874 | LTTNG_ASSERT(trigger); |
993578ff JR |
5875 | |
5876 | token = lttng_trigger_get_tracer_token(trigger); | |
5877 | condition = lttng_trigger_get_condition(trigger); | |
5878 | ||
8dbb86b8 JR |
5879 | if (lttng_condition_get_type(condition) != |
5880 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) { | |
993578ff JR |
5881 | /* Does not apply */ |
5882 | continue; | |
5883 | } | |
5884 | ||
8dbb86b8 JR |
5885 | condition_status = |
5886 | lttng_condition_event_rule_matches_borrow_rule_mutable( | |
5887 | condition, &event_rule); | |
a0377dfe | 5888 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
993578ff JR |
5889 | |
5890 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { | |
5891 | /* Skip kernel related triggers. */ | |
5892 | continue; | |
5893 | } | |
5894 | ||
5895 | /* | |
5896 | * Find or create the associated token event rule. The caller | |
5897 | * holds the RCU read lock, so this is safe to call without | |
5898 | * explicitly acquiring it here. | |
5899 | */ | |
5900 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( | |
5901 | app->token_to_event_notifier_rule_ht, token); | |
5902 | if (!looked_up_event_notifier_rule) { | |
267d66aa | 5903 | ret = create_ust_app_event_notifier_rule(trigger, app); |
993578ff JR |
5904 | if (ret < 0) { |
5905 | goto end; | |
5906 | } | |
5907 | } | |
5908 | } | |
5909 | ||
5910 | rcu_read_lock(); | |
5911 | /* Remove all unknown event sources from the app. */ | |
5912 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
5913 | &app_trigger_iter.iter, event_notifier_rule, | |
5914 | node.node) { | |
5915 | const uint64_t app_token = event_notifier_rule->token; | |
5916 | bool found = false; | |
5917 | ||
5918 | /* | |
5919 | * Check if the app event trigger still exists on the | |
5920 | * notification side. | |
5921 | */ | |
5922 | for (i = 0; i < count; i++) { | |
5923 | uint64_t notification_thread_token; | |
5924 | const struct lttng_trigger *trigger = | |
5925 | lttng_triggers_get_at_index( | |
5926 | triggers, i); | |
5927 | ||
a0377dfe | 5928 | LTTNG_ASSERT(trigger); |
993578ff JR |
5929 | |
5930 | notification_thread_token = | |
5931 | lttng_trigger_get_tracer_token(trigger); | |
5932 | ||
5933 | if (notification_thread_token == app_token) { | |
5934 | found = true; | |
5935 | break; | |
5936 | } | |
5937 | } | |
5938 | ||
5939 | if (found) { | |
5940 | /* Still valid. */ | |
5941 | continue; | |
5942 | } | |
5943 | ||
5944 | /* | |
5945 | * This trigger was unregistered, disable it on the tracer's | |
5946 | * side. | |
5947 | */ | |
5948 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, | |
5949 | &app_trigger_iter); | |
a0377dfe | 5950 | LTTNG_ASSERT(ret == 0); |
993578ff JR |
5951 | |
5952 | /* Callee logs errors. */ | |
5953 | (void) disable_ust_object(app, event_notifier_rule->obj); | |
5954 | ||
5955 | delete_ust_app_event_notifier_rule( | |
5956 | app->sock, event_notifier_rule, app); | |
5957 | } | |
5958 | ||
5959 | rcu_read_unlock(); | |
5960 | ||
5961 | end: | |
5962 | lttng_triggers_destroy(triggers); | |
5963 | return; | |
5964 | } | |
5965 | ||
88e3c2f5 | 5966 | /* |
a84d1024 | 5967 | * RCU read lock must be held by the caller. |
88e3c2f5 JG |
5968 | */ |
5969 | static | |
a84d1024 FD |
5970 | void ust_app_synchronize_all_channels(struct ltt_ust_session *usess, |
5971 | struct ust_app_session *ua_sess, | |
88e3c2f5 JG |
5972 | struct ust_app *app) |
5973 | { | |
5974 | int ret = 0; | |
5975 | struct cds_lfht_iter uchan_iter; | |
5976 | struct ltt_ust_channel *uchan; | |
1f3580c7 | 5977 | |
a0377dfe FD |
5978 | LTTNG_ASSERT(usess); |
5979 | LTTNG_ASSERT(ua_sess); | |
5980 | LTTNG_ASSERT(app); | |
48b7cdc2 | 5981 | ASSERT_RCU_READ_LOCKED(); |
ef67c072 | 5982 | |
88e3c2f5 JG |
5983 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, |
5984 | uchan, node.node) { | |
5985 | struct ust_app_channel *ua_chan; | |
5986 | struct cds_lfht_iter uevent_iter; | |
5987 | struct ltt_ust_event *uevent; | |
487cf67c | 5988 | |
31746f93 | 5989 | /* |
88e3c2f5 JG |
5990 | * Search for a matching ust_app_channel. If none is found, |
5991 | * create it. Creating the channel will cause the ua_chan | |
5992 | * structure to be allocated, the channel buffers to be | |
5993 | * allocated (if necessary) and sent to the application, and | |
5994 | * all enabled contexts will be added to the channel. | |
31746f93 | 5995 | */ |
f3db82be | 5996 | ret = find_or_create_ust_app_channel(usess, ua_sess, |
88e3c2f5 JG |
5997 | app, uchan, &ua_chan); |
5998 | if (ret) { | |
5999 | /* Tracer is probably gone or ENOMEM. */ | |
a84d1024 | 6000 | goto end; |
727d5404 DG |
6001 | } |
6002 | ||
88e3c2f5 JG |
6003 | if (!ua_chan) { |
6004 | /* ua_chan will be NULL for the metadata channel */ | |
6005 | continue; | |
6006 | } | |
727d5404 | 6007 | |
88e3c2f5 | 6008 | cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent, |
bec39940 | 6009 | node.node) { |
88e3c2f5 | 6010 | ret = ust_app_channel_synchronize_event(ua_chan, |
f46376a1 | 6011 | uevent, app); |
88e3c2f5 | 6012 | if (ret) { |
a84d1024 | 6013 | goto end; |
487cf67c | 6014 | } |
36dc12cc | 6015 | } |
d0b96690 | 6016 | |
88e3c2f5 JG |
6017 | if (ua_chan->enabled != uchan->enabled) { |
6018 | ret = uchan->enabled ? | |
6019 | enable_ust_app_channel(ua_sess, uchan, app) : | |
6020 | disable_ust_app_channel(ua_sess, ua_chan, app); | |
6021 | if (ret) { | |
a84d1024 | 6022 | goto end; |
88e3c2f5 JG |
6023 | } |
6024 | } | |
36dc12cc | 6025 | } |
a84d1024 FD |
6026 | end: |
6027 | return; | |
6028 | } | |
6029 | ||
6030 | /* | |
6031 | * The caller must ensure that the application is compatible and is tracked | |
6032 | * by the process attribute trackers. | |
6033 | */ | |
6034 | static | |
6035 | void ust_app_synchronize(struct ltt_ust_session *usess, | |
6036 | struct ust_app *app) | |
6037 | { | |
6038 | int ret = 0; | |
6039 | struct ust_app_session *ua_sess = NULL; | |
6040 | ||
6041 | /* | |
6042 | * The application's configuration should only be synchronized for | |
6043 | * active sessions. | |
6044 | */ | |
a0377dfe | 6045 | LTTNG_ASSERT(usess->active); |
a84d1024 FD |
6046 | |
6047 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL); | |
6048 | if (ret < 0) { | |
6049 | /* Tracer is probably gone or ENOMEM. */ | |
50f71cad FD |
6050 | if (ua_sess) { |
6051 | destroy_app_session(app, ua_sess); | |
6052 | } | |
6053 | goto end; | |
a84d1024 | 6054 | } |
a0377dfe | 6055 | LTTNG_ASSERT(ua_sess); |
a84d1024 FD |
6056 | |
6057 | pthread_mutex_lock(&ua_sess->lock); | |
6058 | if (ua_sess->deleted) { | |
50f71cad | 6059 | goto deleted_session; |
a84d1024 FD |
6060 | } |
6061 | ||
6062 | rcu_read_lock(); | |
6063 | ||
6064 | ust_app_synchronize_all_channels(usess, ua_sess, app); | |
ef67c072 JG |
6065 | |
6066 | /* | |
6067 | * Create the metadata for the application. This returns gracefully if a | |
6068 | * metadata was already set for the session. | |
6069 | * | |
6070 | * The metadata channel must be created after the data channels as the | |
6071 | * consumer daemon assumes this ordering. When interacting with a relay | |
6072 | * daemon, the consumer will use this assumption to send the | |
6073 | * "STREAMS_SENT" message to the relay daemon. | |
6074 | */ | |
6075 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); | |
6076 | if (ret < 0) { | |
50f71cad FD |
6077 | ERR("Metadata creation failed for app sock %d for session id %" PRIu64, |
6078 | app->sock, usess->id); | |
ef67c072 JG |
6079 | } |
6080 | ||
88e3c2f5 | 6081 | rcu_read_unlock(); |
0498a00c | 6082 | |
50f71cad | 6083 | deleted_session: |
d0b96690 | 6084 | pthread_mutex_unlock(&ua_sess->lock); |
50f71cad | 6085 | end: |
487cf67c DG |
6086 | return; |
6087 | } | |
55cc08a6 | 6088 | |
a9ad0c8f MD |
6089 | static |
6090 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) | |
6091 | { | |
6092 | struct ust_app_session *ua_sess; | |
6093 | ||
6094 | ua_sess = lookup_session_by_app(usess, app); | |
6095 | if (ua_sess == NULL) { | |
6096 | return; | |
6097 | } | |
6098 | destroy_app_session(app, ua_sess); | |
6099 | } | |
6100 | ||
6101 | /* | |
6102 | * Add channels/events from UST global domain to registered apps at sock. | |
6103 | * | |
6104 | * Called with session lock held. | |
6105 | * Called with RCU read-side lock held. | |
6106 | */ | |
6107 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) | |
6108 | { | |
a0377dfe FD |
6109 | LTTNG_ASSERT(usess); |
6110 | LTTNG_ASSERT(usess->active); | |
48b7cdc2 | 6111 | ASSERT_RCU_READ_LOCKED(); |
a9ad0c8f MD |
6112 | |
6113 | DBG2("UST app global update for app sock %d for session id %" PRIu64, | |
6114 | app->sock, usess->id); | |
6115 | ||
6116 | if (!app->compatible) { | |
6117 | return; | |
6118 | } | |
159b042f JG |
6119 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, |
6120 | usess, app->pid) && | |
55c9e7ca | 6121 | trace_ust_id_tracker_lookup( |
159b042f JG |
6122 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, |
6123 | usess, app->uid) && | |
55c9e7ca | 6124 | trace_ust_id_tracker_lookup( |
159b042f JG |
6125 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, |
6126 | usess, app->gid)) { | |
88e3c2f5 JG |
6127 | /* |
6128 | * Synchronize the application's internal tracing configuration | |
6129 | * and start tracing. | |
6130 | */ | |
6131 | ust_app_synchronize(usess, app); | |
6132 | ust_app_start_trace(usess, app); | |
a9ad0c8f MD |
6133 | } else { |
6134 | ust_app_global_destroy(usess, app); | |
6135 | } | |
6136 | } | |
6137 | ||
993578ff JR |
6138 | /* |
6139 | * Add all event notifiers to an application. | |
6140 | * | |
6141 | * Called with session lock held. | |
6142 | * Called with RCU read-side lock held. | |
6143 | */ | |
6144 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) | |
6145 | { | |
48b7cdc2 FD |
6146 | ASSERT_RCU_READ_LOCKED(); |
6147 | ||
9324443a | 6148 | DBG2("UST application global event notifier rules update: app = '%s', pid = %d", |
be355079 | 6149 | app->name, app->pid); |
993578ff | 6150 | |
783db316 | 6151 | if (!app->compatible || !ust_app_supports_notifiers(app)) { |
993578ff JR |
6152 | return; |
6153 | } | |
6154 | ||
6155 | if (app->event_notifier_group.object == NULL) { | |
9324443a | 6156 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d", |
be355079 | 6157 | app->name, app->pid); |
993578ff JR |
6158 | return; |
6159 | } | |
6160 | ||
6161 | ust_app_synchronize_event_notifier_rules(app); | |
6162 | } | |
6163 | ||
a9ad0c8f MD |
6164 | /* |
6165 | * Called with session lock held. | |
6166 | */ | |
6167 | void ust_app_global_update_all(struct ltt_ust_session *usess) | |
6168 | { | |
6169 | struct lttng_ht_iter iter; | |
6170 | struct ust_app *app; | |
6171 | ||
6172 | rcu_read_lock(); | |
6173 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6174 | ust_app_global_update(usess, app); | |
6175 | } | |
6176 | rcu_read_unlock(); | |
6177 | } | |
6178 | ||
993578ff JR |
6179 | void ust_app_global_update_all_event_notifier_rules(void) |
6180 | { | |
6181 | struct lttng_ht_iter iter; | |
6182 | struct ust_app *app; | |
6183 | ||
6184 | rcu_read_lock(); | |
6185 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6186 | ust_app_global_update_event_notifier_rules(app); | |
6187 | } | |
6188 | ||
6189 | rcu_read_unlock(); | |
6190 | } | |
6191 | ||
55cc08a6 DG |
6192 | /* |
6193 | * Add context to a specific channel for global UST domain. | |
6194 | */ | |
6195 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
6196 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
6197 | { | |
6198 | int ret = 0; | |
bec39940 DG |
6199 | struct lttng_ht_node_str *ua_chan_node; |
6200 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
6201 | struct ust_app_channel *ua_chan = NULL; |
6202 | struct ust_app_session *ua_sess; | |
6203 | struct ust_app *app; | |
6204 | ||
a0377dfe | 6205 | LTTNG_ASSERT(usess->active); |
0498a00c | 6206 | |
55cc08a6 | 6207 | rcu_read_lock(); |
852d0037 | 6208 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
6209 | if (!app->compatible) { |
6210 | /* | |
6211 | * TODO: In time, we should notice the caller of this error by | |
6212 | * telling him that this is a version error. | |
6213 | */ | |
6214 | continue; | |
6215 | } | |
55cc08a6 DG |
6216 | ua_sess = lookup_session_by_app(usess, app); |
6217 | if (ua_sess == NULL) { | |
6218 | continue; | |
6219 | } | |
6220 | ||
d0b96690 | 6221 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
6222 | |
6223 | if (ua_sess->deleted) { | |
6224 | pthread_mutex_unlock(&ua_sess->lock); | |
6225 | continue; | |
6226 | } | |
6227 | ||
55cc08a6 | 6228 | /* Lookup channel in the ust app session */ |
bec39940 DG |
6229 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
6230 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 | 6231 | if (ua_chan_node == NULL) { |
d0b96690 | 6232 | goto next_app; |
55cc08a6 DG |
6233 | } |
6234 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
6235 | node); | |
c9edf082 | 6236 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
55cc08a6 | 6237 | if (ret < 0) { |
d0b96690 | 6238 | goto next_app; |
55cc08a6 | 6239 | } |
d0b96690 DG |
6240 | next_app: |
6241 | pthread_mutex_unlock(&ua_sess->lock); | |
55cc08a6 DG |
6242 | } |
6243 | ||
55cc08a6 | 6244 | rcu_read_unlock(); |
76d45b40 DG |
6245 | return ret; |
6246 | } | |
7f79d3a1 | 6247 | |
d0b96690 DG |
6248 | /* |
6249 | * Receive registration and populate the given msg structure. | |
6250 | * | |
6251 | * On success return 0 else a negative value returned by the ustctl call. | |
6252 | */ | |
6253 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) | |
6254 | { | |
6255 | int ret; | |
6256 | uint32_t pid, ppid, uid, gid; | |
6257 | ||
a0377dfe | 6258 | LTTNG_ASSERT(msg); |
d0b96690 | 6259 | |
b623cb6a | 6260 | ret = lttng_ust_ctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, |
d0b96690 DG |
6261 | &pid, &ppid, &uid, &gid, |
6262 | &msg->bits_per_long, | |
6263 | &msg->uint8_t_alignment, | |
6264 | &msg->uint16_t_alignment, | |
6265 | &msg->uint32_t_alignment, | |
6266 | &msg->uint64_t_alignment, | |
6267 | &msg->long_alignment, | |
6268 | &msg->byte_order, | |
6269 | msg->name); | |
6270 | if (ret < 0) { | |
6271 | switch (-ret) { | |
6272 | case EPIPE: | |
6273 | case ECONNRESET: | |
6274 | case LTTNG_UST_ERR_EXITING: | |
6275 | DBG3("UST app recv reg message failed. Application died"); | |
6276 | break; | |
6277 | case LTTNG_UST_ERR_UNSUP_MAJOR: | |
6278 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", | |
6279 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, | |
6280 | LTTNG_UST_ABI_MINOR_VERSION); | |
6281 | break; | |
6282 | default: | |
6283 | ERR("UST app recv reg message failed with ret %d", ret); | |
6284 | break; | |
6285 | } | |
6286 | goto error; | |
6287 | } | |
6288 | msg->pid = (pid_t) pid; | |
6289 | msg->ppid = (pid_t) ppid; | |
6290 | msg->uid = (uid_t) uid; | |
6291 | msg->gid = (gid_t) gid; | |
6292 | ||
6293 | error: | |
6294 | return ret; | |
6295 | } | |
6296 | ||
10b56aef MD |
6297 | /* |
6298 | * Return a ust app session object using the application object and the | |
6299 | * session object descriptor has a key. If not found, NULL is returned. | |
6300 | * A RCU read side lock MUST be acquired when calling this function. | |
6301 | */ | |
6302 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, | |
6303 | int objd) | |
6304 | { | |
6305 | struct lttng_ht_node_ulong *node; | |
6306 | struct lttng_ht_iter iter; | |
6307 | struct ust_app_session *ua_sess = NULL; | |
6308 | ||
a0377dfe | 6309 | LTTNG_ASSERT(app); |
48b7cdc2 | 6310 | ASSERT_RCU_READ_LOCKED(); |
10b56aef MD |
6311 | |
6312 | lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter); | |
6313 | node = lttng_ht_iter_get_node_ulong(&iter); | |
6314 | if (node == NULL) { | |
6315 | DBG2("UST app session find by objd %d not found", objd); | |
6316 | goto error; | |
6317 | } | |
6318 | ||
0114db0e | 6319 | ua_sess = lttng::utils::container_of(node, &ust_app_session::ust_objd_node); |
10b56aef MD |
6320 | |
6321 | error: | |
6322 | return ua_sess; | |
6323 | } | |
6324 | ||
d88aee68 DG |
6325 | /* |
6326 | * Return a ust app channel object using the application object and the channel | |
6327 | * object descriptor has a key. If not found, NULL is returned. A RCU read side | |
6328 | * lock MUST be acquired before calling this function. | |
6329 | */ | |
d0b96690 DG |
6330 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
6331 | int objd) | |
6332 | { | |
6333 | struct lttng_ht_node_ulong *node; | |
6334 | struct lttng_ht_iter iter; | |
6335 | struct ust_app_channel *ua_chan = NULL; | |
6336 | ||
a0377dfe | 6337 | LTTNG_ASSERT(app); |
48b7cdc2 | 6338 | ASSERT_RCU_READ_LOCKED(); |
d0b96690 DG |
6339 | |
6340 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); | |
6341 | node = lttng_ht_iter_get_node_ulong(&iter); | |
6342 | if (node == NULL) { | |
6343 | DBG2("UST app channel find by objd %d not found", objd); | |
6344 | goto error; | |
6345 | } | |
6346 | ||
0114db0e | 6347 | ua_chan = lttng::utils::container_of(node, &ust_app_channel::ust_objd_node); |
d0b96690 DG |
6348 | |
6349 | error: | |
6350 | return ua_chan; | |
6351 | } | |
6352 | ||
d88aee68 DG |
6353 | /* |
6354 | * Reply to a register channel notification from an application on the notify | |
6355 | * socket. The channel metadata is also created. | |
6356 | * | |
6357 | * The session UST registry lock is acquired in this function. | |
6358 | * | |
6359 | * On success 0 is returned else a negative value. | |
6360 | */ | |
d7bfb9b0 JG |
6361 | static int handle_app_register_channel_notification(int sock, |
6362 | int cobjd, | |
6363 | struct lttng_ust_ctl_field *raw_context_fields, | |
6364 | size_t context_field_count) | |
d0b96690 DG |
6365 | { |
6366 | int ret, ret_code = 0; | |
294e218e | 6367 | uint32_t chan_id; |
7972aab2 | 6368 | uint64_t chan_reg_key; |
d0b96690 DG |
6369 | struct ust_app *app; |
6370 | struct ust_app_channel *ua_chan; | |
6371 | struct ust_app_session *ua_sess; | |
d7bfb9b0 JG |
6372 | auto ust_ctl_context_fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>( |
6373 | raw_context_fields); | |
d0b96690 | 6374 | |
d7bfb9b0 | 6375 | lttng::urcu::read_lock_guard read_lock_guard; |
d0b96690 DG |
6376 | |
6377 | /* Lookup application. If not found, there is a code flow error. */ | |
6378 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6379 | if (!app) { |
fad1ed2f | 6380 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 | 6381 | sock); |
d7bfb9b0 | 6382 | return -1; |
d88aee68 | 6383 | } |
d0b96690 | 6384 | |
4950b860 | 6385 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6386 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6387 | if (!ua_chan) { |
fad1ed2f | 6388 | DBG("Application channel is being torn down. Abort event notify"); |
d7bfb9b0 | 6389 | return 0; |
4950b860 MD |
6390 | } |
6391 | ||
a0377dfe | 6392 | LTTNG_ASSERT(ua_chan->session); |
d0b96690 | 6393 | ua_sess = ua_chan->session; |
d0b96690 | 6394 | |
7972aab2 | 6395 | /* Get right session registry depending on the session buffer type. */ |
d7bfb9b0 JG |
6396 | auto locked_registry_session = get_locked_session_registry(ua_sess); |
6397 | if (!locked_registry_session) { | |
fad1ed2f | 6398 | DBG("Application session is being torn down. Abort event notify"); |
d7bfb9b0 | 6399 | return 0; |
fad1ed2f | 6400 | }; |
45893984 | 6401 | |
7972aab2 DG |
6402 | /* Depending on the buffer type, a different channel key is used. */ |
6403 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
6404 | chan_reg_key = ua_chan->tracing_channel_id; | |
d0b96690 | 6405 | } else { |
7972aab2 | 6406 | chan_reg_key = ua_chan->key; |
d0b96690 DG |
6407 | } |
6408 | ||
d7bfb9b0 | 6409 | auto& ust_reg_chan = locked_registry_session->get_channel(chan_reg_key); |
7972aab2 | 6410 | |
fb277293 | 6411 | /* Channel id is set during the object creation. */ |
d7bfb9b0 | 6412 | chan_id = ust_reg_chan.id; |
fb277293 | 6413 | |
d7bfb9b0 JG |
6414 | /* |
6415 | * The application returns the typing information of the channel's | |
6416 | * context fields. In per-PID buffering mode, this is the first and only | |
6417 | * time we get this information. It is our chance to finalize the | |
6418 | * initialiation of the channel and serialize it's layout's description | |
6419 | * to the trace's metadata. | |
6420 | * | |
6421 | * However, in per-UID buffering mode, every application will provide | |
6422 | * this information (redundantly). The first time will allow us to | |
6423 | * complete the initialization. The following times, we simply validate | |
6424 | * that all apps provide the same typing for the context fields as a | |
6425 | * sanity check. | |
6426 | */ | |
058a2241 JG |
6427 | try { |
6428 | auto app_context_fields = lsu::create_trace_fields_from_ust_ctl_fields( | |
6429 | *locked_registry_session, ust_ctl_context_fields.get(), | |
d59532b4 JG |
6430 | context_field_count, |
6431 | lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT); | |
d7bfb9b0 | 6432 | |
058a2241 JG |
6433 | if (!ust_reg_chan.is_registered()) { |
6434 | lst::type::cuptr event_context = app_context_fields.size() ? | |
6435 | lttng::make_unique<lst::structure_type>( | |
6436 | 0, std::move(app_context_fields)) : | |
6437 | nullptr; | |
6438 | ||
6439 | ust_reg_chan.set_event_context(std::move(event_context)); | |
6440 | } else { | |
6441 | /* | |
6442 | * Validate that the context fields match between | |
6443 | * registry and newcoming application. | |
6444 | */ | |
6445 | bool context_fields_match; | |
6446 | const auto *previous_event_context = ust_reg_chan.get_event_context(); | |
6447 | ||
6448 | if (!previous_event_context) { | |
6449 | context_fields_match = app_context_fields.size() == 0; | |
6450 | } else { | |
6451 | const lst::structure_type app_event_context_struct( | |
6452 | 0, std::move(app_context_fields)); | |
6453 | ||
6454 | context_fields_match = *previous_event_context == | |
6455 | app_event_context_struct; | |
6456 | } | |
6457 | ||
6458 | if (!context_fields_match) { | |
6459 | ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d", | |
6460 | app->pid, app->sock); | |
6461 | ret_code = -EINVAL; | |
6462 | goto reply; | |
6463 | } | |
fb277293 | 6464 | } |
058a2241 JG |
6465 | } catch (std::exception& ex) { |
6466 | ERR("Failed to handle application context: %s", ex.what()); | |
6467 | ret_code = -EINVAL; | |
6468 | goto reply; | |
d0b96690 | 6469 | } |
d0b96690 | 6470 | |
d0b96690 | 6471 | reply: |
7972aab2 | 6472 | DBG3("UST app replying to register channel key %" PRIu64 |
d7bfb9b0 | 6473 | " with id %u, ret = %d", chan_reg_key, chan_id, |
7972aab2 | 6474 | ret_code); |
d0b96690 | 6475 | |
d7bfb9b0 | 6476 | ret = lttng_ust_ctl_reply_register_channel(sock, chan_id, |
65cd3c0c | 6477 | ust_reg_chan.header_type_ == lst::stream_class::header_type::COMPACT ? |
d7bfb9b0 JG |
6478 | LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT : |
6479 | LTTNG_UST_CTL_CHANNEL_HEADER_LARGE, | |
6480 | ret_code); | |
d0b96690 | 6481 | if (ret < 0) { |
be355079 JR |
6482 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6483 | DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d", | |
6484 | app->pid, app->sock); | |
6485 | } else if (ret == -EAGAIN) { | |
6486 | WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d", | |
6487 | app->pid, app->sock); | |
d0b96690 | 6488 | } else { |
be355079 JR |
6489 | ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d", |
6490 | ret, app->pid, app->sock); | |
d0b96690 | 6491 | } |
d7bfb9b0 JG |
6492 | |
6493 | return ret; | |
d0b96690 DG |
6494 | } |
6495 | ||
d7bfb9b0 JG |
6496 | /* This channel registry's registration is completed. */ |
6497 | ust_reg_chan.set_as_registered(); | |
7972aab2 | 6498 | |
d0b96690 DG |
6499 | return ret; |
6500 | } | |
6501 | ||
d88aee68 DG |
6502 | /* |
6503 | * Add event to the UST channel registry. When the event is added to the | |
6504 | * registry, the metadata is also created. Once done, this replies to the | |
6505 | * application with the appropriate error code. | |
6506 | * | |
6507 | * The session UST registry lock is acquired in the function. | |
6508 | * | |
6509 | * On success 0 is returned else a negative value. | |
6510 | */ | |
d7bfb9b0 JG |
6511 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, const char *name, |
6512 | char *raw_signature, size_t nr_fields, struct lttng_ust_ctl_field *raw_fields, | |
6513 | int loglevel_value, char *raw_model_emf_uri) | |
d0b96690 DG |
6514 | { |
6515 | int ret, ret_code; | |
6516 | uint32_t event_id = 0; | |
7972aab2 | 6517 | uint64_t chan_reg_key; |
d0b96690 DG |
6518 | struct ust_app *app; |
6519 | struct ust_app_channel *ua_chan; | |
6520 | struct ust_app_session *ua_sess; | |
d7bfb9b0 JG |
6521 | lttng::urcu::read_lock_guard rcu_lock; |
6522 | auto signature = lttng::make_unique_wrapper<char, lttng::free>(raw_signature); | |
6523 | auto fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_fields); | |
6524 | auto model_emf_uri = lttng::make_unique_wrapper<char, lttng::free>(raw_model_emf_uri); | |
d0b96690 DG |
6525 | |
6526 | /* Lookup application. If not found, there is a code flow error. */ | |
6527 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6528 | if (!app) { |
fad1ed2f | 6529 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 | 6530 | sock); |
d7bfb9b0 | 6531 | return -1; |
d88aee68 | 6532 | } |
d0b96690 | 6533 | |
4950b860 | 6534 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6535 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6536 | if (!ua_chan) { |
fad1ed2f | 6537 | DBG("Application channel is being torn down. Abort event notify"); |
d7bfb9b0 | 6538 | return 0; |
4950b860 MD |
6539 | } |
6540 | ||
a0377dfe | 6541 | LTTNG_ASSERT(ua_chan->session); |
d0b96690 DG |
6542 | ua_sess = ua_chan->session; |
6543 | ||
7972aab2 DG |
6544 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
6545 | chan_reg_key = ua_chan->tracing_channel_id; | |
6546 | } else { | |
6547 | chan_reg_key = ua_chan->key; | |
6548 | } | |
6549 | ||
d7bfb9b0 JG |
6550 | { |
6551 | auto locked_registry = get_locked_session_registry(ua_sess); | |
6552 | if (locked_registry) { | |
6553 | /* | |
6554 | * From this point on, this call acquires the ownership of the signature, | |
6555 | * fields and model_emf_uri meaning any free are done inside it if needed. | |
6556 | * These three variables MUST NOT be read/write after this. | |
6557 | */ | |
6558 | try { | |
6559 | auto& channel = locked_registry->get_channel(chan_reg_key); | |
6560 | ||
6561 | /* event_id is set on success. */ | |
6562 | channel.add_event(sobjd, cobjd, name, signature.get(), | |
6563 | lsu::create_trace_fields_from_ust_ctl_fields( | |
6564 | *locked_registry, fields.get(), | |
d59532b4 JG |
6565 | nr_fields, |
6566 | lst::field_location::root:: | |
6567 | EVENT_RECORD_PAYLOAD), | |
d7bfb9b0 JG |
6568 | loglevel_value, |
6569 | model_emf_uri.get() ? | |
6570 | nonstd::optional<std::string>( | |
6571 | model_emf_uri.get()) : | |
d59532b4 | 6572 | nonstd::nullopt, |
d7bfb9b0 JG |
6573 | ua_sess->buffer_type, *app, event_id); |
6574 | ret_code = 0; | |
6575 | } catch (const std::exception& ex) { | |
6576 | ERR("Failed to add event `%s` to registry session: %s", name, | |
6577 | ex.what()); | |
6578 | /* Inform the application of the error; don't return directly. */ | |
6579 | ret_code = -EINVAL; | |
6580 | } | |
6581 | } else { | |
6582 | DBG("Application session is being torn down. Abort event notify"); | |
6583 | return 0; | |
6584 | } | |
6585 | } | |
d0b96690 DG |
6586 | |
6587 | /* | |
6588 | * The return value is returned to ustctl so in case of an error, the | |
6589 | * application can be notified. In case of an error, it's important not to | |
6590 | * return a negative error or else the application will get closed. | |
6591 | */ | |
b623cb6a | 6592 | ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code); |
d0b96690 | 6593 | if (ret < 0) { |
be355079 JR |
6594 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6595 | DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.", | |
6596 | app->pid, app->sock); | |
6597 | } else if (ret == -EAGAIN) { | |
6598 | WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d", | |
6599 | app->pid, app->sock); | |
d0b96690 | 6600 | } else { |
be355079 JR |
6601 | ERR("UST app reply event failed with ret %d: pid = %d, sock = %d", |
6602 | ret, app->pid, app->sock); | |
d0b96690 DG |
6603 | } |
6604 | /* | |
6605 | * No need to wipe the create event since the application socket will | |
6606 | * get close on error hence cleaning up everything by itself. | |
6607 | */ | |
d7bfb9b0 | 6608 | return ret; |
d0b96690 DG |
6609 | } |
6610 | ||
7972aab2 DG |
6611 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
6612 | name, event_id); | |
d0b96690 DG |
6613 | return ret; |
6614 | } | |
6615 | ||
10b56aef MD |
6616 | /* |
6617 | * Add enum to the UST session registry. Once done, this replies to the | |
6618 | * application with the appropriate error code. | |
6619 | * | |
6620 | * The session UST registry lock is acquired within this function. | |
6621 | * | |
6622 | * On success 0 is returned else a negative value. | |
6623 | */ | |
97f630d4 JG |
6624 | static int add_enum_ust_registry(int sock, int sobjd, const char *name, |
6625 | struct lttng_ust_ctl_enum_entry *raw_entries, size_t nr_entries) | |
10b56aef | 6626 | { |
97f630d4 | 6627 | int ret = 0; |
10b56aef MD |
6628 | struct ust_app *app; |
6629 | struct ust_app_session *ua_sess; | |
10b56aef | 6630 | uint64_t enum_id = -1ULL; |
97f630d4 JG |
6631 | lttng::urcu::read_lock_guard read_lock_guard; |
6632 | auto entries = lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::free>( | |
6633 | raw_entries); | |
10b56aef MD |
6634 | |
6635 | /* Lookup application. If not found, there is a code flow error. */ | |
6636 | app = find_app_by_notify_sock(sock); | |
6637 | if (!app) { | |
6638 | /* Return an error since this is not an error */ | |
6639 | DBG("Application socket %d is being torn down. Aborting enum registration", | |
6640 | sock); | |
97f630d4 | 6641 | return -1; |
10b56aef MD |
6642 | } |
6643 | ||
6644 | /* Lookup session by UST object descriptor. */ | |
6645 | ua_sess = find_session_by_objd(app, sobjd); | |
6646 | if (!ua_sess) { | |
6647 | /* Return an error since this is not an error */ | |
acfb63a8 | 6648 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
97f630d4 | 6649 | return 0; |
10b56aef MD |
6650 | } |
6651 | ||
97f630d4 JG |
6652 | auto locked_registry = get_locked_session_registry(ua_sess); |
6653 | if (!locked_registry) { | |
acfb63a8 | 6654 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); |
97f630d4 | 6655 | return 0; |
fad1ed2f | 6656 | } |
10b56aef | 6657 | |
10b56aef MD |
6658 | /* |
6659 | * From this point on, the callee acquires the ownership of | |
6660 | * entries. The variable entries MUST NOT be read/written after | |
6661 | * call. | |
6662 | */ | |
97f630d4 JG |
6663 | int application_reply_code; |
6664 | try { | |
6665 | locked_registry->create_or_find_enum( | |
6666 | sobjd, name, entries.release(), nr_entries, &enum_id); | |
6667 | application_reply_code = 0; | |
6668 | } catch (const std::exception& ex) { | |
6669 | ERR("%s: %s", fmt::format("Failed to create or find enumeration provided by application: app = {}, enumeration name = {}", | |
6670 | *app, name).c_str(), ex.what()); | |
6671 | application_reply_code = -1; | |
6672 | } | |
10b56aef MD |
6673 | |
6674 | /* | |
6675 | * The return value is returned to ustctl so in case of an error, the | |
6676 | * application can be notified. In case of an error, it's important not to | |
6677 | * return a negative error or else the application will get closed. | |
6678 | */ | |
97f630d4 | 6679 | ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code); |
10b56aef | 6680 | if (ret < 0) { |
be355079 JR |
6681 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6682 | DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d", | |
6683 | app->pid, app->sock); | |
6684 | } else if (ret == -EAGAIN) { | |
6685 | WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d", | |
6686 | app->pid, app->sock); | |
10b56aef | 6687 | } else { |
be355079 JR |
6688 | ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d", |
6689 | ret, app->pid, app->sock); | |
10b56aef MD |
6690 | } |
6691 | /* | |
6692 | * No need to wipe the create enum since the application socket will | |
6693 | * get close on error hence cleaning up everything by itself. | |
6694 | */ | |
97f630d4 | 6695 | return ret; |
10b56aef MD |
6696 | } |
6697 | ||
6698 | DBG3("UST registry enum %s added successfully or already found", name); | |
97f630d4 | 6699 | return 0; |
10b56aef MD |
6700 | } |
6701 | ||
d88aee68 DG |
6702 | /* |
6703 | * Handle application notification through the given notify socket. | |
6704 | * | |
6705 | * Return 0 on success or else a negative value. | |
6706 | */ | |
d0b96690 DG |
6707 | int ust_app_recv_notify(int sock) |
6708 | { | |
6709 | int ret; | |
b623cb6a | 6710 | enum lttng_ust_ctl_notify_cmd cmd; |
d0b96690 DG |
6711 | |
6712 | DBG3("UST app receiving notify from sock %d", sock); | |
6713 | ||
b623cb6a | 6714 | ret = lttng_ust_ctl_recv_notify(sock, &cmd); |
d0b96690 | 6715 | if (ret < 0) { |
be355079 JR |
6716 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6717 | DBG3("UST app recv notify failed. Application died: sock = %d", | |
6718 | sock); | |
6719 | } else if (ret == -EAGAIN) { | |
6720 | WARN("UST app recv notify failed. Communication time out: sock = %d", | |
6721 | sock); | |
d0b96690 | 6722 | } else { |
be355079 JR |
6723 | ERR("UST app recv notify failed with ret %d: sock = %d", |
6724 | ret, sock); | |
d0b96690 DG |
6725 | } |
6726 | goto error; | |
6727 | } | |
6728 | ||
6729 | switch (cmd) { | |
b623cb6a | 6730 | case LTTNG_UST_CTL_NOTIFY_CMD_EVENT: |
d0b96690 | 6731 | { |
2106efa0 | 6732 | int sobjd, cobjd, loglevel_value; |
fc4b93fa | 6733 | char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri; |
d0b96690 | 6734 | size_t nr_fields; |
b623cb6a | 6735 | struct lttng_ust_ctl_field *fields; |
d0b96690 DG |
6736 | |
6737 | DBG2("UST app ustctl register event received"); | |
6738 | ||
d7bfb9b0 JG |
6739 | ret = lttng_ust_ctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel_value, |
6740 | &sig, &nr_fields, &fields, &model_emf_uri); | |
d0b96690 | 6741 | if (ret < 0) { |
be355079 JR |
6742 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6743 | DBG3("UST app recv event failed. Application died: sock = %d", | |
6744 | sock); | |
6745 | } else if (ret == -EAGAIN) { | |
6746 | WARN("UST app recv event failed. Communication time out: sock = %d", | |
6747 | sock); | |
d0b96690 | 6748 | } else { |
be355079 JR |
6749 | ERR("UST app recv event failed with ret %d: sock = %d", |
6750 | ret, sock); | |
d0b96690 DG |
6751 | } |
6752 | goto error; | |
6753 | } | |
6754 | ||
d7bfb9b0 JG |
6755 | { |
6756 | lttng::urcu::read_lock_guard rcu_lock; | |
6757 | const struct ust_app *app = find_app_by_notify_sock(sock); | |
6758 | if (!app) { | |
6759 | DBG("Application socket %d is being torn down. Abort event notify", sock); | |
6760 | ret = -1; | |
6761 | goto error; | |
6762 | } | |
6763 | } | |
6764 | ||
6765 | if ((!fields && nr_fields > 0) || (fields && nr_fields == 0)) { | |
6766 | ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu", | |
6767 | fields, nr_fields); | |
6768 | ret = -1; | |
6769 | free(fields); | |
6770 | goto error; | |
6771 | } | |
6772 | ||
d5d629b5 DG |
6773 | /* |
6774 | * Add event to the UST registry coming from the notify socket. This | |
6775 | * call will free if needed the sig, fields and model_emf_uri. This | |
6776 | * code path loses the ownsership of these variables and transfer them | |
6777 | * to the this function. | |
6778 | */ | |
d0b96690 | 6779 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
2106efa0 | 6780 | fields, loglevel_value, model_emf_uri); |
d0b96690 DG |
6781 | if (ret < 0) { |
6782 | goto error; | |
6783 | } | |
6784 | ||
6785 | break; | |
6786 | } | |
b623cb6a | 6787 | case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL: |
d0b96690 DG |
6788 | { |
6789 | int sobjd, cobjd; | |
d7bfb9b0 JG |
6790 | size_t field_count; |
6791 | struct lttng_ust_ctl_field *context_fields; | |
d0b96690 DG |
6792 | |
6793 | DBG2("UST app ustctl register channel received"); | |
6794 | ||
d7bfb9b0 JG |
6795 | ret = lttng_ust_ctl_recv_register_channel( |
6796 | sock, &sobjd, &cobjd, &field_count, &context_fields); | |
d0b96690 | 6797 | if (ret < 0) { |
be355079 JR |
6798 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6799 | DBG3("UST app recv channel failed. Application died: sock = %d", | |
6800 | sock); | |
6801 | } else if (ret == -EAGAIN) { | |
6802 | WARN("UST app recv channel failed. Communication time out: sock = %d", | |
6803 | sock); | |
d0b96690 | 6804 | } else { |
d7bfb9b0 JG |
6805 | ERR("UST app recv channel failed with ret %d: sock = %d", ret, |
6806 | sock); | |
d0b96690 DG |
6807 | } |
6808 | goto error; | |
6809 | } | |
6810 | ||
d5d629b5 DG |
6811 | /* |
6812 | * The fields ownership are transfered to this function call meaning | |
6813 | * that if needed it will be freed. After this, it's invalid to access | |
d7bfb9b0 | 6814 | * fields or clean them up. |
d5d629b5 | 6815 | */ |
d7bfb9b0 | 6816 | ret = handle_app_register_channel_notification(sock, cobjd, context_fields, field_count); |
d0b96690 DG |
6817 | if (ret < 0) { |
6818 | goto error; | |
6819 | } | |
6820 | ||
6821 | break; | |
6822 | } | |
b623cb6a | 6823 | case LTTNG_UST_CTL_NOTIFY_CMD_ENUM: |
10b56aef MD |
6824 | { |
6825 | int sobjd; | |
fc4b93fa | 6826 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
10b56aef | 6827 | size_t nr_entries; |
b623cb6a | 6828 | struct lttng_ust_ctl_enum_entry *entries; |
10b56aef MD |
6829 | |
6830 | DBG2("UST app ustctl register enum received"); | |
6831 | ||
b623cb6a | 6832 | ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name, |
10b56aef MD |
6833 | &entries, &nr_entries); |
6834 | if (ret < 0) { | |
be355079 JR |
6835 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6836 | DBG3("UST app recv enum failed. Application died: sock = %d", | |
6837 | sock); | |
6838 | } else if (ret == -EAGAIN) { | |
6839 | WARN("UST app recv enum failed. Communication time out: sock = %d", | |
6840 | sock); | |
10b56aef | 6841 | } else { |
be355079 JR |
6842 | ERR("UST app recv enum failed with ret %d: sock = %d", |
6843 | ret, sock); | |
10b56aef MD |
6844 | } |
6845 | goto error; | |
6846 | } | |
6847 | ||
d7bfb9b0 | 6848 | /* Callee assumes ownership of entries. */ |
10b56aef MD |
6849 | ret = add_enum_ust_registry(sock, sobjd, name, |
6850 | entries, nr_entries); | |
6851 | if (ret < 0) { | |
6852 | goto error; | |
6853 | } | |
6854 | ||
6855 | break; | |
6856 | } | |
d0b96690 DG |
6857 | default: |
6858 | /* Should NEVER happen. */ | |
a0377dfe | 6859 | abort(); |
d0b96690 DG |
6860 | } |
6861 | ||
6862 | error: | |
6863 | return ret; | |
6864 | } | |
d88aee68 DG |
6865 | |
6866 | /* | |
6867 | * Once the notify socket hangs up, this is called. First, it tries to find the | |
6868 | * corresponding application. On failure, the call_rcu to close the socket is | |
6869 | * executed. If an application is found, it tries to delete it from the notify | |
6870 | * socket hash table. Whathever the result, it proceeds to the call_rcu. | |
6871 | * | |
6872 | * Note that an object needs to be allocated here so on ENOMEM failure, the | |
6873 | * call RCU is not done but the rest of the cleanup is. | |
6874 | */ | |
6875 | void ust_app_notify_sock_unregister(int sock) | |
6876 | { | |
6877 | int err_enomem = 0; | |
6878 | struct lttng_ht_iter iter; | |
6879 | struct ust_app *app; | |
6880 | struct ust_app_notify_sock_obj *obj; | |
6881 | ||
a0377dfe | 6882 | LTTNG_ASSERT(sock >= 0); |
d88aee68 DG |
6883 | |
6884 | rcu_read_lock(); | |
6885 | ||
64803277 | 6886 | obj = zmalloc<ust_app_notify_sock_obj>(); |
d88aee68 DG |
6887 | if (!obj) { |
6888 | /* | |
6889 | * An ENOMEM is kind of uncool. If this strikes we continue the | |
6890 | * procedure but the call_rcu will not be called. In this case, we | |
6891 | * accept the fd leak rather than possibly creating an unsynchronized | |
6892 | * state between threads. | |
6893 | * | |
6894 | * TODO: The notify object should be created once the notify socket is | |
6895 | * registered and stored independantely from the ust app object. The | |
6896 | * tricky part is to synchronize the teardown of the application and | |
6897 | * this notify object. Let's keep that in mind so we can avoid this | |
6898 | * kind of shenanigans with ENOMEM in the teardown path. | |
6899 | */ | |
6900 | err_enomem = 1; | |
6901 | } else { | |
6902 | obj->fd = sock; | |
6903 | } | |
6904 | ||
6905 | DBG("UST app notify socket unregister %d", sock); | |
6906 | ||
6907 | /* | |
6908 | * Lookup application by notify socket. If this fails, this means that the | |
6909 | * hash table delete has already been done by the application | |
6910 | * unregistration process so we can safely close the notify socket in a | |
6911 | * call RCU. | |
6912 | */ | |
6913 | app = find_app_by_notify_sock(sock); | |
6914 | if (!app) { | |
6915 | goto close_socket; | |
6916 | } | |
6917 | ||
6918 | iter.iter.node = &app->notify_sock_n.node; | |
6919 | ||
6920 | /* | |
6921 | * Whatever happens here either we fail or succeed, in both cases we have | |
6922 | * to close the socket after a grace period to continue to the call RCU | |
6923 | * here. If the deletion is successful, the application is not visible | |
6924 | * anymore by other threads and is it fails it means that it was already | |
6925 | * deleted from the hash table so either way we just have to close the | |
6926 | * socket. | |
6927 | */ | |
6928 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
6929 | ||
6930 | close_socket: | |
6931 | rcu_read_unlock(); | |
6932 | ||
6933 | /* | |
6934 | * Close socket after a grace period to avoid for the socket to be reused | |
6935 | * before the application object is freed creating potential race between | |
6936 | * threads trying to add unique in the global hash table. | |
6937 | */ | |
6938 | if (!err_enomem) { | |
6939 | call_rcu(&obj->head, close_notify_sock_rcu); | |
6940 | } | |
6941 | } | |
f45e313d DG |
6942 | |
6943 | /* | |
6944 | * Destroy a ust app data structure and free its memory. | |
6945 | */ | |
6946 | void ust_app_destroy(struct ust_app *app) | |
6947 | { | |
6948 | if (!app) { | |
6949 | return; | |
6950 | } | |
6951 | ||
6952 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
6953 | } | |
6dc3064a DG |
6954 | |
6955 | /* | |
6956 | * Take a snapshot for a given UST session. The snapshot is sent to the given | |
6957 | * output. | |
6958 | * | |
9a654598 | 6959 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 6960 | */ |
fb9a95c4 JG |
6961 | enum lttng_error_code ust_app_snapshot_record( |
6962 | const struct ltt_ust_session *usess, | |
f46376a1 | 6963 | const struct consumer_output *output, |
d07ceecd | 6964 | uint64_t nb_packets_per_stream) |
6dc3064a DG |
6965 | { |
6966 | int ret = 0; | |
9a654598 | 6967 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
6968 | struct lttng_ht_iter iter; |
6969 | struct ust_app *app; | |
affce97e | 6970 | char *trace_path = NULL; |
6dc3064a | 6971 | |
a0377dfe FD |
6972 | LTTNG_ASSERT(usess); |
6973 | LTTNG_ASSERT(output); | |
6dc3064a DG |
6974 | |
6975 | rcu_read_lock(); | |
6976 | ||
8c924c7b MD |
6977 | switch (usess->buffer_type) { |
6978 | case LTTNG_BUFFER_PER_UID: | |
6979 | { | |
6980 | struct buffer_reg_uid *reg; | |
6dc3064a | 6981 | |
8c924c7b | 6982 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
3273699d | 6983 | struct buffer_reg_channel *buf_reg_chan; |
8c924c7b | 6984 | struct consumer_socket *socket; |
3b967712 | 6985 | char pathname[PATH_MAX]; |
5da88b0f | 6986 | size_t consumer_path_offset = 0; |
6dc3064a | 6987 | |
aeeb48c6 | 6988 | if (!reg->registry->reg.ust->_metadata_key) { |
2b269489 JR |
6989 | /* Skip since no metadata is present */ |
6990 | continue; | |
6991 | } | |
6992 | ||
8c924c7b MD |
6993 | /* Get consumer socket to use to push the metadata.*/ |
6994 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
6995 | usess->consumer); | |
6996 | if (!socket) { | |
9a654598 | 6997 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
6998 | goto error; |
6999 | } | |
6dc3064a | 7000 | |
8c924c7b MD |
7001 | memset(pathname, 0, sizeof(pathname)); |
7002 | ret = snprintf(pathname, sizeof(pathname), | |
bd666153 | 7003 | DEFAULT_UST_TRACE_UID_PATH, |
8c924c7b MD |
7004 | reg->uid, reg->bits_per_long); |
7005 | if (ret < 0) { | |
7006 | PERROR("snprintf snapshot path"); | |
9a654598 | 7007 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
7008 | goto error; |
7009 | } | |
affce97e JG |
7010 | /* Free path allowed on previous iteration. */ |
7011 | free(trace_path); | |
5da88b0f MD |
7012 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
7013 | &consumer_path_offset); | |
3b967712 MD |
7014 | if (!trace_path) { |
7015 | status = LTTNG_ERR_INVALID; | |
7016 | goto error; | |
7017 | } | |
f3db82be | 7018 | /* Add the UST default trace dir to path. */ |
8c924c7b | 7019 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
3273699d | 7020 | buf_reg_chan, node.node) { |
9a654598 | 7021 | status = consumer_snapshot_channel(socket, |
3273699d | 7022 | buf_reg_chan->consumer_key, |
f46376a1 | 7023 | output, 0, &trace_path[consumer_path_offset], |
d2956687 | 7024 | nb_packets_per_stream); |
9a654598 | 7025 | if (status != LTTNG_OK) { |
8c924c7b MD |
7026 | goto error; |
7027 | } | |
7028 | } | |
9a654598 | 7029 | status = consumer_snapshot_channel(socket, |
aeeb48c6 | 7030 | reg->registry->reg.ust->_metadata_key, output, 1, |
f46376a1 | 7031 | &trace_path[consumer_path_offset], 0); |
9a654598 | 7032 | if (status != LTTNG_OK) { |
8c924c7b MD |
7033 | goto error; |
7034 | } | |
af706bb7 | 7035 | } |
8c924c7b MD |
7036 | break; |
7037 | } | |
7038 | case LTTNG_BUFFER_PER_PID: | |
7039 | { | |
7040 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7041 | struct consumer_socket *socket; | |
7042 | struct lttng_ht_iter chan_iter; | |
7043 | struct ust_app_channel *ua_chan; | |
7044 | struct ust_app_session *ua_sess; | |
b0f2e8db | 7045 | lsu::registry_session *registry; |
3b967712 | 7046 | char pathname[PATH_MAX]; |
5da88b0f | 7047 | size_t consumer_path_offset = 0; |
8c924c7b MD |
7048 | |
7049 | ua_sess = lookup_session_by_app(usess, app); | |
7050 | if (!ua_sess) { | |
7051 | /* Session not associated with this app. */ | |
7052 | continue; | |
7053 | } | |
af706bb7 | 7054 | |
8c924c7b | 7055 | /* Get the right consumer socket for the application. */ |
d7bfb9b0 | 7056 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
348a81dc | 7057 | output); |
8c924c7b | 7058 | if (!socket) { |
9a654598 | 7059 | status = LTTNG_ERR_INVALID; |
5c786ded JD |
7060 | goto error; |
7061 | } | |
7062 | ||
8c924c7b MD |
7063 | /* Add the UST default trace dir to path. */ |
7064 | memset(pathname, 0, sizeof(pathname)); | |
bd666153 | 7065 | ret = snprintf(pathname, sizeof(pathname), "%s", |
8c924c7b | 7066 | ua_sess->path); |
6dc3064a | 7067 | if (ret < 0) { |
9a654598 | 7068 | status = LTTNG_ERR_INVALID; |
8c924c7b | 7069 | PERROR("snprintf snapshot path"); |
6dc3064a DG |
7070 | goto error; |
7071 | } | |
affce97e JG |
7072 | /* Free path allowed on previous iteration. */ |
7073 | free(trace_path); | |
5da88b0f MD |
7074 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
7075 | &consumer_path_offset); | |
3b967712 MD |
7076 | if (!trace_path) { |
7077 | status = LTTNG_ERR_INVALID; | |
7078 | goto error; | |
7079 | } | |
f3db82be | 7080 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
8c924c7b | 7081 | ua_chan, node.node) { |
9a654598 | 7082 | status = consumer_snapshot_channel(socket, |
470cc211 | 7083 | ua_chan->key, output, 0, |
f46376a1 | 7084 | &trace_path[consumer_path_offset], |
d2956687 | 7085 | nb_packets_per_stream); |
9a654598 JG |
7086 | switch (status) { |
7087 | case LTTNG_OK: | |
7088 | break; | |
7089 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
7090 | continue; | |
7091 | default: | |
8c924c7b MD |
7092 | goto error; |
7093 | } | |
7094 | } | |
7095 | ||
7096 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 7097 | if (!registry) { |
9bbfb88c MD |
7098 | DBG("Application session is being torn down. Skip application."); |
7099 | continue; | |
fad1ed2f | 7100 | } |
9a654598 | 7101 | status = consumer_snapshot_channel(socket, |
aeeb48c6 | 7102 | registry->_metadata_key, output, 1, |
f46376a1 | 7103 | &trace_path[consumer_path_offset], 0); |
9a654598 JG |
7104 | switch (status) { |
7105 | case LTTNG_OK: | |
7106 | break; | |
7107 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
7108 | continue; | |
7109 | default: | |
8c924c7b MD |
7110 | goto error; |
7111 | } | |
7112 | } | |
7113 | break; | |
7114 | } | |
7115 | default: | |
a0377dfe | 7116 | abort(); |
8c924c7b | 7117 | break; |
6dc3064a DG |
7118 | } |
7119 | ||
7120 | error: | |
affce97e | 7121 | free(trace_path); |
6dc3064a | 7122 | rcu_read_unlock(); |
9a654598 | 7123 | return status; |
6dc3064a | 7124 | } |
5c786ded JD |
7125 | |
7126 | /* | |
d07ceecd | 7127 | * Return the size taken by one more packet per stream. |
5c786ded | 7128 | */ |
fb9a95c4 JG |
7129 | uint64_t ust_app_get_size_one_more_packet_per_stream( |
7130 | const struct ltt_ust_session *usess, uint64_t cur_nr_packets) | |
5c786ded | 7131 | { |
d07ceecd | 7132 | uint64_t tot_size = 0; |
5c786ded JD |
7133 | struct ust_app *app; |
7134 | struct lttng_ht_iter iter; | |
7135 | ||
a0377dfe | 7136 | LTTNG_ASSERT(usess); |
5c786ded JD |
7137 | |
7138 | switch (usess->buffer_type) { | |
7139 | case LTTNG_BUFFER_PER_UID: | |
7140 | { | |
7141 | struct buffer_reg_uid *reg; | |
7142 | ||
7143 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7144 | struct buffer_reg_channel *buf_reg_chan; |
5c786ded | 7145 | |
b7064eaa | 7146 | rcu_read_lock(); |
5c786ded | 7147 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
3273699d FD |
7148 | buf_reg_chan, node.node) { |
7149 | if (cur_nr_packets >= buf_reg_chan->num_subbuf) { | |
d07ceecd MD |
7150 | /* |
7151 | * Don't take channel into account if we | |
7152 | * already grab all its packets. | |
7153 | */ | |
7154 | continue; | |
7155 | } | |
3273699d | 7156 | tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count; |
5c786ded | 7157 | } |
b7064eaa | 7158 | rcu_read_unlock(); |
5c786ded JD |
7159 | } |
7160 | break; | |
7161 | } | |
7162 | case LTTNG_BUFFER_PER_PID: | |
7163 | { | |
7164 | rcu_read_lock(); | |
7165 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7166 | struct ust_app_channel *ua_chan; | |
7167 | struct ust_app_session *ua_sess; | |
7168 | struct lttng_ht_iter chan_iter; | |
7169 | ||
7170 | ua_sess = lookup_session_by_app(usess, app); | |
7171 | if (!ua_sess) { | |
7172 | /* Session not associated with this app. */ | |
7173 | continue; | |
7174 | } | |
7175 | ||
7176 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7177 | ua_chan, node.node) { | |
d07ceecd MD |
7178 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
7179 | /* | |
7180 | * Don't take channel into account if we | |
7181 | * already grab all its packets. | |
7182 | */ | |
7183 | continue; | |
7184 | } | |
7185 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; | |
5c786ded JD |
7186 | } |
7187 | } | |
7188 | rcu_read_unlock(); | |
7189 | break; | |
7190 | } | |
7191 | default: | |
a0377dfe | 7192 | abort(); |
5c786ded JD |
7193 | break; |
7194 | } | |
7195 | ||
d07ceecd | 7196 | return tot_size; |
5c786ded | 7197 | } |
fb83fe64 JD |
7198 | |
7199 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, | |
7200 | struct cds_list_head *buffer_reg_uid_list, | |
7201 | struct consumer_output *consumer, uint64_t uchan_id, | |
7202 | int overwrite, uint64_t *discarded, uint64_t *lost) | |
7203 | { | |
7204 | int ret; | |
7205 | uint64_t consumer_chan_key; | |
7206 | ||
70dd8162 MD |
7207 | *discarded = 0; |
7208 | *lost = 0; | |
7209 | ||
fb83fe64 | 7210 | ret = buffer_reg_uid_consumer_channel_key( |
76604852 | 7211 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
fb83fe64 | 7212 | if (ret < 0) { |
70dd8162 MD |
7213 | /* Not found */ |
7214 | ret = 0; | |
fb83fe64 JD |
7215 | goto end; |
7216 | } | |
7217 | ||
7218 | if (overwrite) { | |
7219 | ret = consumer_get_lost_packets(ust_session_id, | |
7220 | consumer_chan_key, consumer, lost); | |
7221 | } else { | |
7222 | ret = consumer_get_discarded_events(ust_session_id, | |
7223 | consumer_chan_key, consumer, discarded); | |
7224 | } | |
7225 | ||
7226 | end: | |
7227 | return ret; | |
7228 | } | |
7229 | ||
7230 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, | |
7231 | struct ltt_ust_channel *uchan, | |
7232 | struct consumer_output *consumer, int overwrite, | |
7233 | uint64_t *discarded, uint64_t *lost) | |
7234 | { | |
7235 | int ret = 0; | |
7236 | struct lttng_ht_iter iter; | |
7237 | struct lttng_ht_node_str *ua_chan_node; | |
7238 | struct ust_app *app; | |
7239 | struct ust_app_session *ua_sess; | |
7240 | struct ust_app_channel *ua_chan; | |
7241 | ||
70dd8162 MD |
7242 | *discarded = 0; |
7243 | *lost = 0; | |
7244 | ||
fb83fe64 JD |
7245 | rcu_read_lock(); |
7246 | /* | |
70dd8162 MD |
7247 | * Iterate over every registered applications. Sum counters for |
7248 | * all applications containing requested session and channel. | |
fb83fe64 JD |
7249 | */ |
7250 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7251 | struct lttng_ht_iter uiter; | |
7252 | ||
7253 | ua_sess = lookup_session_by_app(usess, app); | |
7254 | if (ua_sess == NULL) { | |
7255 | continue; | |
7256 | } | |
7257 | ||
7258 | /* Get channel */ | |
ee022399 | 7259 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
fb83fe64 JD |
7260 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
7261 | /* If the session is found for the app, the channel must be there */ | |
a0377dfe | 7262 | LTTNG_ASSERT(ua_chan_node); |
fb83fe64 | 7263 | |
0114db0e | 7264 | ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node); |
fb83fe64 JD |
7265 | |
7266 | if (overwrite) { | |
70dd8162 MD |
7267 | uint64_t _lost; |
7268 | ||
fb83fe64 | 7269 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, |
70dd8162 MD |
7270 | consumer, &_lost); |
7271 | if (ret < 0) { | |
7272 | break; | |
7273 | } | |
7274 | (*lost) += _lost; | |
fb83fe64 | 7275 | } else { |
70dd8162 MD |
7276 | uint64_t _discarded; |
7277 | ||
fb83fe64 | 7278 | ret = consumer_get_discarded_events(usess->id, |
70dd8162 MD |
7279 | ua_chan->key, consumer, &_discarded); |
7280 | if (ret < 0) { | |
7281 | break; | |
7282 | } | |
7283 | (*discarded) += _discarded; | |
fb83fe64 | 7284 | } |
fb83fe64 JD |
7285 | } |
7286 | ||
fb83fe64 JD |
7287 | rcu_read_unlock(); |
7288 | return ret; | |
7289 | } | |
c2561365 JD |
7290 | |
7291 | static | |
7292 | int ust_app_regenerate_statedump(struct ltt_ust_session *usess, | |
7293 | struct ust_app *app) | |
7294 | { | |
7295 | int ret = 0; | |
7296 | struct ust_app_session *ua_sess; | |
7297 | ||
7298 | DBG("Regenerating the metadata for ust app pid %d", app->pid); | |
7299 | ||
7300 | rcu_read_lock(); | |
7301 | ||
7302 | ua_sess = lookup_session_by_app(usess, app); | |
7303 | if (ua_sess == NULL) { | |
7304 | /* The session is in teardown process. Ignore and continue. */ | |
7305 | goto end; | |
7306 | } | |
7307 | ||
7308 | pthread_mutex_lock(&ua_sess->lock); | |
7309 | ||
7310 | if (ua_sess->deleted) { | |
7311 | goto end_unlock; | |
7312 | } | |
7313 | ||
7314 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 7315 | ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle); |
c2561365 JD |
7316 | pthread_mutex_unlock(&app->sock_lock); |
7317 | ||
7318 | end_unlock: | |
7319 | pthread_mutex_unlock(&ua_sess->lock); | |
7320 | ||
7321 | end: | |
7322 | rcu_read_unlock(); | |
7323 | health_code_update(); | |
7324 | return ret; | |
7325 | } | |
7326 | ||
7327 | /* | |
7328 | * Regenerate the statedump for each app in the session. | |
7329 | */ | |
7330 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) | |
7331 | { | |
7332 | int ret = 0; | |
7333 | struct lttng_ht_iter iter; | |
7334 | struct ust_app *app; | |
7335 | ||
7336 | DBG("Regenerating the metadata for all UST apps"); | |
7337 | ||
7338 | rcu_read_lock(); | |
7339 | ||
7340 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7341 | if (!app->compatible) { | |
7342 | continue; | |
7343 | } | |
7344 | ||
7345 | ret = ust_app_regenerate_statedump(usess, app); | |
7346 | if (ret < 0) { | |
7347 | /* Continue to the next app even on error */ | |
7348 | continue; | |
7349 | } | |
7350 | } | |
7351 | ||
7352 | rcu_read_unlock(); | |
7353 | ||
7354 | return 0; | |
7355 | } | |
5c408ad8 JD |
7356 | |
7357 | /* | |
7358 | * Rotate all the channels of a session. | |
7359 | * | |
6f6d3b69 | 7360 | * Return LTTNG_OK on success or else an LTTng error code. |
5c408ad8 | 7361 | */ |
6f6d3b69 | 7362 | enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) |
5c408ad8 | 7363 | { |
6f6d3b69 MD |
7364 | int ret; |
7365 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
5c408ad8 JD |
7366 | struct lttng_ht_iter iter; |
7367 | struct ust_app *app; | |
7368 | struct ltt_ust_session *usess = session->ust_session; | |
5c408ad8 | 7369 | |
a0377dfe | 7370 | LTTNG_ASSERT(usess); |
5c408ad8 JD |
7371 | |
7372 | rcu_read_lock(); | |
7373 | ||
7374 | switch (usess->buffer_type) { | |
7375 | case LTTNG_BUFFER_PER_UID: | |
7376 | { | |
7377 | struct buffer_reg_uid *reg; | |
7378 | ||
7379 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7380 | struct buffer_reg_channel *buf_reg_chan; |
5c408ad8 JD |
7381 | struct consumer_socket *socket; |
7382 | ||
7383 | /* Get consumer socket to use to push the metadata.*/ | |
7384 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7385 | usess->consumer); | |
7386 | if (!socket) { | |
6f6d3b69 | 7387 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
7388 | goto error; |
7389 | } | |
7390 | ||
5c408ad8 JD |
7391 | /* Rotate the data channels. */ |
7392 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 7393 | buf_reg_chan, node.node) { |
5c408ad8 | 7394 | ret = consumer_rotate_channel(socket, |
3273699d | 7395 | buf_reg_chan->consumer_key, |
d2956687 JG |
7396 | usess->consumer, |
7397 | /* is_metadata_channel */ false); | |
5c408ad8 | 7398 | if (ret < 0) { |
6f6d3b69 | 7399 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
7400 | goto error; |
7401 | } | |
7402 | } | |
7403 | ||
db786d44 JR |
7404 | /* |
7405 | * The metadata channel might not be present. | |
7406 | * | |
7407 | * Consumer stream allocation can be done | |
7408 | * asynchronously and can fail on intermediary | |
7409 | * operations (i.e add context) and lead to data | |
7410 | * channels created with no metadata channel. | |
7411 | */ | |
aeeb48c6 | 7412 | if (!reg->registry->reg.ust->_metadata_key) { |
db786d44 JR |
7413 | /* Skip since no metadata is present. */ |
7414 | continue; | |
7415 | } | |
7416 | ||
d7bfb9b0 JG |
7417 | { |
7418 | auto locked_registry = reg->registry->reg.ust->lock(); | |
7419 | (void) push_metadata(locked_registry, usess->consumer); | |
7420 | } | |
5c408ad8 JD |
7421 | |
7422 | ret = consumer_rotate_channel(socket, | |
aeeb48c6 | 7423 | reg->registry->reg.ust->_metadata_key, |
d2956687 JG |
7424 | usess->consumer, |
7425 | /* is_metadata_channel */ true); | |
5c408ad8 | 7426 | if (ret < 0) { |
6f6d3b69 | 7427 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
7428 | goto error; |
7429 | } | |
5c408ad8 JD |
7430 | } |
7431 | break; | |
7432 | } | |
7433 | case LTTNG_BUFFER_PER_PID: | |
7434 | { | |
7435 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7436 | struct consumer_socket *socket; | |
7437 | struct lttng_ht_iter chan_iter; | |
7438 | struct ust_app_channel *ua_chan; | |
7439 | struct ust_app_session *ua_sess; | |
b0f2e8db | 7440 | lsu::registry_session *registry; |
5c408ad8 JD |
7441 | |
7442 | ua_sess = lookup_session_by_app(usess, app); | |
7443 | if (!ua_sess) { | |
7444 | /* Session not associated with this app. */ | |
7445 | continue; | |
7446 | } | |
5c408ad8 JD |
7447 | |
7448 | /* Get the right consumer socket for the application. */ | |
d7bfb9b0 | 7449 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
5c408ad8 JD |
7450 | usess->consumer); |
7451 | if (!socket) { | |
6f6d3b69 | 7452 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
7453 | goto error; |
7454 | } | |
7455 | ||
7456 | registry = get_session_registry(ua_sess); | |
7457 | if (!registry) { | |
6f6d3b69 MD |
7458 | DBG("Application session is being torn down. Skip application."); |
7459 | continue; | |
5c408ad8 JD |
7460 | } |
7461 | ||
5c408ad8 JD |
7462 | /* Rotate the data channels. */ |
7463 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7464 | ua_chan, node.node) { | |
470cc211 JG |
7465 | ret = consumer_rotate_channel(socket, |
7466 | ua_chan->key, | |
d2956687 JG |
7467 | ua_sess->consumer, |
7468 | /* is_metadata_channel */ false); | |
5c408ad8 | 7469 | if (ret < 0) { |
6f6d3b69 MD |
7470 | /* Per-PID buffer and application going away. */ |
7471 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7472 | continue; | |
7473 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7474 | goto error; |
7475 | } | |
7476 | } | |
7477 | ||
7478 | /* Rotate the metadata channel. */ | |
d7bfb9b0 JG |
7479 | { |
7480 | auto locked_registry = registry->lock(); | |
7481 | ||
7482 | (void) push_metadata(locked_registry, usess->consumer); | |
7483 | } | |
470cc211 | 7484 | ret = consumer_rotate_channel(socket, |
aeeb48c6 | 7485 | registry->_metadata_key, |
d2956687 JG |
7486 | ua_sess->consumer, |
7487 | /* is_metadata_channel */ true); | |
5c408ad8 | 7488 | if (ret < 0) { |
6f6d3b69 MD |
7489 | /* Per-PID buffer and application going away. */ |
7490 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7491 | continue; | |
7492 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7493 | goto error; |
7494 | } | |
5c408ad8 JD |
7495 | } |
7496 | break; | |
7497 | } | |
7498 | default: | |
a0377dfe | 7499 | abort(); |
5c408ad8 JD |
7500 | break; |
7501 | } | |
7502 | ||
6f6d3b69 | 7503 | cmd_ret = LTTNG_OK; |
5c408ad8 JD |
7504 | |
7505 | error: | |
7506 | rcu_read_unlock(); | |
6f6d3b69 | 7507 | return cmd_ret; |
5c408ad8 | 7508 | } |
d2956687 JG |
7509 | |
7510 | enum lttng_error_code ust_app_create_channel_subdirectories( | |
7511 | const struct ltt_ust_session *usess) | |
7512 | { | |
7513 | enum lttng_error_code ret = LTTNG_OK; | |
7514 | struct lttng_ht_iter iter; | |
7515 | enum lttng_trace_chunk_status chunk_status; | |
7516 | char *pathname_index; | |
7517 | int fmt_ret; | |
7518 | ||
a0377dfe | 7519 | LTTNG_ASSERT(usess->current_trace_chunk); |
d2956687 JG |
7520 | rcu_read_lock(); |
7521 | ||
7522 | switch (usess->buffer_type) { | |
7523 | case LTTNG_BUFFER_PER_UID: | |
7524 | { | |
7525 | struct buffer_reg_uid *reg; | |
7526 | ||
7527 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7528 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7529 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7530 | reg->uid, reg->bits_per_long); |
7531 | if (fmt_ret < 0) { | |
7532 | ERR("Failed to format channel index directory"); | |
7533 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7534 | goto error; | |
7535 | } | |
7536 | ||
7537 | /* | |
7538 | * Create the index subdirectory which will take care | |
7539 | * of implicitly creating the channel's path. | |
7540 | */ | |
7541 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7542 | usess->current_trace_chunk, | |
7543 | pathname_index); | |
7544 | free(pathname_index); | |
7545 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7546 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7547 | goto error; | |
7548 | } | |
7549 | } | |
7550 | break; | |
7551 | } | |
7552 | case LTTNG_BUFFER_PER_PID: | |
7553 | { | |
7554 | struct ust_app *app; | |
7555 | ||
495dece5 MD |
7556 | /* |
7557 | * Create the toplevel ust/ directory in case no apps are running. | |
7558 | */ | |
7559 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7560 | usess->current_trace_chunk, | |
7561 | DEFAULT_UST_TRACE_DIR); | |
7562 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7563 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7564 | goto error; | |
7565 | } | |
7566 | ||
d2956687 JG |
7567 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
7568 | pid_n.node) { | |
7569 | struct ust_app_session *ua_sess; | |
b0f2e8db | 7570 | lsu::registry_session *registry; |
d2956687 JG |
7571 | |
7572 | ua_sess = lookup_session_by_app(usess, app); | |
7573 | if (!ua_sess) { | |
7574 | /* Session not associated with this app. */ | |
7575 | continue; | |
7576 | } | |
7577 | ||
7578 | registry = get_session_registry(ua_sess); | |
7579 | if (!registry) { | |
7580 | DBG("Application session is being torn down. Skip application."); | |
7581 | continue; | |
7582 | } | |
7583 | ||
7584 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7585 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7586 | ua_sess->path); |
7587 | if (fmt_ret < 0) { | |
7588 | ERR("Failed to format channel index directory"); | |
7589 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7590 | goto error; | |
7591 | } | |
7592 | /* | |
7593 | * Create the index subdirectory which will take care | |
7594 | * of implicitly creating the channel's path. | |
7595 | */ | |
7596 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7597 | usess->current_trace_chunk, | |
7598 | pathname_index); | |
7599 | free(pathname_index); | |
7600 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7601 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7602 | goto error; | |
7603 | } | |
7604 | } | |
7605 | break; | |
7606 | } | |
7607 | default: | |
7608 | abort(); | |
7609 | } | |
7610 | ||
7611 | ret = LTTNG_OK; | |
7612 | error: | |
7613 | rcu_read_unlock(); | |
7614 | return ret; | |
7615 | } | |
4a9b9759 MD |
7616 | |
7617 | /* | |
7618 | * Clear all the channels of a session. | |
7619 | * | |
7620 | * Return LTTNG_OK on success or else an LTTng error code. | |
7621 | */ | |
7622 | enum lttng_error_code ust_app_clear_session(struct ltt_session *session) | |
7623 | { | |
7624 | int ret; | |
7625 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
7626 | struct lttng_ht_iter iter; | |
7627 | struct ust_app *app; | |
7628 | struct ltt_ust_session *usess = session->ust_session; | |
7629 | ||
a0377dfe | 7630 | LTTNG_ASSERT(usess); |
4a9b9759 MD |
7631 | |
7632 | rcu_read_lock(); | |
7633 | ||
7634 | if (usess->active) { | |
7635 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); | |
7636 | cmd_ret = LTTNG_ERR_FATAL; | |
7637 | goto end; | |
7638 | } | |
7639 | ||
7640 | switch (usess->buffer_type) { | |
7641 | case LTTNG_BUFFER_PER_UID: | |
7642 | { | |
7643 | struct buffer_reg_uid *reg; | |
7644 | ||
7645 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7646 | struct buffer_reg_channel *buf_reg_chan; |
4a9b9759 MD |
7647 | struct consumer_socket *socket; |
7648 | ||
7649 | /* Get consumer socket to use to push the metadata.*/ | |
7650 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7651 | usess->consumer); | |
7652 | if (!socket) { | |
7653 | cmd_ret = LTTNG_ERR_INVALID; | |
7654 | goto error_socket; | |
7655 | } | |
7656 | ||
7657 | /* Clear the data channels. */ | |
7658 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 7659 | buf_reg_chan, node.node) { |
4a9b9759 | 7660 | ret = consumer_clear_channel(socket, |
3273699d | 7661 | buf_reg_chan->consumer_key); |
4a9b9759 MD |
7662 | if (ret < 0) { |
7663 | goto error; | |
7664 | } | |
7665 | } | |
7666 | ||
d7bfb9b0 JG |
7667 | { |
7668 | auto locked_registry = reg->registry->reg.ust->lock(); | |
7669 | (void) push_metadata(locked_registry, usess->consumer); | |
7670 | } | |
4a9b9759 MD |
7671 | |
7672 | /* | |
7673 | * Clear the metadata channel. | |
7674 | * Metadata channel is not cleared per se but we still need to | |
7675 | * perform a rotation operation on it behind the scene. | |
7676 | */ | |
7677 | ret = consumer_clear_channel(socket, | |
aeeb48c6 | 7678 | reg->registry->reg.ust->_metadata_key); |
4a9b9759 MD |
7679 | if (ret < 0) { |
7680 | goto error; | |
7681 | } | |
7682 | } | |
7683 | break; | |
7684 | } | |
7685 | case LTTNG_BUFFER_PER_PID: | |
7686 | { | |
7687 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7688 | struct consumer_socket *socket; | |
7689 | struct lttng_ht_iter chan_iter; | |
7690 | struct ust_app_channel *ua_chan; | |
7691 | struct ust_app_session *ua_sess; | |
b0f2e8db | 7692 | lsu::registry_session *registry; |
4a9b9759 MD |
7693 | |
7694 | ua_sess = lookup_session_by_app(usess, app); | |
7695 | if (!ua_sess) { | |
7696 | /* Session not associated with this app. */ | |
7697 | continue; | |
7698 | } | |
7699 | ||
7700 | /* Get the right consumer socket for the application. */ | |
d7bfb9b0 | 7701 | socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, |
4a9b9759 MD |
7702 | usess->consumer); |
7703 | if (!socket) { | |
7704 | cmd_ret = LTTNG_ERR_INVALID; | |
7705 | goto error_socket; | |
7706 | } | |
7707 | ||
7708 | registry = get_session_registry(ua_sess); | |
7709 | if (!registry) { | |
7710 | DBG("Application session is being torn down. Skip application."); | |
7711 | continue; | |
7712 | } | |
7713 | ||
7714 | /* Clear the data channels. */ | |
7715 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7716 | ua_chan, node.node) { | |
7717 | ret = consumer_clear_channel(socket, ua_chan->key); | |
7718 | if (ret < 0) { | |
7719 | /* Per-PID buffer and application going away. */ | |
7720 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7721 | continue; | |
7722 | } | |
7723 | goto error; | |
7724 | } | |
7725 | } | |
7726 | ||
d7bfb9b0 JG |
7727 | { |
7728 | auto locked_registry = registry->lock(); | |
7729 | (void) push_metadata(locked_registry, usess->consumer); | |
7730 | } | |
4a9b9759 MD |
7731 | |
7732 | /* | |
7733 | * Clear the metadata channel. | |
7734 | * Metadata channel is not cleared per se but we still need to | |
7735 | * perform rotation operation on it behind the scene. | |
7736 | */ | |
aeeb48c6 | 7737 | ret = consumer_clear_channel(socket, registry->_metadata_key); |
4a9b9759 MD |
7738 | if (ret < 0) { |
7739 | /* Per-PID buffer and application going away. */ | |
7740 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7741 | continue; | |
7742 | } | |
7743 | goto error; | |
7744 | } | |
7745 | } | |
7746 | break; | |
7747 | } | |
7748 | default: | |
a0377dfe | 7749 | abort(); |
4a9b9759 MD |
7750 | break; |
7751 | } | |
7752 | ||
7753 | cmd_ret = LTTNG_OK; | |
7754 | goto end; | |
7755 | ||
7756 | error: | |
7757 | switch (-ret) { | |
7758 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
7759 | cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; | |
7760 | break; | |
7761 | default: | |
7762 | cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; | |
7763 | } | |
7764 | ||
7765 | error_socket: | |
7766 | end: | |
7767 | rcu_read_unlock(); | |
7768 | return cmd_ret; | |
7769 | } | |
04ed9e10 JG |
7770 | |
7771 | /* | |
7772 | * This function skips the metadata channel as the begin/end timestamps of a | |
7773 | * metadata packet are useless. | |
7774 | * | |
7775 | * Moreover, opening a packet after a "clear" will cause problems for live | |
7776 | * sessions as it will introduce padding that was not part of the first trace | |
7777 | * chunk. The relay daemon expects the content of the metadata stream of | |
7778 | * successive metadata trace chunks to be strict supersets of one another. | |
7779 | * | |
7780 | * For example, flushing a packet at the beginning of the metadata stream of | |
7781 | * a trace chunk resulting from a "clear" session command will cause the | |
7782 | * size of the metadata stream of the new trace chunk to not match the size of | |
7783 | * the metadata stream of the original chunk. This will confuse the relay | |
7784 | * daemon as the same "offset" in a metadata stream will no longer point | |
7785 | * to the same content. | |
7786 | */ | |
7787 | enum lttng_error_code ust_app_open_packets(struct ltt_session *session) | |
7788 | { | |
7789 | enum lttng_error_code ret = LTTNG_OK; | |
7790 | struct lttng_ht_iter iter; | |
7791 | struct ltt_ust_session *usess = session->ust_session; | |
7792 | ||
a0377dfe | 7793 | LTTNG_ASSERT(usess); |
04ed9e10 JG |
7794 | |
7795 | rcu_read_lock(); | |
7796 | ||
7797 | switch (usess->buffer_type) { | |
7798 | case LTTNG_BUFFER_PER_UID: | |
7799 | { | |
7800 | struct buffer_reg_uid *reg; | |
7801 | ||
7802 | cds_list_for_each_entry ( | |
7803 | reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7804 | struct buffer_reg_channel *buf_reg_chan; |
04ed9e10 JG |
7805 | struct consumer_socket *socket; |
7806 | ||
7807 | socket = consumer_find_socket_by_bitness( | |
7808 | reg->bits_per_long, usess->consumer); | |
7809 | if (!socket) { | |
7810 | ret = LTTNG_ERR_FATAL; | |
7811 | goto error; | |
7812 | } | |
7813 | ||
7814 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
3273699d | 7815 | &iter.iter, buf_reg_chan, node.node) { |
04ed9e10 JG |
7816 | const int open_ret = |
7817 | consumer_open_channel_packets( | |
7818 | socket, | |
3273699d | 7819 | buf_reg_chan->consumer_key); |
04ed9e10 JG |
7820 | |
7821 | if (open_ret < 0) { | |
7822 | ret = LTTNG_ERR_UNK; | |
7823 | goto error; | |
7824 | } | |
7825 | } | |
7826 | } | |
7827 | break; | |
7828 | } | |
7829 | case LTTNG_BUFFER_PER_PID: | |
7830 | { | |
7831 | struct ust_app *app; | |
7832 | ||
7833 | cds_lfht_for_each_entry ( | |
7834 | ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7835 | struct consumer_socket *socket; | |
7836 | struct lttng_ht_iter chan_iter; | |
7837 | struct ust_app_channel *ua_chan; | |
7838 | struct ust_app_session *ua_sess; | |
b0f2e8db | 7839 | lsu::registry_session *registry; |
04ed9e10 JG |
7840 | |
7841 | ua_sess = lookup_session_by_app(usess, app); | |
7842 | if (!ua_sess) { | |
7843 | /* Session not associated with this app. */ | |
7844 | continue; | |
7845 | } | |
7846 | ||
7847 | /* Get the right consumer socket for the application. */ | |
7848 | socket = consumer_find_socket_by_bitness( | |
d7bfb9b0 | 7849 | app->abi.bits_per_long, usess->consumer); |
04ed9e10 JG |
7850 | if (!socket) { |
7851 | ret = LTTNG_ERR_FATAL; | |
7852 | goto error; | |
7853 | } | |
7854 | ||
7855 | registry = get_session_registry(ua_sess); | |
7856 | if (!registry) { | |
7857 | DBG("Application session is being torn down. Skip application."); | |
7858 | continue; | |
7859 | } | |
7860 | ||
7861 | cds_lfht_for_each_entry(ua_sess->channels->ht, | |
7862 | &chan_iter.iter, ua_chan, node.node) { | |
7863 | const int open_ret = | |
7864 | consumer_open_channel_packets( | |
7865 | socket, | |
7866 | ua_chan->key); | |
7867 | ||
7868 | if (open_ret < 0) { | |
7869 | /* | |
7870 | * Per-PID buffer and application going | |
7871 | * away. | |
7872 | */ | |
97a171e1 | 7873 | if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
04ed9e10 JG |
7874 | continue; |
7875 | } | |
7876 | ||
7877 | ret = LTTNG_ERR_UNK; | |
7878 | goto error; | |
7879 | } | |
7880 | } | |
7881 | } | |
7882 | break; | |
7883 | } | |
7884 | default: | |
7885 | abort(); | |
7886 | break; | |
7887 | } | |
7888 | ||
7889 | error: | |
7890 | rcu_read_unlock(); | |
7891 | return ret; | |
7892 | } |