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