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