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