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