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