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