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