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 DG |
1987 | /* Flag the channel that it is sent to the application. */ |
1988 | ua_chan->is_sent = 1; | |
ffe60014 | 1989 | |
b551a063 | 1990 | error: |
840cb59c | 1991 | health_code_update(); |
b551a063 DG |
1992 | return ret; |
1993 | } | |
1994 | ||
91d76f53 | 1995 | /* |
5b4a0ec0 | 1996 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
1997 | * |
1998 | * Should be called with session mutex held. | |
91d76f53 | 1999 | */ |
edb67388 | 2000 | static |
f46376a1 | 2001 | int create_ust_event(struct ust_app *app, |
edb67388 | 2002 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
91d76f53 | 2003 | { |
5b4a0ec0 | 2004 | int ret = 0; |
284d8f55 | 2005 | |
840cb59c | 2006 | health_code_update(); |
86acf0da | 2007 | |
5b4a0ec0 | 2008 | /* Create UST event on tracer */ |
fb45065e | 2009 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 2010 | ret = lttng_ust_ctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 | 2011 | &ua_event->obj); |
fb45065e | 2012 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 | 2013 | if (ret < 0) { |
be355079 JR |
2014 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2015 | ret = 0; | |
2016 | DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d", | |
2017 | app->pid, app->sock); | |
2018 | } else if (ret == -EAGAIN) { | |
3757b385 | 2019 | ret = 0; |
be355079 JR |
2020 | WARN("UST app create event failed. Communication time out: pid = %d, sock = %d", |
2021 | app->pid, app->sock); | |
2022 | } else { | |
2023 | ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d", | |
2024 | ua_event->attr.name, ret, app->pid, | |
2025 | app->sock); | |
ffe60014 | 2026 | } |
5b4a0ec0 | 2027 | goto error; |
91d76f53 | 2028 | } |
f6a9efaa | 2029 | |
5b4a0ec0 | 2030 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 2031 | |
be355079 | 2032 | DBG2("UST app event %s created successfully for pid:%d object = %p", |
3428a1b7 | 2033 | ua_event->attr.name, app->pid, ua_event->obj); |
f6a9efaa | 2034 | |
840cb59c | 2035 | health_code_update(); |
86acf0da | 2036 | |
025faf73 DG |
2037 | /* Set filter if one is present. */ |
2038 | if (ua_event->filter) { | |
a154c7b8 | 2039 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
025faf73 DG |
2040 | if (ret < 0) { |
2041 | goto error; | |
2042 | } | |
2043 | } | |
2044 | ||
7cc9a73c JI |
2045 | /* Set exclusions for the event */ |
2046 | if (ua_event->exclusion) { | |
c0901ffa | 2047 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
7cc9a73c JI |
2048 | if (ret < 0) { |
2049 | goto error; | |
2050 | } | |
2051 | } | |
2052 | ||
8535a6d9 | 2053 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
2054 | if (ua_event->enabled) { |
2055 | /* | |
2056 | * We now need to explicitly enable the event, since it | |
2057 | * is now disabled at creation. | |
2058 | */ | |
3428a1b7 | 2059 | ret = enable_ust_object(app, ua_event->obj); |
40113787 MD |
2060 | if (ret < 0) { |
2061 | /* | |
2062 | * If we hit an EPERM, something is wrong with our enable call. If | |
2063 | * we get an EEXIST, there is a problem on the tracer side since we | |
2064 | * just created it. | |
2065 | */ | |
2066 | switch (ret) { | |
2067 | case -LTTNG_UST_ERR_PERM: | |
2068 | /* Code flow problem */ | |
a0377dfe | 2069 | abort(); |
40113787 MD |
2070 | case -LTTNG_UST_ERR_EXIST: |
2071 | /* It's OK for our use case. */ | |
2072 | ret = 0; | |
2073 | break; | |
2074 | default: | |
2075 | break; | |
2076 | } | |
2077 | goto error; | |
2078 | } | |
8535a6d9 DG |
2079 | } |
2080 | ||
5b4a0ec0 | 2081 | error: |
840cb59c | 2082 | health_code_update(); |
5b4a0ec0 | 2083 | return ret; |
91d76f53 | 2084 | } |
48842b30 | 2085 | |
993578ff JR |
2086 | static int init_ust_event_notifier_from_event_rule( |
2087 | const struct lttng_event_rule *rule, | |
fc4b93fa | 2088 | struct lttng_ust_abi_event_notifier *event_notifier) |
993578ff JR |
2089 | { |
2090 | enum lttng_event_rule_status status; | |
fc4b93fa | 2091 | enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
993578ff JR |
2092 | int loglevel = -1, ret = 0; |
2093 | const char *pattern; | |
2094 | ||
993578ff JR |
2095 | |
2096 | memset(event_notifier, 0, sizeof(*event_notifier)); | |
2097 | ||
44760c20 JR |
2098 | if (lttng_event_rule_targets_agent_domain(rule)) { |
2099 | /* | |
2100 | * Special event for agents | |
2101 | * The actual meat of the event is in the filter that will be | |
2102 | * attached later on. | |
2103 | * Set the default values for the agent event. | |
2104 | */ | |
2105 | pattern = event_get_default_agent_ust_name( | |
2106 | lttng_event_rule_get_domain_type(rule)); | |
2107 | loglevel = 0; | |
fc4b93fa | 2108 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
44760c20 | 2109 | } else { |
85b05318 | 2110 | const struct lttng_log_level_rule *log_level_rule; |
993578ff | 2111 | |
a0377dfe | 2112 | LTTNG_ASSERT(lttng_event_rule_get_type(rule) == |
695f7044 JR |
2113 | LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT); |
2114 | ||
2115 | status = lttng_event_rule_user_tracepoint_get_name_pattern(rule, &pattern); | |
44760c20 JR |
2116 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
2117 | /* At this point, this is a fatal error. */ | |
2118 | abort(); | |
2119 | } | |
993578ff | 2120 | |
695f7044 | 2121 | status = lttng_event_rule_user_tracepoint_get_log_level_rule( |
85b05318 JR |
2122 | rule, &log_level_rule); |
2123 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { | |
fc4b93fa | 2124 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL; |
85b05318 JR |
2125 | } else if (status == LTTNG_EVENT_RULE_STATUS_OK) { |
2126 | enum lttng_log_level_rule_status llr_status; | |
2127 | ||
2128 | switch (lttng_log_level_rule_get_type(log_level_rule)) { | |
2129 | case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY: | |
2130 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE; | |
2131 | llr_status = lttng_log_level_rule_exactly_get_level( | |
2132 | log_level_rule, &loglevel); | |
2133 | break; | |
2134 | case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS: | |
2135 | ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE; | |
2136 | llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level( | |
2137 | log_level_rule, &loglevel); | |
2138 | break; | |
2139 | default: | |
2140 | abort(); | |
2141 | } | |
993578ff | 2142 | |
a0377dfe | 2143 | LTTNG_ASSERT(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK); |
85b05318 JR |
2144 | } else { |
2145 | /* At this point this is a fatal error. */ | |
2146 | abort(); | |
44760c20 | 2147 | } |
993578ff JR |
2148 | } |
2149 | ||
fc4b93fa | 2150 | event_notifier->event.instrumentation = LTTNG_UST_ABI_TRACEPOINT; |
993578ff | 2151 | ret = lttng_strncpy(event_notifier->event.name, pattern, |
4ab5469c | 2152 | sizeof(event_notifier->event.name)); |
993578ff JR |
2153 | if (ret) { |
2154 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", | |
2155 | pattern); | |
2156 | goto end; | |
2157 | } | |
2158 | ||
2159 | event_notifier->event.loglevel_type = ust_loglevel_type; | |
2160 | event_notifier->event.loglevel = loglevel; | |
2161 | end: | |
2162 | return ret; | |
2163 | } | |
2164 | ||
2165 | /* | |
2166 | * Create the specified event notifier against the user space tracer of a | |
2167 | * given application. | |
2168 | */ | |
2169 | static int create_ust_event_notifier(struct ust_app *app, | |
2170 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) | |
2171 | { | |
2172 | int ret = 0; | |
267d66aa JR |
2173 | enum lttng_condition_status condition_status; |
2174 | const struct lttng_condition *condition = NULL; | |
fc4b93fa | 2175 | struct lttng_ust_abi_event_notifier event_notifier; |
267d66aa | 2176 | const struct lttng_event_rule *event_rule = NULL; |
f83be61d JR |
2177 | unsigned int capture_bytecode_count = 0, i; |
2178 | enum lttng_condition_status cond_status; | |
695f7044 | 2179 | enum lttng_event_rule_type event_rule_type; |
993578ff JR |
2180 | |
2181 | health_code_update(); | |
a0377dfe | 2182 | LTTNG_ASSERT(app->event_notifier_group.object); |
993578ff | 2183 | |
267d66aa JR |
2184 | condition = lttng_trigger_get_const_condition( |
2185 | ua_event_notifier_rule->trigger); | |
a0377dfe FD |
2186 | LTTNG_ASSERT(condition); |
2187 | LTTNG_ASSERT(lttng_condition_get_type(condition) == | |
8dbb86b8 | 2188 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES); |
993578ff | 2189 | |
8dbb86b8 | 2190 | condition_status = lttng_condition_event_rule_matches_get_rule( |
d602bd6a | 2191 | condition, &event_rule); |
a0377dfe | 2192 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
d602bd6a | 2193 | |
a0377dfe | 2194 | LTTNG_ASSERT(event_rule); |
695f7044 JR |
2195 | |
2196 | event_rule_type = lttng_event_rule_get_type(event_rule); | |
a0377dfe | 2197 | LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT || |
695f7044 JR |
2198 | event_rule_type == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING || |
2199 | event_rule_type == | |
2200 | LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING || | |
2201 | event_rule_type == | |
2202 | LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING); | |
267d66aa JR |
2203 | |
2204 | init_ust_event_notifier_from_event_rule(event_rule, &event_notifier); | |
993578ff | 2205 | event_notifier.event.token = ua_event_notifier_rule->token; |
533a90fb | 2206 | event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index; |
993578ff JR |
2207 | |
2208 | /* Create UST event notifier against the tracer. */ | |
2209 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 2210 | ret = lttng_ust_ctl_create_event_notifier(app->sock, &event_notifier, |
993578ff JR |
2211 | app->event_notifier_group.object, |
2212 | &ua_event_notifier_rule->obj); | |
2213 | pthread_mutex_unlock(&app->sock_lock); | |
2214 | if (ret < 0) { | |
be355079 | 2215 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
993578ff | 2216 | ret = 0; |
be355079 JR |
2217 | DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d", |
2218 | app->pid, app->sock); | |
2219 | } else if (ret == -EAGAIN) { | |
2220 | ret = 0; | |
2221 | WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d", | |
2222 | app->pid, app->sock); | |
2223 | } else { | |
2224 | ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d", | |
2225 | event_notifier.event.name, ret, app->pid, | |
2226 | app->sock); | |
993578ff | 2227 | } |
993578ff JR |
2228 | goto error; |
2229 | } | |
2230 | ||
2231 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; | |
2232 | ||
9324443a | 2233 | DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p", |
be355079 | 2234 | event_notifier.event.name, app->name, app->pid, |
993578ff JR |
2235 | ua_event_notifier_rule->obj); |
2236 | ||
2237 | health_code_update(); | |
2238 | ||
2239 | /* Set filter if one is present. */ | |
2240 | if (ua_event_notifier_rule->filter) { | |
2241 | ret = set_ust_object_filter(app, ua_event_notifier_rule->filter, | |
2242 | ua_event_notifier_rule->obj); | |
2243 | if (ret < 0) { | |
2244 | goto error; | |
2245 | } | |
2246 | } | |
2247 | ||
2248 | /* Set exclusions for the event. */ | |
2249 | if (ua_event_notifier_rule->exclusion) { | |
2250 | ret = set_ust_object_exclusions(app, | |
2251 | ua_event_notifier_rule->exclusion, | |
2252 | ua_event_notifier_rule->obj); | |
2253 | if (ret < 0) { | |
2254 | goto error; | |
2255 | } | |
2256 | } | |
f83be61d JR |
2257 | |
2258 | /* Set the capture bytecodes. */ | |
8dbb86b8 | 2259 | cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count( |
f83be61d | 2260 | condition, &capture_bytecode_count); |
a0377dfe | 2261 | LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK); |
f83be61d JR |
2262 | |
2263 | for (i = 0; i < capture_bytecode_count; i++) { | |
2264 | const struct lttng_bytecode *capture_bytecode = | |
8dbb86b8 | 2265 | lttng_condition_event_rule_matches_get_capture_bytecode_at_index( |
f83be61d JR |
2266 | condition, i); |
2267 | ||
11f6ce94 | 2268 | ret = set_ust_capture(app, capture_bytecode, i, |
f83be61d JR |
2269 | ua_event_notifier_rule->obj); |
2270 | if (ret < 0) { | |
2271 | goto error; | |
2272 | } | |
2273 | } | |
993578ff JR |
2274 | |
2275 | /* | |
2276 | * We now need to explicitly enable the event, since it | |
2277 | * is disabled at creation. | |
2278 | */ | |
2279 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); | |
2280 | if (ret < 0) { | |
2281 | /* | |
2282 | * If we hit an EPERM, something is wrong with our enable call. | |
2283 | * If we get an EEXIST, there is a problem on the tracer side | |
2284 | * since we just created it. | |
2285 | */ | |
2286 | switch (ret) { | |
2287 | case -LTTNG_UST_ERR_PERM: | |
2288 | /* Code flow problem. */ | |
2289 | abort(); | |
2290 | case -LTTNG_UST_ERR_EXIST: | |
2291 | /* It's OK for our use case. */ | |
2292 | ret = 0; | |
2293 | break; | |
2294 | default: | |
2295 | break; | |
2296 | } | |
2297 | ||
2298 | goto error; | |
2299 | } | |
2300 | ||
2301 | ua_event_notifier_rule->enabled = true; | |
2302 | ||
2303 | error: | |
2304 | health_code_update(); | |
2305 | return ret; | |
2306 | } | |
2307 | ||
5b4a0ec0 DG |
2308 | /* |
2309 | * Copy data between an UST app event and a LTT event. | |
2310 | */ | |
421cb601 | 2311 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
2312 | struct ltt_ust_event *uevent) |
2313 | { | |
b4ffad32 JI |
2314 | size_t exclusion_alloc_size; |
2315 | ||
48842b30 DG |
2316 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
2317 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
2318 | ||
fc34caaa DG |
2319 | ua_event->enabled = uevent->enabled; |
2320 | ||
5b4a0ec0 DG |
2321 | /* Copy event attributes */ |
2322 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
2323 | ||
53a80697 MD |
2324 | /* Copy filter bytecode */ |
2325 | if (uevent->filter) { | |
2b00d462 | 2326 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
025faf73 | 2327 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 2328 | } |
b4ffad32 JI |
2329 | |
2330 | /* Copy exclusion data */ | |
2331 | if (uevent->exclusion) { | |
51755dc8 | 2332 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
fc4b93fa | 2333 | LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count; |
64803277 | 2334 | ua_event->exclusion = zmalloc<lttng_event_exclusion>(exclusion_alloc_size); |
5f8df26c JI |
2335 | if (ua_event->exclusion == NULL) { |
2336 | PERROR("malloc"); | |
2337 | } else { | |
2338 | memcpy(ua_event->exclusion, uevent->exclusion, | |
2339 | exclusion_alloc_size); | |
b4ffad32 JI |
2340 | } |
2341 | } | |
48842b30 DG |
2342 | } |
2343 | ||
5b4a0ec0 DG |
2344 | /* |
2345 | * Copy data between an UST app channel and a LTT channel. | |
2346 | */ | |
421cb601 | 2347 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
2348 | struct ltt_ust_channel *uchan) |
2349 | { | |
fc34caaa | 2350 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
2351 | |
2352 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
2353 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 2354 | |
1624d5b7 JD |
2355 | ua_chan->tracefile_size = uchan->tracefile_size; |
2356 | ua_chan->tracefile_count = uchan->tracefile_count; | |
2357 | ||
ffe60014 DG |
2358 | /* Copy event attributes since the layout is different. */ |
2359 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
2360 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
2361 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
2362 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
2363 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
e9404c27 | 2364 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
7966af57 | 2365 | ua_chan->attr.output = (lttng_ust_abi_output) uchan->attr.output; |
491d1539 MD |
2366 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
2367 | ||
ffe60014 DG |
2368 | /* |
2369 | * Note that the attribute channel type is not set since the channel on the | |
2370 | * tracing registry side does not have this information. | |
2371 | */ | |
48842b30 | 2372 | |
fc34caaa | 2373 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 2374 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 2375 | |
fc34caaa | 2376 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
2377 | } |
2378 | ||
5b4a0ec0 DG |
2379 | /* |
2380 | * Copy data between a UST app session and a regular LTT session. | |
2381 | */ | |
421cb601 | 2382 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 2383 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2384 | { |
477d7741 MD |
2385 | struct tm *timeinfo; |
2386 | char datetime[16]; | |
2387 | int ret; | |
d7ba1388 | 2388 | char tmp_shm_path[PATH_MAX]; |
477d7741 | 2389 | |
940c4592 | 2390 | timeinfo = localtime(&app->registration_time); |
477d7741 | 2391 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
48842b30 | 2392 | |
421cb601 | 2393 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 2394 | |
7972aab2 DG |
2395 | ua_sess->tracing_id = usess->id; |
2396 | ua_sess->id = get_next_session_id(); | |
ff588497 JR |
2397 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
2398 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); | |
2399 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); | |
2400 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); | |
7972aab2 DG |
2401 | ua_sess->buffer_type = usess->buffer_type; |
2402 | ua_sess->bits_per_long = app->bits_per_long; | |
6addfa37 | 2403 | |
7972aab2 | 2404 | /* There is only one consumer object per session possible. */ |
6addfa37 | 2405 | consumer_output_get(usess->consumer); |
7972aab2 | 2406 | ua_sess->consumer = usess->consumer; |
6addfa37 | 2407 | |
2bba9e53 | 2408 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 2409 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
2410 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
2411 | &usess->metadata_attr); | |
7972aab2 DG |
2412 | |
2413 | switch (ua_sess->buffer_type) { | |
2414 | case LTTNG_BUFFER_PER_PID: | |
2415 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 2416 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
2417 | datetime); |
2418 | break; | |
2419 | case LTTNG_BUFFER_PER_UID: | |
2420 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
470cc211 | 2421 | DEFAULT_UST_TRACE_UID_PATH, |
ff588497 | 2422 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
470cc211 | 2423 | app->bits_per_long); |
7972aab2 DG |
2424 | break; |
2425 | default: | |
a0377dfe | 2426 | abort(); |
7972aab2 DG |
2427 | goto error; |
2428 | } | |
477d7741 MD |
2429 | if (ret < 0) { |
2430 | PERROR("asprintf UST shadow copy session"); | |
a0377dfe | 2431 | abort(); |
7972aab2 | 2432 | goto error; |
477d7741 MD |
2433 | } |
2434 | ||
3d071855 MD |
2435 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
2436 | sizeof(ua_sess->root_shm_path)); | |
2437 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2438 | strncpy(ua_sess->shm_path, usess->shm_path, |
2439 | sizeof(ua_sess->shm_path)); | |
2440 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2441 | if (ua_sess->shm_path[0]) { | |
2442 | switch (ua_sess->buffer_type) { | |
2443 | case LTTNG_BUFFER_PER_PID: | |
2444 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2445 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
d7ba1388 MD |
2446 | app->name, app->pid, datetime); |
2447 | break; | |
2448 | case LTTNG_BUFFER_PER_UID: | |
2449 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2450 | "/" DEFAULT_UST_TRACE_UID_PATH, |
d7ba1388 MD |
2451 | app->uid, app->bits_per_long); |
2452 | break; | |
2453 | default: | |
a0377dfe | 2454 | abort(); |
d7ba1388 MD |
2455 | goto error; |
2456 | } | |
2457 | if (ret < 0) { | |
2458 | PERROR("sprintf UST shadow copy session"); | |
a0377dfe | 2459 | abort(); |
d7ba1388 MD |
2460 | goto error; |
2461 | } | |
2462 | strncat(ua_sess->shm_path, tmp_shm_path, | |
2463 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
2464 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2465 | } | |
6addfa37 | 2466 | return; |
7972aab2 DG |
2467 | |
2468 | error: | |
6addfa37 | 2469 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
2470 | } |
2471 | ||
78f0bacd DG |
2472 | /* |
2473 | * Lookup sesison wrapper. | |
2474 | */ | |
84cd17c6 | 2475 | static |
fb9a95c4 | 2476 | void __lookup_session_by_app(const struct ltt_ust_session *usess, |
bec39940 | 2477 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
2478 | { |
2479 | /* Get right UST app session from app */ | |
d9bf3ca4 | 2480 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
2481 | } |
2482 | ||
421cb601 DG |
2483 | /* |
2484 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 2485 | * id. |
421cb601 | 2486 | */ |
48842b30 | 2487 | static struct ust_app_session *lookup_session_by_app( |
fb9a95c4 | 2488 | const struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2489 | { |
bec39940 | 2490 | struct lttng_ht_iter iter; |
d9bf3ca4 | 2491 | struct lttng_ht_node_u64 *node; |
48842b30 | 2492 | |
84cd17c6 | 2493 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 2494 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
2495 | if (node == NULL) { |
2496 | goto error; | |
2497 | } | |
2498 | ||
2499 | return caa_container_of(node, struct ust_app_session, node); | |
2500 | ||
2501 | error: | |
2502 | return NULL; | |
2503 | } | |
2504 | ||
7972aab2 DG |
2505 | /* |
2506 | * Setup buffer registry per PID for the given session and application. If none | |
2507 | * is found, a new one is created, added to the global registry and | |
2508 | * initialized. If regp is valid, it's set with the newly created object. | |
2509 | * | |
2510 | * Return 0 on success or else a negative value. | |
2511 | */ | |
2512 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
2513 | struct ust_app *app, struct buffer_reg_pid **regp) | |
2514 | { | |
2515 | int ret = 0; | |
2516 | struct buffer_reg_pid *reg_pid; | |
2517 | ||
a0377dfe FD |
2518 | LTTNG_ASSERT(ua_sess); |
2519 | LTTNG_ASSERT(app); | |
7972aab2 DG |
2520 | |
2521 | rcu_read_lock(); | |
2522 | ||
2523 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
2524 | if (!reg_pid) { | |
2525 | /* | |
2526 | * This is the create channel path meaning that if there is NO | |
2527 | * registry available, we have to create one for this session. | |
2528 | */ | |
d7ba1388 | 2529 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 2530 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
2531 | if (ret < 0) { |
2532 | goto error; | |
2533 | } | |
7972aab2 DG |
2534 | } else { |
2535 | goto end; | |
2536 | } | |
2537 | ||
2538 | /* Initialize registry. */ | |
aeeb48c6 | 2539 | reg_pid->registry->reg.ust = ust_registry_session_per_pid_create(app, |
7972aab2 DG |
2540 | app->bits_per_long, app->uint8_t_alignment, |
2541 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf | 2542 | app->uint64_t_alignment, app->long_alignment, |
470cc211 JG |
2543 | app->byte_order, app->version.major, app->version.minor, |
2544 | reg_pid->root_shm_path, reg_pid->shm_path, | |
ff588497 JR |
2545 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
2546 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
aeeb48c6 JG |
2547 | ua_sess->tracing_id); |
2548 | if (!reg_pid->registry->reg.ust) { | |
286c991a MD |
2549 | /* |
2550 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
2551 | * destroy the buffer registry, because it is always expected | |
2552 | * that if the buffer registry can be found, its ust registry is | |
2553 | * non-NULL. | |
2554 | */ | |
2555 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
2556 | goto error; |
2557 | } | |
2558 | ||
286c991a MD |
2559 | buffer_reg_pid_add(reg_pid); |
2560 | ||
7972aab2 DG |
2561 | DBG3("UST app buffer registry per PID created successfully"); |
2562 | ||
2563 | end: | |
2564 | if (regp) { | |
2565 | *regp = reg_pid; | |
2566 | } | |
2567 | error: | |
2568 | rcu_read_unlock(); | |
2569 | return ret; | |
2570 | } | |
2571 | ||
2572 | /* | |
2573 | * Setup buffer registry per UID for the given session and application. If none | |
2574 | * is found, a new one is created, added to the global registry and | |
2575 | * initialized. If regp is valid, it's set with the newly created object. | |
2576 | * | |
2577 | * Return 0 on success or else a negative value. | |
2578 | */ | |
2579 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 2580 | struct ust_app_session *ua_sess, |
7972aab2 DG |
2581 | struct ust_app *app, struct buffer_reg_uid **regp) |
2582 | { | |
2583 | int ret = 0; | |
2584 | struct buffer_reg_uid *reg_uid; | |
2585 | ||
a0377dfe FD |
2586 | LTTNG_ASSERT(usess); |
2587 | LTTNG_ASSERT(app); | |
7972aab2 DG |
2588 | |
2589 | rcu_read_lock(); | |
2590 | ||
2591 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
2592 | if (!reg_uid) { | |
2593 | /* | |
2594 | * This is the create channel path meaning that if there is NO | |
2595 | * registry available, we have to create one for this session. | |
2596 | */ | |
2597 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, | |
3d071855 MD |
2598 | LTTNG_DOMAIN_UST, ®_uid, |
2599 | ua_sess->root_shm_path, ua_sess->shm_path); | |
7972aab2 DG |
2600 | if (ret < 0) { |
2601 | goto error; | |
2602 | } | |
7972aab2 DG |
2603 | } else { |
2604 | goto end; | |
2605 | } | |
2606 | ||
2607 | /* Initialize registry. */ | |
aeeb48c6 | 2608 | reg_uid->registry->reg.ust = ust_registry_session_per_uid_create( |
7972aab2 DG |
2609 | app->bits_per_long, app->uint8_t_alignment, |
2610 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
2611 | app->uint64_t_alignment, app->long_alignment, |
2612 | app->byte_order, app->version.major, | |
3d071855 | 2613 | app->version.minor, reg_uid->root_shm_path, |
8de88061 JR |
2614 | reg_uid->shm_path, usess->uid, usess->gid, |
2615 | ua_sess->tracing_id, app->uid); | |
aeeb48c6 | 2616 | if (!reg_uid->registry->reg.ust) { |
286c991a MD |
2617 | /* |
2618 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
2619 | * destroy the buffer registry, because it is always expected | |
2620 | * that if the buffer registry can be found, its ust registry is | |
2621 | * non-NULL. | |
2622 | */ | |
2623 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
2624 | goto error; |
2625 | } | |
aeeb48c6 | 2626 | |
7972aab2 DG |
2627 | /* Add node to teardown list of the session. */ |
2628 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
2629 | ||
286c991a | 2630 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 2631 | |
286c991a | 2632 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
2633 | end: |
2634 | if (regp) { | |
2635 | *regp = reg_uid; | |
2636 | } | |
2637 | error: | |
2638 | rcu_read_unlock(); | |
2639 | return ret; | |
2640 | } | |
2641 | ||
421cb601 | 2642 | /* |
3d8ca23b | 2643 | * Create a session on the tracer side for the given app. |
421cb601 | 2644 | * |
3d8ca23b DG |
2645 | * On success, ua_sess_ptr is populated with the session pointer or else left |
2646 | * untouched. If the session was created, is_created is set to 1. On error, | |
2647 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
2648 | * be NULL. | |
2649 | * | |
2650 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
b623cb6a | 2651 | * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails. |
421cb601 | 2652 | */ |
03f91eaa | 2653 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
3d8ca23b DG |
2654 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
2655 | int *is_created) | |
421cb601 | 2656 | { |
3d8ca23b | 2657 | int ret, created = 0; |
421cb601 DG |
2658 | struct ust_app_session *ua_sess; |
2659 | ||
a0377dfe FD |
2660 | LTTNG_ASSERT(usess); |
2661 | LTTNG_ASSERT(app); | |
2662 | LTTNG_ASSERT(ua_sess_ptr); | |
3d8ca23b | 2663 | |
840cb59c | 2664 | health_code_update(); |
86acf0da | 2665 | |
421cb601 DG |
2666 | ua_sess = lookup_session_by_app(usess, app); |
2667 | if (ua_sess == NULL) { | |
d9bf3ca4 | 2668 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 2669 | app->pid, usess->id); |
40bbd087 | 2670 | ua_sess = alloc_ust_app_session(); |
421cb601 DG |
2671 | if (ua_sess == NULL) { |
2672 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
2673 | ret = -ENOMEM; |
2674 | goto error; | |
421cb601 | 2675 | } |
477d7741 | 2676 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 2677 | created = 1; |
421cb601 DG |
2678 | } |
2679 | ||
7972aab2 DG |
2680 | switch (usess->buffer_type) { |
2681 | case LTTNG_BUFFER_PER_PID: | |
2682 | /* Init local registry. */ | |
2683 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 2684 | if (ret < 0) { |
e64207cf | 2685 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2686 | goto error; |
2687 | } | |
2688 | break; | |
2689 | case LTTNG_BUFFER_PER_UID: | |
2690 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 2691 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 2692 | if (ret < 0) { |
e64207cf | 2693 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2694 | goto error; |
2695 | } | |
2696 | break; | |
2697 | default: | |
a0377dfe | 2698 | abort(); |
7972aab2 DG |
2699 | ret = -EINVAL; |
2700 | goto error; | |
2701 | } | |
2702 | ||
2703 | health_code_update(); | |
2704 | ||
2705 | if (ua_sess->handle == -1) { | |
fb45065e | 2706 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 2707 | ret = lttng_ust_ctl_create_session(app->sock); |
fb45065e | 2708 | pthread_mutex_unlock(&app->sock_lock); |
7972aab2 | 2709 | if (ret < 0) { |
be355079 JR |
2710 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2711 | DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d", | |
2712 | app->pid, app->sock); | |
2713 | ret = 0; | |
2714 | } else if (ret == -EAGAIN) { | |
2715 | DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d", | |
2716 | app->pid, app->sock); | |
3757b385 | 2717 | ret = 0; |
be355079 JR |
2718 | } else { |
2719 | ERR("UST app creating session failed with ret %d: pid = %d, sock =%d", | |
2720 | ret, app->pid, app->sock); | |
ffe60014 | 2721 | } |
d0b96690 | 2722 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
2723 | if (ret != -ENOMEM) { |
2724 | /* | |
2725 | * Tracer is probably gone or got an internal error so let's | |
2726 | * behave like it will soon unregister or not usable. | |
2727 | */ | |
2728 | ret = -ENOTCONN; | |
2729 | } | |
2730 | goto error; | |
421cb601 DG |
2731 | } |
2732 | ||
7972aab2 DG |
2733 | ua_sess->handle = ret; |
2734 | ||
2735 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
2736 | lttng_ht_node_init_u64(&ua_sess->node, |
2737 | ua_sess->tracing_id); | |
2738 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
10b56aef MD |
2739 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
2740 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, | |
2741 | &ua_sess->ust_objd_node); | |
7972aab2 DG |
2742 | |
2743 | DBG2("UST app session created successfully with handle %d", ret); | |
2744 | } | |
2745 | ||
2746 | *ua_sess_ptr = ua_sess; | |
2747 | if (is_created) { | |
2748 | *is_created = created; | |
2749 | } | |
2750 | ||
2751 | /* Everything went well. */ | |
2752 | ret = 0; | |
2753 | ||
2754 | error: | |
2755 | health_code_update(); | |
2756 | return ret; | |
2757 | } | |
2758 | ||
6a6b2068 JG |
2759 | /* |
2760 | * Match function for a hash table lookup of ust_app_ctx. | |
2761 | * | |
2762 | * It matches an ust app context based on the context type and, in the case | |
2763 | * of perf counters, their name. | |
2764 | */ | |
2765 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
2766 | { | |
2767 | struct ust_app_ctx *ctx; | |
bdf64013 | 2768 | const struct lttng_ust_context_attr *key; |
6a6b2068 | 2769 | |
a0377dfe FD |
2770 | LTTNG_ASSERT(node); |
2771 | LTTNG_ASSERT(_key); | |
6a6b2068 JG |
2772 | |
2773 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
7966af57 | 2774 | key = (lttng_ust_context_attr *) _key; |
6a6b2068 JG |
2775 | |
2776 | /* Context type */ | |
2777 | if (ctx->ctx.ctx != key->ctx) { | |
2778 | goto no_match; | |
2779 | } | |
2780 | ||
bdf64013 | 2781 | switch(key->ctx) { |
fc4b93fa | 2782 | case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER: |
6a6b2068 | 2783 | if (strncmp(key->u.perf_counter.name, |
bdf64013 JG |
2784 | ctx->ctx.u.perf_counter.name, |
2785 | sizeof(key->u.perf_counter.name))) { | |
2786 | goto no_match; | |
2787 | } | |
2788 | break; | |
fc4b93fa | 2789 | case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT: |
bdf64013 JG |
2790 | if (strcmp(key->u.app_ctx.provider_name, |
2791 | ctx->ctx.u.app_ctx.provider_name) || | |
2792 | strcmp(key->u.app_ctx.ctx_name, | |
2793 | ctx->ctx.u.app_ctx.ctx_name)) { | |
6a6b2068 JG |
2794 | goto no_match; |
2795 | } | |
bdf64013 JG |
2796 | break; |
2797 | default: | |
2798 | break; | |
6a6b2068 JG |
2799 | } |
2800 | ||
2801 | /* Match. */ | |
2802 | return 1; | |
2803 | ||
2804 | no_match: | |
2805 | return 0; | |
2806 | } | |
2807 | ||
2808 | /* | |
2809 | * Lookup for an ust app context from an lttng_ust_context. | |
2810 | * | |
be184a0f | 2811 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
2812 | * Return an ust_app_ctx object or NULL on error. |
2813 | */ | |
2814 | static | |
2815 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
bdf64013 | 2816 | struct lttng_ust_context_attr *uctx) |
6a6b2068 JG |
2817 | { |
2818 | struct lttng_ht_iter iter; | |
2819 | struct lttng_ht_node_ulong *node; | |
2820 | struct ust_app_ctx *app_ctx = NULL; | |
2821 | ||
a0377dfe FD |
2822 | LTTNG_ASSERT(uctx); |
2823 | LTTNG_ASSERT(ht); | |
48b7cdc2 | 2824 | ASSERT_RCU_READ_LOCKED(); |
6a6b2068 JG |
2825 | |
2826 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
2827 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
2828 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
2829 | node = lttng_ht_iter_get_node_ulong(&iter); | |
2830 | if (!node) { | |
2831 | goto end; | |
2832 | } | |
2833 | ||
2834 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); | |
2835 | ||
2836 | end: | |
2837 | return app_ctx; | |
2838 | } | |
2839 | ||
7972aab2 DG |
2840 | /* |
2841 | * Create a context for the channel on the tracer. | |
2842 | * | |
2843 | * Called with UST app session lock held and a RCU read side lock. | |
2844 | */ | |
2845 | static | |
c9edf082 | 2846 | int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
f3db82be | 2847 | struct lttng_ust_context_attr *uctx, |
7972aab2 DG |
2848 | struct ust_app *app) |
2849 | { | |
2850 | int ret = 0; | |
7972aab2 DG |
2851 | struct ust_app_ctx *ua_ctx; |
2852 | ||
48b7cdc2 FD |
2853 | ASSERT_RCU_READ_LOCKED(); |
2854 | ||
7972aab2 DG |
2855 | DBG2("UST app adding context to channel %s", ua_chan->name); |
2856 | ||
6a6b2068 JG |
2857 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
2858 | if (ua_ctx) { | |
7972aab2 DG |
2859 | ret = -EEXIST; |
2860 | goto error; | |
2861 | } | |
2862 | ||
2863 | ua_ctx = alloc_ust_app_ctx(uctx); | |
2864 | if (ua_ctx == NULL) { | |
2865 | /* malloc failed */ | |
7682f304 | 2866 | ret = -ENOMEM; |
7972aab2 DG |
2867 | goto error; |
2868 | } | |
2869 | ||
2870 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 2871 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 2872 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
2873 | |
2874 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2875 | if (ret < 0) { | |
2876 | goto error; | |
2877 | } | |
2878 | ||
2879 | error: | |
2880 | return ret; | |
2881 | } | |
2882 | ||
2883 | /* | |
2884 | * Enable on the tracer side a ust app event for the session and channel. | |
2885 | * | |
2886 | * Called with UST app session lock held. | |
2887 | */ | |
2888 | static | |
f46376a1 MJ |
2889 | int enable_ust_app_event(struct ust_app_event *ua_event, |
2890 | struct ust_app *app) | |
7972aab2 DG |
2891 | { |
2892 | int ret; | |
2893 | ||
3428a1b7 | 2894 | ret = enable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2895 | if (ret < 0) { |
2896 | goto error; | |
2897 | } | |
2898 | ||
2899 | ua_event->enabled = 1; | |
2900 | ||
2901 | error: | |
2902 | return ret; | |
2903 | } | |
2904 | ||
2905 | /* | |
2906 | * Disable on the tracer side a ust app event for the session and channel. | |
2907 | */ | |
f46376a1 MJ |
2908 | static int disable_ust_app_event(struct ust_app_event *ua_event, |
2909 | struct ust_app *app) | |
7972aab2 DG |
2910 | { |
2911 | int ret; | |
2912 | ||
e2456d0a | 2913 | ret = disable_ust_object(app, ua_event->obj); |
7972aab2 DG |
2914 | if (ret < 0) { |
2915 | goto error; | |
2916 | } | |
2917 | ||
2918 | ua_event->enabled = 0; | |
2919 | ||
2920 | error: | |
2921 | return ret; | |
2922 | } | |
2923 | ||
2924 | /* | |
2925 | * Lookup ust app channel for session and disable it on the tracer side. | |
2926 | */ | |
2927 | static | |
2928 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
2929 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
2930 | { | |
2931 | int ret; | |
2932 | ||
2933 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2934 | if (ret < 0) { | |
2935 | goto error; | |
2936 | } | |
2937 | ||
2938 | ua_chan->enabled = 0; | |
2939 | ||
2940 | error: | |
2941 | return ret; | |
2942 | } | |
2943 | ||
2944 | /* | |
2945 | * Lookup ust app channel for session and enable it on the tracer side. This | |
2946 | * MUST be called with a RCU read side lock acquired. | |
2947 | */ | |
2948 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
2949 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
2950 | { | |
2951 | int ret = 0; | |
2952 | struct lttng_ht_iter iter; | |
2953 | struct lttng_ht_node_str *ua_chan_node; | |
2954 | struct ust_app_channel *ua_chan; | |
2955 | ||
48b7cdc2 FD |
2956 | ASSERT_RCU_READ_LOCKED(); |
2957 | ||
7972aab2 DG |
2958 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2959 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
2960 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 2961 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
2962 | uchan->name, ua_sess->tracing_id); |
2963 | goto error; | |
2964 | } | |
2965 | ||
2966 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2967 | ||
2968 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
2969 | if (ret < 0) { | |
2970 | goto error; | |
2971 | } | |
2972 | ||
2973 | error: | |
2974 | return ret; | |
2975 | } | |
2976 | ||
2977 | /* | |
2978 | * Ask the consumer to create a channel and get it if successful. | |
2979 | * | |
fad1ed2f JR |
2980 | * Called with UST app session lock held. |
2981 | * | |
7972aab2 DG |
2982 | * Return 0 on success or else a negative value. |
2983 | */ | |
2984 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
2985 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
aeeb48c6 | 2986 | int bitness, ust_registry_session *registry) |
7972aab2 DG |
2987 | { |
2988 | int ret; | |
2989 | unsigned int nb_fd = 0; | |
2990 | struct consumer_socket *socket; | |
2991 | ||
a0377dfe FD |
2992 | LTTNG_ASSERT(usess); |
2993 | LTTNG_ASSERT(ua_sess); | |
2994 | LTTNG_ASSERT(ua_chan); | |
2995 | LTTNG_ASSERT(registry); | |
7972aab2 DG |
2996 | |
2997 | rcu_read_lock(); | |
2998 | health_code_update(); | |
2999 | ||
3000 | /* Get the right consumer socket for the application. */ | |
3001 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
3002 | if (!socket) { | |
3003 | ret = -EINVAL; | |
3004 | goto error; | |
3005 | } | |
3006 | ||
3007 | health_code_update(); | |
3008 | ||
3009 | /* Need one fd for the channel. */ | |
3010 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3011 | if (ret < 0) { | |
3012 | ERR("Exhausted number of available FD upon create channel"); | |
3013 | goto error; | |
3014 | } | |
3015 | ||
3016 | /* | |
3017 | * Ask consumer to create channel. The consumer will return the number of | |
3018 | * stream we have to expect. | |
3019 | */ | |
3020 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
d2956687 | 3021 | registry, usess->current_trace_chunk); |
7972aab2 DG |
3022 | if (ret < 0) { |
3023 | goto error_ask; | |
3024 | } | |
3025 | ||
3026 | /* | |
3027 | * Compute the number of fd needed before receiving them. It must be 2 per | |
3028 | * stream (2 being the default value here). | |
3029 | */ | |
3030 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
3031 | ||
3032 | /* Reserve the amount of file descriptor we need. */ | |
3033 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
3034 | if (ret < 0) { | |
3035 | ERR("Exhausted number of available FD upon create channel"); | |
3036 | goto error_fd_get_stream; | |
3037 | } | |
3038 | ||
3039 | health_code_update(); | |
3040 | ||
3041 | /* | |
db786d44 | 3042 | * Now get the channel from the consumer. This call will populate the stream |
7972aab2 DG |
3043 | * list of that channel and set the ust objects. |
3044 | */ | |
d9078d0c DG |
3045 | if (usess->consumer->enabled) { |
3046 | ret = ust_consumer_get_channel(socket, ua_chan); | |
3047 | if (ret < 0) { | |
3048 | goto error_destroy; | |
3049 | } | |
7972aab2 DG |
3050 | } |
3051 | ||
3052 | rcu_read_unlock(); | |
3053 | return 0; | |
3054 | ||
3055 | error_destroy: | |
3056 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
3057 | error_fd_get_stream: | |
3058 | /* | |
3059 | * Initiate a destroy channel on the consumer since we had an error | |
3060 | * handling it on our side. The return value is of no importance since we | |
3061 | * already have a ret value set by the previous error that we need to | |
3062 | * return. | |
3063 | */ | |
3064 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
3065 | error_ask: | |
3066 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3067 | error: | |
3068 | health_code_update(); | |
3069 | rcu_read_unlock(); | |
3070 | return ret; | |
3071 | } | |
3072 | ||
3073 | /* | |
3074 | * Duplicate the ust data object of the ust app stream and save it in the | |
3075 | * buffer registry stream. | |
3076 | * | |
3077 | * Return 0 on success or else a negative value. | |
3078 | */ | |
3079 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
3080 | struct ust_app_stream *stream) | |
3081 | { | |
3082 | int ret; | |
3083 | ||
a0377dfe FD |
3084 | LTTNG_ASSERT(reg_stream); |
3085 | LTTNG_ASSERT(stream); | |
7972aab2 | 3086 | |
86ba1e22 | 3087 | /* Duplicating a stream requires 2 new fds. Reserve them. */ |
7972aab2 DG |
3088 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
3089 | if (ret < 0) { | |
3090 | ERR("Exhausted number of available FD upon duplicate stream"); | |
3091 | goto error; | |
3092 | } | |
3093 | ||
3094 | /* Duplicate object for stream once the original is in the registry. */ | |
b623cb6a | 3095 | ret = lttng_ust_ctl_duplicate_ust_object_data(&stream->obj, |
7972aab2 DG |
3096 | reg_stream->obj.ust); |
3097 | if (ret < 0) { | |
3098 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
3099 | reg_stream->obj.ust, stream->obj, ret); | |
3100 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
3101 | goto error; | |
3102 | } | |
3103 | stream->handle = stream->obj->handle; | |
3104 | ||
3105 | error: | |
3106 | return ret; | |
3107 | } | |
3108 | ||
3109 | /* | |
3110 | * Duplicate the ust data object of the ust app. channel and save it in the | |
3111 | * buffer registry channel. | |
3112 | * | |
3113 | * Return 0 on success or else a negative value. | |
3114 | */ | |
3273699d | 3115 | static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
3116 | struct ust_app_channel *ua_chan) |
3117 | { | |
3118 | int ret; | |
3119 | ||
a0377dfe FD |
3120 | LTTNG_ASSERT(buf_reg_chan); |
3121 | LTTNG_ASSERT(ua_chan); | |
7972aab2 | 3122 | |
86ba1e22 | 3123 | /* Duplicating a channel requires 1 new fd. Reserve it. */ |
7972aab2 DG |
3124 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
3125 | if (ret < 0) { | |
3126 | ERR("Exhausted number of available FD upon duplicate channel"); | |
3127 | goto error_fd_get; | |
3128 | } | |
3129 | ||
3130 | /* Duplicate object for stream once the original is in the registry. */ | |
b623cb6a | 3131 | ret = lttng_ust_ctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust); |
7972aab2 DG |
3132 | if (ret < 0) { |
3133 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
3273699d | 3134 | buf_reg_chan->obj.ust, ua_chan->obj, ret); |
7972aab2 DG |
3135 | goto error; |
3136 | } | |
3137 | ua_chan->handle = ua_chan->obj->handle; | |
3138 | ||
3139 | return 0; | |
3140 | ||
3141 | error: | |
3142 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3143 | error_fd_get: | |
3144 | return ret; | |
3145 | } | |
3146 | ||
3147 | /* | |
3148 | * For a given channel buffer registry, setup all streams of the given ust | |
3149 | * application channel. | |
3150 | * | |
3151 | * Return 0 on success or else a negative value. | |
3152 | */ | |
3273699d | 3153 | static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan, |
fb45065e MD |
3154 | struct ust_app_channel *ua_chan, |
3155 | struct ust_app *app) | |
7972aab2 DG |
3156 | { |
3157 | int ret = 0; | |
3158 | struct ust_app_stream *stream, *stmp; | |
3159 | ||
a0377dfe FD |
3160 | LTTNG_ASSERT(buf_reg_chan); |
3161 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3162 | |
3163 | DBG2("UST app setup buffer registry stream"); | |
3164 | ||
3165 | /* Send all streams to application. */ | |
3166 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
3167 | struct buffer_reg_stream *reg_stream; | |
3168 | ||
3169 | ret = buffer_reg_stream_create(®_stream); | |
3170 | if (ret < 0) { | |
3171 | goto error; | |
3172 | } | |
3173 | ||
3174 | /* | |
3175 | * Keep original pointer and nullify it in the stream so the delete | |
3176 | * stream call does not release the object. | |
3177 | */ | |
3178 | reg_stream->obj.ust = stream->obj; | |
3179 | stream->obj = NULL; | |
3273699d | 3180 | buffer_reg_stream_add(reg_stream, buf_reg_chan); |
421cb601 | 3181 | |
7972aab2 DG |
3182 | /* We don't need the streams anymore. */ |
3183 | cds_list_del(&stream->list); | |
fb45065e | 3184 | delete_ust_app_stream(-1, stream, app); |
7972aab2 | 3185 | } |
421cb601 | 3186 | |
7972aab2 DG |
3187 | error: |
3188 | return ret; | |
3189 | } | |
3190 | ||
3191 | /* | |
3192 | * Create a buffer registry channel for the given session registry and | |
3193 | * application channel object. If regp pointer is valid, it's set with the | |
3194 | * created object. Important, the created object is NOT added to the session | |
3195 | * registry hash table. | |
3196 | * | |
3197 | * Return 0 on success else a negative value. | |
3198 | */ | |
3199 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
3200 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
3201 | { | |
3202 | int ret; | |
3273699d | 3203 | struct buffer_reg_channel *buf_reg_chan = NULL; |
7972aab2 | 3204 | |
a0377dfe FD |
3205 | LTTNG_ASSERT(reg_sess); |
3206 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3207 | |
3208 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
3209 | ||
3210 | /* Create buffer registry channel. */ | |
3273699d | 3211 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan); |
7972aab2 DG |
3212 | if (ret < 0) { |
3213 | goto error_create; | |
421cb601 | 3214 | } |
a0377dfe | 3215 | LTTNG_ASSERT(buf_reg_chan); |
3273699d FD |
3216 | buf_reg_chan->consumer_key = ua_chan->key; |
3217 | buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size; | |
3218 | buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf; | |
421cb601 | 3219 | |
7972aab2 DG |
3220 | /* Create and add a channel registry to session. */ |
3221 | ret = ust_registry_channel_add(reg_sess->reg.ust, | |
3222 | ua_chan->tracing_channel_id); | |
3223 | if (ret < 0) { | |
3224 | goto error; | |
d88aee68 | 3225 | } |
3273699d | 3226 | buffer_reg_channel_add(reg_sess, buf_reg_chan); |
d88aee68 | 3227 | |
7972aab2 | 3228 | if (regp) { |
3273699d | 3229 | *regp = buf_reg_chan; |
3d8ca23b | 3230 | } |
d88aee68 | 3231 | |
7972aab2 | 3232 | return 0; |
3d8ca23b DG |
3233 | |
3234 | error: | |
7972aab2 | 3235 | /* Safe because the registry channel object was not added to any HT. */ |
3273699d | 3236 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
7972aab2 | 3237 | error_create: |
3d8ca23b | 3238 | return ret; |
421cb601 DG |
3239 | } |
3240 | ||
55cc08a6 | 3241 | /* |
7972aab2 DG |
3242 | * Setup buffer registry channel for the given session registry and application |
3243 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 3244 | * |
7972aab2 | 3245 | * Return 0 on success else a negative value. |
55cc08a6 | 3246 | */ |
7972aab2 | 3247 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
3273699d | 3248 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan, |
fb45065e | 3249 | struct ust_app *app) |
55cc08a6 | 3250 | { |
7972aab2 | 3251 | int ret; |
55cc08a6 | 3252 | |
a0377dfe FD |
3253 | LTTNG_ASSERT(reg_sess); |
3254 | LTTNG_ASSERT(buf_reg_chan); | |
3255 | LTTNG_ASSERT(ua_chan); | |
3256 | LTTNG_ASSERT(ua_chan->obj); | |
55cc08a6 | 3257 | |
7972aab2 | 3258 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 3259 | |
7972aab2 | 3260 | /* Setup all streams for the registry. */ |
3273699d | 3261 | ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app); |
7972aab2 | 3262 | if (ret < 0) { |
55cc08a6 DG |
3263 | goto error; |
3264 | } | |
3265 | ||
3273699d | 3266 | buf_reg_chan->obj.ust = ua_chan->obj; |
7972aab2 | 3267 | ua_chan->obj = NULL; |
55cc08a6 | 3268 | |
7972aab2 | 3269 | return 0; |
55cc08a6 DG |
3270 | |
3271 | error: | |
3273699d FD |
3272 | buffer_reg_channel_remove(reg_sess, buf_reg_chan); |
3273 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
3274 | return ret; |
3275 | } | |
3276 | ||
edb67388 | 3277 | /* |
7972aab2 | 3278 | * Send buffer registry channel to the application. |
d0b96690 | 3279 | * |
7972aab2 | 3280 | * Return 0 on success else a negative value. |
edb67388 | 3281 | */ |
3273699d | 3282 | static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
3283 | struct ust_app *app, struct ust_app_session *ua_sess, |
3284 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
3285 | { |
3286 | int ret; | |
7972aab2 | 3287 | struct buffer_reg_stream *reg_stream; |
edb67388 | 3288 | |
a0377dfe FD |
3289 | LTTNG_ASSERT(buf_reg_chan); |
3290 | LTTNG_ASSERT(app); | |
3291 | LTTNG_ASSERT(ua_sess); | |
3292 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3293 | |
3294 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
3295 | ||
3273699d | 3296 | ret = duplicate_channel_object(buf_reg_chan, ua_chan); |
edb67388 DG |
3297 | if (ret < 0) { |
3298 | goto error; | |
3299 | } | |
3300 | ||
7972aab2 DG |
3301 | /* Send channel to the application. */ |
3302 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
3303 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3304 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
3305 | goto error; | |
be355079 JR |
3306 | } else if (ret == -EAGAIN) { |
3307 | /* Caused by timeout. */ | |
3308 | WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64 "\".", | |
3309 | app->pid, ua_chan->name, ua_sess->tracing_id); | |
3310 | /* Treat this the same way as an application that is exiting. */ | |
3311 | ret = -ENOTCONN; | |
3312 | goto error; | |
a7169585 | 3313 | } else if (ret < 0) { |
7972aab2 DG |
3314 | goto error; |
3315 | } | |
3316 | ||
3317 | health_code_update(); | |
3318 | ||
3319 | /* Send all streams to application. */ | |
3273699d FD |
3320 | pthread_mutex_lock(&buf_reg_chan->stream_list_lock); |
3321 | cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) { | |
9ee61e74 | 3322 | struct ust_app_stream stream = {}; |
7972aab2 DG |
3323 | |
3324 | ret = duplicate_stream_object(reg_stream, &stream); | |
3325 | if (ret < 0) { | |
3326 | goto error_stream_unlock; | |
3327 | } | |
3328 | ||
3329 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
3330 | if (ret < 0) { | |
a7169585 MD |
3331 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
3332 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
be355079 JR |
3333 | } else if (ret == -EAGAIN) { |
3334 | /* | |
3335 | * Caused by timeout. | |
3336 | * Treat this the same way as an application | |
3337 | * that is exiting. | |
3338 | */ | |
9ee61e74 JG |
3339 | WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64 "\".", |
3340 | app->pid, | |
be355079 JR |
3341 | ua_chan->name, |
3342 | ua_sess->tracing_id); | |
3343 | ret = -ENOTCONN; | |
a7169585 | 3344 | } |
be355079 | 3345 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
3346 | goto error_stream_unlock; |
3347 | } | |
edb67388 | 3348 | |
7972aab2 DG |
3349 | /* |
3350 | * The return value is not important here. This function will output an | |
3351 | * error if needed. | |
3352 | */ | |
fb45065e | 3353 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
3354 | } |
3355 | ua_chan->is_sent = 1; | |
3356 | ||
3357 | error_stream_unlock: | |
3273699d | 3358 | pthread_mutex_unlock(&buf_reg_chan->stream_list_lock); |
edb67388 DG |
3359 | error: |
3360 | return ret; | |
3361 | } | |
3362 | ||
9730260e | 3363 | /* |
7972aab2 DG |
3364 | * Create and send to the application the created buffers with per UID buffers. |
3365 | * | |
9acdc1d6 | 3366 | * This MUST be called with a RCU read side lock acquired. |
71e0a100 | 3367 | * The session list lock and the session's lock must be acquired. |
9acdc1d6 | 3368 | * |
7972aab2 | 3369 | * Return 0 on success else a negative value. |
9730260e | 3370 | */ |
7972aab2 DG |
3371 | static int create_channel_per_uid(struct ust_app *app, |
3372 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3373 | struct ust_app_channel *ua_chan) | |
9730260e DG |
3374 | { |
3375 | int ret; | |
7972aab2 | 3376 | struct buffer_reg_uid *reg_uid; |
3273699d | 3377 | struct buffer_reg_channel *buf_reg_chan; |
e32d7f27 | 3378 | struct ltt_session *session = NULL; |
e098433c | 3379 | enum lttng_error_code notification_ret; |
3273699d | 3380 | struct ust_registry_channel *ust_reg_chan; |
9730260e | 3381 | |
a0377dfe FD |
3382 | LTTNG_ASSERT(app); |
3383 | LTTNG_ASSERT(usess); | |
3384 | LTTNG_ASSERT(ua_sess); | |
3385 | LTTNG_ASSERT(ua_chan); | |
48b7cdc2 | 3386 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 DG |
3387 | |
3388 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
3389 | ||
3390 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
3391 | /* | |
3392 | * The session creation handles the creation of this global registry | |
3393 | * object. If none can be find, there is a code flow problem or a | |
3394 | * teardown race. | |
3395 | */ | |
a0377dfe | 3396 | LTTNG_ASSERT(reg_uid); |
7972aab2 | 3397 | |
3273699d | 3398 | buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, |
7972aab2 | 3399 | reg_uid); |
3273699d | 3400 | if (buf_reg_chan) { |
2721f7ea JG |
3401 | goto send_channel; |
3402 | } | |
7972aab2 | 3403 | |
2721f7ea | 3404 | /* Create the buffer registry channel object. */ |
3273699d | 3405 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan); |
2721f7ea JG |
3406 | if (ret < 0) { |
3407 | ERR("Error creating the UST channel \"%s\" registry instance", | |
f14256d6 | 3408 | ua_chan->name); |
2721f7ea JG |
3409 | goto error; |
3410 | } | |
f14256d6 | 3411 | |
e098433c | 3412 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3413 | LTTNG_ASSERT(session); |
3130a40c JG |
3414 | ASSERT_LOCKED(session->lock); |
3415 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3416 | |
2721f7ea JG |
3417 | /* |
3418 | * Create the buffers on the consumer side. This call populates the | |
3419 | * ust app channel object with all streams and data object. | |
3420 | */ | |
3421 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
f46376a1 | 3422 | app->bits_per_long, reg_uid->registry->reg.ust); |
2721f7ea JG |
3423 | if (ret < 0) { |
3424 | ERR("Error creating UST channel \"%s\" on the consumer daemon", | |
3425 | ua_chan->name); | |
7972aab2 DG |
3426 | |
3427 | /* | |
2721f7ea JG |
3428 | * Let's remove the previously created buffer registry channel so |
3429 | * it's not visible anymore in the session registry. | |
7972aab2 | 3430 | */ |
2721f7ea JG |
3431 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, |
3432 | ua_chan->tracing_channel_id, false); | |
3273699d FD |
3433 | buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan); |
3434 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
2721f7ea | 3435 | goto error; |
7972aab2 DG |
3436 | } |
3437 | ||
2721f7ea JG |
3438 | /* |
3439 | * Setup the streams and add it to the session registry. | |
3440 | */ | |
3441 | ret = setup_buffer_reg_channel(reg_uid->registry, | |
3273699d | 3442 | ua_chan, buf_reg_chan, app); |
2721f7ea JG |
3443 | if (ret < 0) { |
3444 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); | |
3445 | goto error; | |
3446 | } | |
3447 | ||
e098433c | 3448 | /* Notify the notification subsystem of the channel's creation. */ |
aeeb48c6 | 3449 | pthread_mutex_lock(®_uid->registry->reg.ust->_lock); |
3273699d | 3450 | ust_reg_chan = ust_registry_channel_find(reg_uid->registry->reg.ust, |
e098433c | 3451 | ua_chan->tracing_channel_id); |
a0377dfe | 3452 | LTTNG_ASSERT(ust_reg_chan); |
3273699d FD |
3453 | ust_reg_chan->consumer_key = ua_chan->key; |
3454 | ust_reg_chan = NULL; | |
aeeb48c6 | 3455 | pthread_mutex_unlock(®_uid->registry->reg.ust->_lock); |
e9404c27 | 3456 | |
e098433c | 3457 | notification_ret = notification_thread_command_add_channel( |
412d7227 SM |
3458 | the_notification_thread_handle, session->name, |
3459 | lttng_credentials_get_uid( | |
3460 | &ua_sess->effective_credentials), | |
3461 | lttng_credentials_get_gid( | |
3462 | &ua_sess->effective_credentials), | |
3463 | ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, | |
e098433c JG |
3464 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3465 | if (notification_ret != LTTNG_OK) { | |
3466 | ret = - (int) notification_ret; | |
3467 | ERR("Failed to add channel to notification thread"); | |
3468 | goto error; | |
e9404c27 JG |
3469 | } |
3470 | ||
2721f7ea | 3471 | send_channel: |
66ff8e3f | 3472 | /* Send buffers to the application. */ |
3273699d | 3473 | ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan); |
66ff8e3f JG |
3474 | if (ret < 0) { |
3475 | if (ret != -ENOTCONN) { | |
3476 | ERR("Error sending channel to application"); | |
3477 | } | |
3478 | goto error; | |
3479 | } | |
3480 | ||
9730260e | 3481 | error: |
e32d7f27 JG |
3482 | if (session) { |
3483 | session_put(session); | |
3484 | } | |
9730260e DG |
3485 | return ret; |
3486 | } | |
3487 | ||
78f0bacd | 3488 | /* |
7972aab2 DG |
3489 | * Create and send to the application the created buffers with per PID buffers. |
3490 | * | |
fad1ed2f | 3491 | * Called with UST app session lock held. |
71e0a100 | 3492 | * The session list lock and the session's lock must be acquired. |
fad1ed2f | 3493 | * |
7972aab2 | 3494 | * Return 0 on success else a negative value. |
78f0bacd | 3495 | */ |
7972aab2 DG |
3496 | static int create_channel_per_pid(struct ust_app *app, |
3497 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
3498 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3499 | { |
8535a6d9 | 3500 | int ret; |
aeeb48c6 | 3501 | ust_registry_session *registry; |
e9404c27 | 3502 | enum lttng_error_code cmd_ret; |
e32d7f27 | 3503 | struct ltt_session *session = NULL; |
e9404c27 | 3504 | uint64_t chan_reg_key; |
3273699d | 3505 | struct ust_registry_channel *ust_reg_chan; |
78f0bacd | 3506 | |
a0377dfe FD |
3507 | LTTNG_ASSERT(app); |
3508 | LTTNG_ASSERT(usess); | |
3509 | LTTNG_ASSERT(ua_sess); | |
3510 | LTTNG_ASSERT(ua_chan); | |
7972aab2 DG |
3511 | |
3512 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
3513 | ||
3514 | rcu_read_lock(); | |
3515 | ||
3516 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 3517 | /* The UST app session lock is held, registry shall not be null. */ |
a0377dfe | 3518 | LTTNG_ASSERT(registry); |
7972aab2 DG |
3519 | |
3520 | /* Create and add a new channel registry to session. */ | |
3521 | ret = ust_registry_channel_add(registry, ua_chan->key); | |
78f0bacd | 3522 | if (ret < 0) { |
f14256d6 MD |
3523 | ERR("Error creating the UST channel \"%s\" registry instance", |
3524 | ua_chan->name); | |
78f0bacd DG |
3525 | goto error; |
3526 | } | |
3527 | ||
e098433c | 3528 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3529 | LTTNG_ASSERT(session); |
3130a40c JG |
3530 | ASSERT_LOCKED(session->lock); |
3531 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3532 | |
7972aab2 DG |
3533 | /* Create and get channel on the consumer side. */ |
3534 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
f46376a1 | 3535 | app->bits_per_long, registry); |
7972aab2 | 3536 | if (ret < 0) { |
f14256d6 MD |
3537 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
3538 | ua_chan->name); | |
5b951542 | 3539 | goto error_remove_from_registry; |
7972aab2 DG |
3540 | } |
3541 | ||
3542 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
3543 | if (ret < 0) { | |
a7169585 MD |
3544 | if (ret != -ENOTCONN) { |
3545 | ERR("Error sending channel to application"); | |
3546 | } | |
5b951542 | 3547 | goto error_remove_from_registry; |
7972aab2 | 3548 | } |
8535a6d9 | 3549 | |
e9404c27 | 3550 | chan_reg_key = ua_chan->key; |
aeeb48c6 | 3551 | pthread_mutex_lock(®istry->_lock); |
3273699d | 3552 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
a0377dfe | 3553 | LTTNG_ASSERT(ust_reg_chan); |
3273699d | 3554 | ust_reg_chan->consumer_key = ua_chan->key; |
aeeb48c6 | 3555 | pthread_mutex_unlock(®istry->_lock); |
e9404c27 JG |
3556 | |
3557 | cmd_ret = notification_thread_command_add_channel( | |
412d7227 SM |
3558 | the_notification_thread_handle, session->name, |
3559 | lttng_credentials_get_uid( | |
3560 | &ua_sess->effective_credentials), | |
3561 | lttng_credentials_get_gid( | |
3562 | &ua_sess->effective_credentials), | |
3563 | ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST, | |
e9404c27 JG |
3564 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
3565 | if (cmd_ret != LTTNG_OK) { | |
3566 | ret = - (int) cmd_ret; | |
3567 | ERR("Failed to add channel to notification thread"); | |
5b951542 | 3568 | goto error_remove_from_registry; |
e9404c27 JG |
3569 | } |
3570 | ||
5b951542 MD |
3571 | error_remove_from_registry: |
3572 | if (ret) { | |
3573 | ust_registry_channel_del_free(registry, ua_chan->key, false); | |
3574 | } | |
78f0bacd | 3575 | error: |
7972aab2 | 3576 | rcu_read_unlock(); |
e32d7f27 JG |
3577 | if (session) { |
3578 | session_put(session); | |
3579 | } | |
78f0bacd DG |
3580 | return ret; |
3581 | } | |
3582 | ||
3583 | /* | |
7972aab2 | 3584 | * From an already allocated ust app channel, create the channel buffers if |
88e3c2f5 | 3585 | * needed and send them to the application. This MUST be called with a RCU read |
7972aab2 DG |
3586 | * side lock acquired. |
3587 | * | |
fad1ed2f JR |
3588 | * Called with UST app session lock held. |
3589 | * | |
a7169585 MD |
3590 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
3591 | * the application exited concurrently. | |
78f0bacd | 3592 | */ |
88e3c2f5 | 3593 | static int ust_app_channel_send(struct ust_app *app, |
7972aab2 DG |
3594 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
3595 | struct ust_app_channel *ua_chan) | |
78f0bacd | 3596 | { |
7972aab2 | 3597 | int ret; |
78f0bacd | 3598 | |
a0377dfe FD |
3599 | LTTNG_ASSERT(app); |
3600 | LTTNG_ASSERT(usess); | |
3601 | LTTNG_ASSERT(usess->active); | |
3602 | LTTNG_ASSERT(ua_sess); | |
3603 | LTTNG_ASSERT(ua_chan); | |
48b7cdc2 | 3604 | ASSERT_RCU_READ_LOCKED(); |
7972aab2 DG |
3605 | |
3606 | /* Handle buffer type before sending the channel to the application. */ | |
3607 | switch (usess->buffer_type) { | |
3608 | case LTTNG_BUFFER_PER_UID: | |
3609 | { | |
3610 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
3611 | if (ret < 0) { | |
3612 | goto error; | |
3613 | } | |
3614 | break; | |
3615 | } | |
3616 | case LTTNG_BUFFER_PER_PID: | |
3617 | { | |
3618 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
3619 | if (ret < 0) { | |
3620 | goto error; | |
3621 | } | |
3622 | break; | |
3623 | } | |
3624 | default: | |
a0377dfe | 3625 | abort(); |
7972aab2 | 3626 | ret = -EINVAL; |
78f0bacd DG |
3627 | goto error; |
3628 | } | |
3629 | ||
7972aab2 DG |
3630 | /* Initialize ust objd object using the received handle and add it. */ |
3631 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
3632 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 3633 | |
7972aab2 DG |
3634 | /* If channel is not enabled, disable it on the tracer */ |
3635 | if (!ua_chan->enabled) { | |
3636 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
3637 | if (ret < 0) { | |
3638 | goto error; | |
3639 | } | |
78f0bacd DG |
3640 | } |
3641 | ||
3642 | error: | |
3643 | return ret; | |
3644 | } | |
3645 | ||
284d8f55 | 3646 | /* |
88e3c2f5 | 3647 | * Create UST app channel and return it through ua_chanp if not NULL. |
d0b96690 | 3648 | * |
36b588ed | 3649 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 3650 | * |
88e3c2f5 | 3651 | * Return 0 on success or else a negative value. |
284d8f55 | 3652 | */ |
88e3c2f5 JG |
3653 | static int ust_app_channel_allocate(struct ust_app_session *ua_sess, |
3654 | struct ltt_ust_channel *uchan, | |
f46376a1 MJ |
3655 | enum lttng_ust_abi_chan_type type, |
3656 | struct ltt_ust_session *usess __attribute__((unused)), | |
4d710ac2 | 3657 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
3658 | { |
3659 | int ret = 0; | |
bec39940 DG |
3660 | struct lttng_ht_iter iter; |
3661 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
3662 | struct ust_app_channel *ua_chan; |
3663 | ||
48b7cdc2 FD |
3664 | ASSERT_RCU_READ_LOCKED(); |
3665 | ||
5b4a0ec0 | 3666 | /* Lookup channel in the ust app session */ |
bec39940 DG |
3667 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
3668 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 3669 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 3670 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 3671 | goto end; |
5b4a0ec0 DG |
3672 | } |
3673 | ||
d0b96690 | 3674 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
3675 | if (ua_chan == NULL) { |
3676 | /* Only malloc can fail here */ | |
4d710ac2 | 3677 | ret = -ENOMEM; |
88e3c2f5 | 3678 | goto error; |
fc34caaa DG |
3679 | } |
3680 | shadow_copy_channel(ua_chan, uchan); | |
3681 | ||
ffe60014 DG |
3682 | /* Set channel type. */ |
3683 | ua_chan->attr.type = type; | |
3684 | ||
d0b96690 DG |
3685 | /* Only add the channel if successful on the tracer side. */ |
3686 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
fc34caaa | 3687 | end: |
4d710ac2 DG |
3688 | if (ua_chanp) { |
3689 | *ua_chanp = ua_chan; | |
3690 | } | |
3691 | ||
3692 | /* Everything went well. */ | |
3693 | return 0; | |
5b4a0ec0 DG |
3694 | |
3695 | error: | |
4d710ac2 | 3696 | return ret; |
5b4a0ec0 DG |
3697 | } |
3698 | ||
3699 | /* | |
3700 | * Create UST app event and create it on the tracer side. | |
d0b96690 | 3701 | * |
993578ff | 3702 | * Must be called with the RCU read side lock held. |
d0b96690 | 3703 | * Called with ust app session mutex held. |
5b4a0ec0 | 3704 | */ |
edb67388 | 3705 | static |
f46376a1 MJ |
3706 | int create_ust_app_event(struct ust_app_channel *ua_chan, |
3707 | struct ltt_ust_event *uevent, | |
edb67388 | 3708 | struct ust_app *app) |
284d8f55 | 3709 | { |
edb67388 | 3710 | int ret = 0; |
5b4a0ec0 | 3711 | struct ust_app_event *ua_event; |
284d8f55 | 3712 | |
48b7cdc2 FD |
3713 | ASSERT_RCU_READ_LOCKED(); |
3714 | ||
edb67388 DG |
3715 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
3716 | if (ua_event == NULL) { | |
20533947 | 3717 | /* Only failure mode of alloc_ust_app_event(). */ |
edb67388 | 3718 | ret = -ENOMEM; |
fc34caaa | 3719 | goto end; |
5b4a0ec0 | 3720 | } |
edb67388 | 3721 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 3722 | |
edb67388 | 3723 | /* Create it on the tracer side */ |
f46376a1 | 3724 | ret = create_ust_event(app, ua_chan, ua_event); |
284d8f55 | 3725 | if (ret < 0) { |
e9f11505 JG |
3726 | /* |
3727 | * Not found previously means that it does not exist on the | |
3728 | * tracer. If the application reports that the event existed, | |
3729 | * it means there is a bug in the sessiond or lttng-ust | |
3730 | * (or corruption, etc.) | |
3731 | */ | |
3732 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3733 | ERR("Tracer for application reported that an event being created already existed: " | |
3734 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
3735 | uevent->attr.name, | |
3736 | app->pid, app->ppid, app->uid, | |
3737 | app->gid); | |
3738 | } | |
284d8f55 DG |
3739 | goto error; |
3740 | } | |
3741 | ||
d0b96690 | 3742 | add_unique_ust_app_event(ua_chan, ua_event); |
284d8f55 | 3743 | |
be355079 JR |
3744 | DBG2("UST app create event completed: app = '%s' pid = %d", |
3745 | app->name, app->pid); | |
7f79d3a1 | 3746 | |
edb67388 | 3747 | end: |
fc34caaa DG |
3748 | return ret; |
3749 | ||
5b4a0ec0 | 3750 | error: |
fc34caaa | 3751 | /* Valid. Calling here is already in a read side lock */ |
fb45065e | 3752 | delete_ust_app_event(-1, ua_event, app); |
edb67388 | 3753 | return ret; |
5b4a0ec0 DG |
3754 | } |
3755 | ||
993578ff JR |
3756 | /* |
3757 | * Create UST app event notifier rule and create it on the tracer side. | |
3758 | * | |
3759 | * Must be called with the RCU read side lock held. | |
3760 | * Called with ust app session mutex held. | |
3761 | */ | |
3762 | static | |
267d66aa JR |
3763 | int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, |
3764 | struct ust_app *app) | |
993578ff JR |
3765 | { |
3766 | int ret = 0; | |
3767 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
3768 | ||
48b7cdc2 FD |
3769 | ASSERT_RCU_READ_LOCKED(); |
3770 | ||
267d66aa | 3771 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
993578ff JR |
3772 | if (ua_event_notifier_rule == NULL) { |
3773 | ret = -ENOMEM; | |
3774 | goto end; | |
3775 | } | |
3776 | ||
3777 | /* Create it on the tracer side. */ | |
3778 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); | |
3779 | if (ret < 0) { | |
3780 | /* | |
3781 | * Not found previously means that it does not exist on the | |
3782 | * tracer. If the application reports that the event existed, | |
3783 | * it means there is a bug in the sessiond or lttng-ust | |
3784 | * (or corruption, etc.) | |
3785 | */ | |
3786 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
3787 | ERR("Tracer for application reported that an event notifier being created already exists: " | |
3788 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
267d66aa | 3789 | lttng_trigger_get_tracer_token(trigger), |
993578ff JR |
3790 | app->pid, app->ppid, app->uid, |
3791 | app->gid); | |
3792 | } | |
3793 | goto error; | |
3794 | } | |
3795 | ||
3796 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, | |
3797 | &ua_event_notifier_rule->node); | |
3798 | ||
9324443a | 3799 | DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64, |
be355079 | 3800 | app->name, app->pid, lttng_trigger_get_tracer_token(trigger)); |
993578ff | 3801 | |
533a90fb | 3802 | goto end; |
993578ff JR |
3803 | |
3804 | error: | |
3805 | /* The RCU read side lock is already being held by the caller. */ | |
3806 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); | |
533a90fb | 3807 | end: |
993578ff JR |
3808 | return ret; |
3809 | } | |
3810 | ||
5b4a0ec0 DG |
3811 | /* |
3812 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 3813 | * |
7972aab2 | 3814 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
3815 | */ |
3816 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 3817 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
3818 | { |
3819 | int ret = 0; | |
ffe60014 | 3820 | struct ust_app_channel *metadata; |
d88aee68 | 3821 | struct consumer_socket *socket; |
aeeb48c6 | 3822 | ust_registry_session *registry; |
e32d7f27 | 3823 | struct ltt_session *session = NULL; |
5b4a0ec0 | 3824 | |
a0377dfe FD |
3825 | LTTNG_ASSERT(ua_sess); |
3826 | LTTNG_ASSERT(app); | |
3827 | LTTNG_ASSERT(consumer); | |
48b7cdc2 | 3828 | ASSERT_RCU_READ_LOCKED(); |
5b4a0ec0 | 3829 | |
7972aab2 | 3830 | registry = get_session_registry(ua_sess); |
fad1ed2f | 3831 | /* The UST app session is held registry shall not be null. */ |
a0377dfe | 3832 | LTTNG_ASSERT(registry); |
7972aab2 | 3833 | |
aeeb48c6 | 3834 | pthread_mutex_lock(®istry->_lock); |
ce34fcd0 | 3835 | |
1b532a60 | 3836 | /* Metadata already exists for this registry or it was closed previously */ |
aeeb48c6 | 3837 | if (registry->_metadata_key || registry->_metadata_closed) { |
7972aab2 DG |
3838 | ret = 0; |
3839 | goto error; | |
5b4a0ec0 DG |
3840 | } |
3841 | ||
ffe60014 | 3842 | /* Allocate UST metadata */ |
d0b96690 | 3843 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
3844 | if (!metadata) { |
3845 | /* malloc() failed */ | |
3846 | ret = -ENOMEM; | |
3847 | goto error; | |
3848 | } | |
5b4a0ec0 | 3849 | |
ad7a9107 | 3850 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 3851 | |
7972aab2 DG |
3852 | /* Need one fd for the channel. */ |
3853 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3854 | if (ret < 0) { | |
3855 | ERR("Exhausted number of available FD upon create metadata"); | |
3856 | goto error; | |
3857 | } | |
3858 | ||
4dc3dfc5 DG |
3859 | /* Get the right consumer socket for the application. */ |
3860 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); | |
3861 | if (!socket) { | |
3862 | ret = -EINVAL; | |
3863 | goto error_consumer; | |
3864 | } | |
3865 | ||
331744e3 JD |
3866 | /* |
3867 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
3868 | * to the registry *before* we ask the consumer so we avoid the race of the | |
3869 | * consumer requesting the metadata and the ask_channel call on our side | |
3870 | * did not returned yet. | |
3871 | */ | |
aeeb48c6 | 3872 | registry->_metadata_key = metadata->key; |
331744e3 | 3873 | |
e098433c | 3874 | session = session_find_by_id(ua_sess->tracing_id); |
a0377dfe | 3875 | LTTNG_ASSERT(session); |
3130a40c JG |
3876 | ASSERT_LOCKED(session->lock); |
3877 | ASSERT_SESSION_LIST_LOCKED(); | |
e098433c | 3878 | |
d88aee68 DG |
3879 | /* |
3880 | * Ask the metadata channel creation to the consumer. The metadata object | |
3881 | * will be created by the consumer and kept their. However, the stream is | |
3882 | * never added or monitored until we do a first push metadata to the | |
3883 | * consumer. | |
3884 | */ | |
7972aab2 | 3885 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
d2956687 | 3886 | registry, session->current_trace_chunk); |
d88aee68 | 3887 | if (ret < 0) { |
f2a444f1 | 3888 | /* Nullify the metadata key so we don't try to close it later on. */ |
aeeb48c6 | 3889 | registry->_metadata_key = 0; |
d88aee68 DG |
3890 | goto error_consumer; |
3891 | } | |
3892 | ||
3893 | /* | |
3894 | * The setup command will make the metadata stream be sent to the relayd, | |
3895 | * if applicable, and the thread managing the metadatas. This is important | |
3896 | * because after this point, if an error occurs, the only way the stream | |
3897 | * can be deleted is to be monitored in the consumer. | |
3898 | */ | |
7972aab2 | 3899 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 3900 | if (ret < 0) { |
f2a444f1 | 3901 | /* Nullify the metadata key so we don't try to close it later on. */ |
aeeb48c6 | 3902 | registry->_metadata_key = 0; |
d88aee68 | 3903 | goto error_consumer; |
5b4a0ec0 DG |
3904 | } |
3905 | ||
7972aab2 DG |
3906 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
3907 | metadata->key, app->pid); | |
5b4a0ec0 | 3908 | |
d88aee68 | 3909 | error_consumer: |
b80f0b6c | 3910 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 3911 | delete_ust_app_channel(-1, metadata, app); |
5b4a0ec0 | 3912 | error: |
aeeb48c6 | 3913 | pthread_mutex_unlock(®istry->_lock); |
e32d7f27 JG |
3914 | if (session) { |
3915 | session_put(session); | |
3916 | } | |
ffe60014 | 3917 | return ret; |
5b4a0ec0 DG |
3918 | } |
3919 | ||
5b4a0ec0 | 3920 | /* |
d88aee68 DG |
3921 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
3922 | * acquired before calling this function. | |
5b4a0ec0 DG |
3923 | */ |
3924 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
3925 | { | |
d88aee68 | 3926 | struct ust_app *app = NULL; |
bec39940 DG |
3927 | struct lttng_ht_node_ulong *node; |
3928 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 3929 | |
bec39940 DG |
3930 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
3931 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
3932 | if (node == NULL) { |
3933 | DBG2("UST app no found with pid %d", pid); | |
3934 | goto error; | |
3935 | } | |
5b4a0ec0 DG |
3936 | |
3937 | DBG2("Found UST app by pid %d", pid); | |
3938 | ||
d88aee68 | 3939 | app = caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
3940 | |
3941 | error: | |
d88aee68 | 3942 | return app; |
5b4a0ec0 DG |
3943 | } |
3944 | ||
d88aee68 DG |
3945 | /* |
3946 | * Allocate and init an UST app object using the registration information and | |
3947 | * the command socket. This is called when the command socket connects to the | |
3948 | * session daemon. | |
3949 | * | |
3950 | * The object is returned on success or else NULL. | |
3951 | */ | |
d0b96690 | 3952 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 3953 | { |
5e2abfaf | 3954 | int ret; |
d0b96690 | 3955 | struct ust_app *lta = NULL; |
da873412 | 3956 | struct lttng_pipe *event_notifier_event_source_pipe = NULL; |
d0b96690 | 3957 | |
a0377dfe FD |
3958 | LTTNG_ASSERT(msg); |
3959 | LTTNG_ASSERT(sock >= 0); | |
d0b96690 DG |
3960 | |
3961 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 3962 | |
173af62f | 3963 | if ((msg->bits_per_long == 64 && |
412d7227 SM |
3964 | (uatomic_read(&the_ust_consumerd64_fd) == |
3965 | -EINVAL)) || | |
3966 | (msg->bits_per_long == 32 && | |
3967 | (uatomic_read(&the_ust_consumerd32_fd) == | |
3968 | -EINVAL))) { | |
f943b0fb | 3969 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
3970 | "%d-bit long, but no consumerd for this size is available.\n", |
3971 | msg->name, msg->pid, msg->bits_per_long); | |
3972 | goto error; | |
3f2c5fcc | 3973 | } |
d0b96690 | 3974 | |
5e2abfaf JG |
3975 | /* |
3976 | * Reserve the two file descriptors of the event source pipe. The write | |
3977 | * end will be closed once it is passed to the application, at which | |
3978 | * point a single 'put' will be performed. | |
3979 | */ | |
3980 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
3981 | if (ret) { | |
be355079 JR |
3982 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d", |
3983 | msg->name, (int) msg->pid); | |
5e2abfaf JG |
3984 | goto error; |
3985 | } | |
3986 | ||
da873412 JR |
3987 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
3988 | if (!event_notifier_event_source_pipe) { | |
be355079 JR |
3989 | PERROR("Failed to open application event source pipe: '%s' (pid = %d)", |
3990 | msg->name, msg->pid); | |
da873412 JR |
3991 | goto error; |
3992 | } | |
3993 | ||
64803277 | 3994 | lta = zmalloc<ust_app>(); |
5b4a0ec0 DG |
3995 | if (lta == NULL) { |
3996 | PERROR("malloc"); | |
da873412 | 3997 | goto error_free_pipe; |
5b4a0ec0 DG |
3998 | } |
3999 | ||
da873412 JR |
4000 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
4001 | ||
5b4a0ec0 DG |
4002 | lta->ppid = msg->ppid; |
4003 | lta->uid = msg->uid; | |
4004 | lta->gid = msg->gid; | |
d0b96690 | 4005 | |
7753dea8 | 4006 | lta->bits_per_long = msg->bits_per_long; |
d0b96690 DG |
4007 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
4008 | lta->uint16_t_alignment = msg->uint16_t_alignment; | |
4009 | lta->uint32_t_alignment = msg->uint32_t_alignment; | |
4010 | lta->uint64_t_alignment = msg->uint64_t_alignment; | |
4011 | lta->long_alignment = msg->long_alignment; | |
4012 | lta->byte_order = msg->byte_order; | |
4013 | ||
5b4a0ec0 DG |
4014 | lta->v_major = msg->major; |
4015 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 4016 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d0b96690 | 4017 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
10b56aef | 4018 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
d0b96690 | 4019 | lta->notify_sock = -1; |
993578ff | 4020 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d88aee68 DG |
4021 | |
4022 | /* Copy name and make sure it's NULL terminated. */ | |
4023 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
4024 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
4025 | ||
4026 | /* | |
4027 | * Before this can be called, when receiving the registration information, | |
4028 | * the application compatibility is checked. So, at this point, the | |
4029 | * application can work with this session daemon. | |
4030 | */ | |
d0b96690 | 4031 | lta->compatible = 1; |
5b4a0ec0 | 4032 | |
852d0037 | 4033 | lta->pid = msg->pid; |
d0b96690 | 4034 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 4035 | lta->sock = sock; |
fb45065e | 4036 | pthread_mutex_init(<a->sock_lock, NULL); |
d0b96690 | 4037 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 4038 | |
d42f20df | 4039 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
d0b96690 | 4040 | return lta; |
da873412 JR |
4041 | |
4042 | error_free_pipe: | |
4043 | lttng_pipe_destroy(event_notifier_event_source_pipe); | |
5e2abfaf | 4044 | lttng_fd_put(LTTNG_FD_APPS, 2); |
da873412 JR |
4045 | error: |
4046 | return NULL; | |
d0b96690 DG |
4047 | } |
4048 | ||
d88aee68 DG |
4049 | /* |
4050 | * For a given application object, add it to every hash table. | |
4051 | */ | |
d0b96690 DG |
4052 | void ust_app_add(struct ust_app *app) |
4053 | { | |
a0377dfe FD |
4054 | LTTNG_ASSERT(app); |
4055 | LTTNG_ASSERT(app->notify_sock >= 0); | |
d0b96690 | 4056 | |
940c4592 JR |
4057 | app->registration_time = time(NULL); |
4058 | ||
5b4a0ec0 | 4059 | rcu_read_lock(); |
852d0037 DG |
4060 | |
4061 | /* | |
4062 | * On a re-registration, we want to kick out the previous registration of | |
4063 | * that pid | |
4064 | */ | |
d0b96690 | 4065 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
4066 | |
4067 | /* | |
4068 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
4069 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
4070 | * already in the table. | |
4071 | */ | |
d0b96690 | 4072 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 4073 | |
d0b96690 DG |
4074 | /* Add application to the notify socket hash table. */ |
4075 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
4076 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 4077 | |
be355079 JR |
4078 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s " |
4079 | "notify_sock =%d (version %d.%d)", app->pid, app->ppid, app->uid, | |
d88aee68 DG |
4080 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, |
4081 | app->v_minor); | |
5b4a0ec0 | 4082 | |
d0b96690 DG |
4083 | rcu_read_unlock(); |
4084 | } | |
4085 | ||
d88aee68 DG |
4086 | /* |
4087 | * Set the application version into the object. | |
4088 | * | |
4089 | * Return 0 on success else a negative value either an errno code or a | |
4090 | * LTTng-UST error code. | |
4091 | */ | |
d0b96690 DG |
4092 | int ust_app_version(struct ust_app *app) |
4093 | { | |
d88aee68 DG |
4094 | int ret; |
4095 | ||
a0377dfe | 4096 | LTTNG_ASSERT(app); |
d88aee68 | 4097 | |
fb45065e | 4098 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4099 | ret = lttng_ust_ctl_tracer_version(app->sock, &app->version); |
fb45065e | 4100 | pthread_mutex_unlock(&app->sock_lock); |
d88aee68 | 4101 | if (ret < 0) { |
be355079 JR |
4102 | if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) { |
4103 | DBG3("UST app version failed. Application is dead: pid = %d, sock = %d", | |
4104 | app->pid, app->sock); | |
4105 | } else if (ret == -EAGAIN) { | |
4106 | WARN("UST app version failed. Communication time out: pid = %d, sock = %d", | |
4107 | app->pid, app->sock); | |
d88aee68 | 4108 | } else { |
be355079 JR |
4109 | ERR("UST app version failed with ret %d: pid = %d, sock = %d", |
4110 | ret, app->pid, app->sock); | |
d88aee68 DG |
4111 | } |
4112 | } | |
4113 | ||
4114 | return ret; | |
5b4a0ec0 DG |
4115 | } |
4116 | ||
783db316 MD |
4117 | bool ust_app_supports_notifiers(const struct ust_app *app) |
4118 | { | |
4119 | return app->v_major >= 9; | |
4120 | } | |
4121 | ||
4122 | bool ust_app_supports_counters(const struct ust_app *app) | |
4123 | { | |
4124 | return app->v_major >= 9; | |
4125 | } | |
4126 | ||
da873412 JR |
4127 | /* |
4128 | * Setup the base event notifier group. | |
4129 | * | |
4130 | * Return 0 on success else a negative value either an errno code or a | |
4131 | * LTTng-UST error code. | |
4132 | */ | |
4133 | int ust_app_setup_event_notifier_group(struct ust_app *app) | |
4134 | { | |
4135 | int ret; | |
4136 | int event_pipe_write_fd; | |
fc4b93fa | 4137 | struct lttng_ust_abi_object_data *event_notifier_group = NULL; |
da873412 | 4138 | enum lttng_error_code lttng_ret; |
533a90fb | 4139 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
da873412 | 4140 | |
a0377dfe | 4141 | LTTNG_ASSERT(app); |
da873412 | 4142 | |
783db316 MD |
4143 | if (!ust_app_supports_notifiers(app)) { |
4144 | ret = -ENOSYS; | |
4145 | goto error; | |
4146 | } | |
4147 | ||
da873412 JR |
4148 | /* Get the write side of the pipe. */ |
4149 | event_pipe_write_fd = lttng_pipe_get_writefd( | |
4150 | app->event_notifier_group.event_pipe); | |
4151 | ||
4152 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 4153 | ret = lttng_ust_ctl_create_event_notifier_group(app->sock, |
da873412 JR |
4154 | event_pipe_write_fd, &event_notifier_group); |
4155 | pthread_mutex_unlock(&app->sock_lock); | |
4156 | if (ret < 0) { | |
be355079 JR |
4157 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
4158 | ret = 0; | |
4159 | DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d", | |
4160 | app->pid, app->sock); | |
4161 | } else if (ret == -EAGAIN) { | |
4162 | ret = 0; | |
4163 | WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d", | |
4164 | app->pid, app->sock); | |
da873412 | 4165 | } else { |
be355079 JR |
4166 | ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d", |
4167 | ret, app->pid, app->sock, event_pipe_write_fd); | |
da873412 | 4168 | } |
da873412 JR |
4169 | goto error; |
4170 | } | |
4171 | ||
5d4193fd JG |
4172 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
4173 | if (ret) { | |
be355079 JR |
4174 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)", |
4175 | app->name, app->pid); | |
5d4193fd JG |
4176 | goto error; |
4177 | } | |
4178 | ||
5e2abfaf JG |
4179 | /* |
4180 | * Release the file descriptor that was reserved for the write-end of | |
4181 | * the pipe. | |
4182 | */ | |
4183 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
4184 | ||
da873412 | 4185 | lttng_ret = notification_thread_command_add_tracer_event_source( |
412d7227 SM |
4186 | the_notification_thread_handle, |
4187 | lttng_pipe_get_readfd( | |
4188 | app->event_notifier_group.event_pipe), | |
da873412 JR |
4189 | LTTNG_DOMAIN_UST); |
4190 | if (lttng_ret != LTTNG_OK) { | |
4191 | ERR("Failed to add tracer event source to notification thread"); | |
4192 | ret = - 1; | |
4193 | goto error; | |
4194 | } | |
4195 | ||
4196 | /* Assign handle only when the complete setup is valid. */ | |
4197 | app->event_notifier_group.object = event_notifier_group; | |
533a90fb | 4198 | |
a5a21280 FD |
4199 | event_notifier_error_accounting_status = |
4200 | event_notifier_error_accounting_register_app(app); | |
783db316 MD |
4201 | switch (event_notifier_error_accounting_status) { |
4202 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK: | |
4203 | break; | |
4204 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED: | |
be355079 JR |
4205 | 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", |
4206 | app->sock, app->name, (int) app->pid); | |
783db316 MD |
4207 | ret = 0; |
4208 | goto error_accounting; | |
4209 | case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD: | |
be355079 JR |
4210 | DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d", |
4211 | app->sock, app->name, (int) app->pid); | |
783db316 MD |
4212 | ret = 0; |
4213 | goto error_accounting; | |
4214 | default: | |
533a90fb FD |
4215 | ERR("Failed to setup event notifier error accounting for app"); |
4216 | ret = -1; | |
cd9c532c | 4217 | goto error_accounting; |
533a90fb FD |
4218 | } |
4219 | ||
da873412 JR |
4220 | return ret; |
4221 | ||
cd9c532c FD |
4222 | error_accounting: |
4223 | lttng_ret = notification_thread_command_remove_tracer_event_source( | |
4224 | the_notification_thread_handle, | |
4225 | lttng_pipe_get_readfd( | |
4226 | app->event_notifier_group.event_pipe)); | |
4227 | if (lttng_ret != LTTNG_OK) { | |
4228 | ERR("Failed to remove application tracer event source from notification thread"); | |
4229 | } | |
4230 | ||
da873412 | 4231 | error: |
b623cb6a | 4232 | lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object); |
da873412 | 4233 | free(app->event_notifier_group.object); |
88631abd | 4234 | app->event_notifier_group.object = NULL; |
da873412 JR |
4235 | return ret; |
4236 | } | |
4237 | ||
5b4a0ec0 DG |
4238 | /* |
4239 | * Unregister app by removing it from the global traceable app list and freeing | |
4240 | * the data struct. | |
4241 | * | |
4242 | * The socket is already closed at this point so no close to sock. | |
4243 | */ | |
4244 | void ust_app_unregister(int sock) | |
4245 | { | |
4246 | struct ust_app *lta; | |
bec39940 | 4247 | struct lttng_ht_node_ulong *node; |
c4b88406 | 4248 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 4249 | struct lttng_ht_iter iter; |
d42f20df | 4250 | struct ust_app_session *ua_sess; |
525b0740 | 4251 | int ret; |
5b4a0ec0 DG |
4252 | |
4253 | rcu_read_lock(); | |
886459c6 | 4254 | |
5b4a0ec0 | 4255 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
4256 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
4257 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
a0377dfe | 4258 | LTTNG_ASSERT(node); |
284d8f55 | 4259 | |
852d0037 | 4260 | lta = caa_container_of(node, struct ust_app, sock_n); |
852d0037 DG |
4261 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
4262 | ||
d88aee68 | 4263 | /* |
ce34fcd0 MD |
4264 | * For per-PID buffers, perform "push metadata" and flush all |
4265 | * application streams before removing app from hash tables, | |
4266 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 4267 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 4268 | */ |
d42f20df DG |
4269 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
4270 | node.node) { | |
aeeb48c6 | 4271 | ust_registry_session *registry; |
7972aab2 | 4272 | |
d42f20df DG |
4273 | ret = lttng_ht_del(lta->sessions, &iter); |
4274 | if (ret) { | |
4275 | /* The session was already removed so scheduled for teardown. */ | |
4276 | continue; | |
4277 | } | |
4278 | ||
ce34fcd0 MD |
4279 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
4280 | (void) ust_app_flush_app_session(lta, ua_sess); | |
4281 | } | |
c4b88406 | 4282 | |
d42f20df DG |
4283 | /* |
4284 | * Add session to list for teardown. This is safe since at this point we | |
4285 | * are the only one using this list. | |
4286 | */ | |
d88aee68 DG |
4287 | pthread_mutex_lock(&ua_sess->lock); |
4288 | ||
b161602a MD |
4289 | if (ua_sess->deleted) { |
4290 | pthread_mutex_unlock(&ua_sess->lock); | |
4291 | continue; | |
4292 | } | |
4293 | ||
d88aee68 DG |
4294 | /* |
4295 | * Normally, this is done in the delete session process which is | |
4296 | * executed in the call rcu below. However, upon registration we can't | |
4297 | * afford to wait for the grace period before pushing data or else the | |
4298 | * data pending feature can race between the unregistration and stop | |
4299 | * command where the data pending command is sent *before* the grace | |
4300 | * period ended. | |
4301 | * | |
4302 | * The close metadata below nullifies the metadata pointer in the | |
4303 | * session so the delete session will NOT push/close a second time. | |
4304 | */ | |
7972aab2 | 4305 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 4306 | if (registry) { |
7972aab2 DG |
4307 | /* Push metadata for application before freeing the application. */ |
4308 | (void) push_metadata(registry, ua_sess->consumer); | |
4309 | ||
4310 | /* | |
4311 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
4312 | * metadata only on destroy trace session in this case. Also, the |
4313 | * previous push metadata could have flag the metadata registry to | |
4314 | * close so don't send a close command if closed. | |
7972aab2 | 4315 | */ |
ce34fcd0 | 4316 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
4317 | /* And ask to close it for this session registry. */ |
4318 | (void) close_metadata(registry, ua_sess->consumer); | |
4319 | } | |
4320 | } | |
d42f20df | 4321 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
c4b88406 | 4322 | |
d88aee68 | 4323 | pthread_mutex_unlock(&ua_sess->lock); |
d42f20df DG |
4324 | } |
4325 | ||
c4b88406 MD |
4326 | /* Remove application from PID hash table */ |
4327 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); | |
a0377dfe | 4328 | LTTNG_ASSERT(!ret); |
c4b88406 MD |
4329 | |
4330 | /* | |
4331 | * Remove application from notify hash table. The thread handling the | |
4332 | * notify socket could have deleted the node so ignore on error because | |
c48239ca JG |
4333 | * either way it's valid. The close of that socket is handled by the |
4334 | * apps_notify_thread. | |
c4b88406 MD |
4335 | */ |
4336 | iter.iter.node = <a->notify_sock_n.node; | |
4337 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
4338 | ||
4339 | /* | |
4340 | * Ignore return value since the node might have been removed before by an | |
4341 | * add replace during app registration because the PID can be reassigned by | |
4342 | * the OS. | |
4343 | */ | |
4344 | iter.iter.node = <a->pid_n.node; | |
4345 | ret = lttng_ht_del(ust_app_ht, &iter); | |
4346 | if (ret) { | |
4347 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
4348 | lta->pid); | |
4349 | } | |
4350 | ||
852d0037 DG |
4351 | /* Free memory */ |
4352 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
4353 | ||
5b4a0ec0 DG |
4354 | rcu_read_unlock(); |
4355 | return; | |
284d8f55 DG |
4356 | } |
4357 | ||
5b4a0ec0 DG |
4358 | /* |
4359 | * Fill events array with all events name of all registered apps. | |
4360 | */ | |
4361 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 4362 | { |
5b4a0ec0 DG |
4363 | int ret, handle; |
4364 | size_t nbmem, count = 0; | |
bec39940 | 4365 | struct lttng_ht_iter iter; |
5b4a0ec0 | 4366 | struct ust_app *app; |
c617c0c6 | 4367 | struct lttng_event *tmp_event; |
421cb601 | 4368 | |
5b4a0ec0 | 4369 | nbmem = UST_APP_EVENT_LIST_SIZE; |
64803277 | 4370 | tmp_event = calloc<lttng_event>(nbmem); |
c617c0c6 | 4371 | if (tmp_event == NULL) { |
5b4a0ec0 DG |
4372 | PERROR("zmalloc ust app events"); |
4373 | ret = -ENOMEM; | |
421cb601 DG |
4374 | goto error; |
4375 | } | |
4376 | ||
5b4a0ec0 | 4377 | rcu_read_lock(); |
421cb601 | 4378 | |
852d0037 | 4379 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
fc4b93fa | 4380 | struct lttng_ust_abi_tracepoint_iter uiter; |
ac3bd9c0 | 4381 | |
840cb59c | 4382 | health_code_update(); |
86acf0da | 4383 | |
e0c7ec2b DG |
4384 | if (!app->compatible) { |
4385 | /* | |
4386 | * TODO: In time, we should notice the caller of this error by | |
4387 | * telling him that this is a version error. | |
4388 | */ | |
4389 | continue; | |
4390 | } | |
fb45065e | 4391 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4392 | handle = lttng_ust_ctl_tracepoint_list(app->sock); |
5b4a0ec0 | 4393 | if (handle < 0) { |
ffe60014 DG |
4394 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4395 | ERR("UST app list events getting handle failed for app pid %d", | |
4396 | app->pid); | |
4397 | } | |
fb45065e | 4398 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 DG |
4399 | continue; |
4400 | } | |
421cb601 | 4401 | |
b623cb6a | 4402 | while ((ret = lttng_ust_ctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 4403 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4404 | /* Handle ustctl error. */ |
4405 | if (ret < 0) { | |
fb45065e MD |
4406 | int release_ret; |
4407 | ||
a2ba1ab0 | 4408 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4409 | ERR("UST app tp list get failed for app %d with ret %d", |
4410 | app->sock, ret); | |
4411 | } else { | |
4412 | DBG3("UST app tp list get failed. Application is dead"); | |
3757b385 | 4413 | break; |
ffe60014 | 4414 | } |
98f595d4 | 4415 | free(tmp_event); |
b623cb6a | 4416 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
68313703 JG |
4417 | if (release_ret < 0 && |
4418 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4419 | release_ret != -EPIPE) { | |
fb45065e MD |
4420 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4421 | } | |
4422 | pthread_mutex_unlock(&app->sock_lock); | |
ffe60014 DG |
4423 | goto rcu_error; |
4424 | } | |
4425 | ||
840cb59c | 4426 | health_code_update(); |
815564d8 | 4427 | if (count >= nbmem) { |
d7b3776f | 4428 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4429 | struct lttng_event *new_tmp_event; |
4430 | size_t new_nbmem; | |
4431 | ||
4432 | new_nbmem = nbmem << 1; | |
4433 | DBG2("Reallocating event list from %zu to %zu entries", | |
4434 | nbmem, new_nbmem); | |
7966af57 | 4435 | new_tmp_event = (lttng_event *) realloc(tmp_event, |
53efb85a MD |
4436 | new_nbmem * sizeof(struct lttng_event)); |
4437 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4438 | int release_ret; |
4439 | ||
5b4a0ec0 | 4440 | PERROR("realloc ust app events"); |
c617c0c6 | 4441 | free(tmp_event); |
5b4a0ec0 | 4442 | ret = -ENOMEM; |
b623cb6a | 4443 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
68313703 JG |
4444 | if (release_ret < 0 && |
4445 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4446 | release_ret != -EPIPE) { | |
fb45065e MD |
4447 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4448 | } | |
4449 | pthread_mutex_unlock(&app->sock_lock); | |
5b4a0ec0 DG |
4450 | goto rcu_error; |
4451 | } | |
53efb85a MD |
4452 | /* Zero the new memory */ |
4453 | memset(new_tmp_event + nbmem, 0, | |
4454 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
4455 | nbmem = new_nbmem; | |
4456 | tmp_event = new_tmp_event; | |
5b4a0ec0 | 4457 | } |
fc4b93fa | 4458 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN); |
c617c0c6 | 4459 | tmp_event[count].loglevel = uiter.loglevel; |
fc4b93fa | 4460 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT; |
c617c0c6 MD |
4461 | tmp_event[count].pid = app->pid; |
4462 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 4463 | count++; |
421cb601 | 4464 | } |
b623cb6a | 4465 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4466 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
4467 | if (ret < 0) { |
4468 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
4469 | DBG3("Error releasing app handle. Application died: pid = %d, sock = %d", | |
4470 | app->pid, app->sock); | |
4471 | } else if (ret == -EAGAIN) { | |
4472 | WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d", | |
4473 | app->pid, app->sock); | |
4474 | } else { | |
4475 | ERR("Error releasing app handle with ret %d: pid = %d, sock = %d", | |
4476 | ret, app->pid, app->sock); | |
4477 | } | |
fb45065e | 4478 | } |
421cb601 DG |
4479 | } |
4480 | ||
5b4a0ec0 | 4481 | ret = count; |
c617c0c6 | 4482 | *events = tmp_event; |
421cb601 | 4483 | |
5b4a0ec0 | 4484 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 4485 | |
5b4a0ec0 DG |
4486 | rcu_error: |
4487 | rcu_read_unlock(); | |
421cb601 | 4488 | error: |
840cb59c | 4489 | health_code_update(); |
5b4a0ec0 | 4490 | return ret; |
421cb601 DG |
4491 | } |
4492 | ||
f37d259d MD |
4493 | /* |
4494 | * Fill events array with all events name of all registered apps. | |
4495 | */ | |
4496 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
4497 | { | |
4498 | int ret, handle; | |
4499 | size_t nbmem, count = 0; | |
4500 | struct lttng_ht_iter iter; | |
4501 | struct ust_app *app; | |
c617c0c6 | 4502 | struct lttng_event_field *tmp_event; |
f37d259d MD |
4503 | |
4504 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
64803277 | 4505 | tmp_event = calloc<lttng_event_field>(nbmem); |
c617c0c6 | 4506 | if (tmp_event == NULL) { |
f37d259d MD |
4507 | PERROR("zmalloc ust app event fields"); |
4508 | ret = -ENOMEM; | |
4509 | goto error; | |
4510 | } | |
4511 | ||
4512 | rcu_read_lock(); | |
4513 | ||
4514 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
fc4b93fa | 4515 | struct lttng_ust_abi_field_iter uiter; |
f37d259d | 4516 | |
840cb59c | 4517 | health_code_update(); |
86acf0da | 4518 | |
f37d259d MD |
4519 | if (!app->compatible) { |
4520 | /* | |
4521 | * TODO: In time, we should notice the caller of this error by | |
4522 | * telling him that this is a version error. | |
4523 | */ | |
4524 | continue; | |
4525 | } | |
fb45065e | 4526 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 4527 | handle = lttng_ust_ctl_tracepoint_field_list(app->sock); |
f37d259d | 4528 | if (handle < 0) { |
ffe60014 DG |
4529 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
4530 | ERR("UST app list field getting handle failed for app pid %d", | |
4531 | app->pid); | |
4532 | } | |
fb45065e | 4533 | pthread_mutex_unlock(&app->sock_lock); |
f37d259d MD |
4534 | continue; |
4535 | } | |
4536 | ||
b623cb6a | 4537 | while ((ret = lttng_ust_ctl_tracepoint_field_list_get(app->sock, handle, |
fb54cdbf | 4538 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
4539 | /* Handle ustctl error. */ |
4540 | if (ret < 0) { | |
fb45065e MD |
4541 | int release_ret; |
4542 | ||
a2ba1ab0 | 4543 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
4544 | ERR("UST app tp list field failed for app %d with ret %d", |
4545 | app->sock, ret); | |
4546 | } else { | |
4547 | DBG3("UST app tp list field failed. Application is dead"); | |
3757b385 | 4548 | break; |
ffe60014 | 4549 | } |
98f595d4 | 4550 | free(tmp_event); |
b623cb6a | 4551 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4552 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4553 | if (release_ret < 0 && |
4554 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4555 | release_ret != -EPIPE) { | |
fb45065e MD |
4556 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4557 | } | |
ffe60014 DG |
4558 | goto rcu_error; |
4559 | } | |
4560 | ||
840cb59c | 4561 | health_code_update(); |
f37d259d | 4562 | if (count >= nbmem) { |
d7b3776f | 4563 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
4564 | struct lttng_event_field *new_tmp_event; |
4565 | size_t new_nbmem; | |
4566 | ||
4567 | new_nbmem = nbmem << 1; | |
4568 | DBG2("Reallocating event field list from %zu to %zu entries", | |
4569 | nbmem, new_nbmem); | |
7966af57 | 4570 | new_tmp_event = (lttng_event_field *) realloc(tmp_event, |
53efb85a MD |
4571 | new_nbmem * sizeof(struct lttng_event_field)); |
4572 | if (new_tmp_event == NULL) { | |
fb45065e MD |
4573 | int release_ret; |
4574 | ||
f37d259d | 4575 | PERROR("realloc ust app event fields"); |
c617c0c6 | 4576 | free(tmp_event); |
f37d259d | 4577 | ret = -ENOMEM; |
b623cb6a | 4578 | release_ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4579 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4580 | if (release_ret && |
4581 | release_ret != -LTTNG_UST_ERR_EXITING && | |
4582 | release_ret != -EPIPE) { | |
fb45065e MD |
4583 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
4584 | } | |
f37d259d MD |
4585 | goto rcu_error; |
4586 | } | |
53efb85a MD |
4587 | /* Zero the new memory */ |
4588 | memset(new_tmp_event + nbmem, 0, | |
4589 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); | |
4590 | nbmem = new_nbmem; | |
4591 | tmp_event = new_tmp_event; | |
f37d259d | 4592 | } |
f37d259d | 4593 | |
fc4b93fa | 4594 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
2e84128e DG |
4595 | /* Mapping between these enums matches 1 to 1. */ |
4596 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; | |
c617c0c6 | 4597 | tmp_event[count].nowrite = uiter.nowrite; |
f37d259d | 4598 | |
fc4b93fa | 4599 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_ABI_SYM_NAME_LEN); |
c617c0c6 | 4600 | tmp_event[count].event.loglevel = uiter.loglevel; |
2e84128e | 4601 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
c617c0c6 MD |
4602 | tmp_event[count].event.pid = app->pid; |
4603 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
4604 | count++; |
4605 | } | |
b623cb6a | 4606 | ret = lttng_ust_ctl_release_handle(app->sock, handle); |
fb45065e | 4607 | pthread_mutex_unlock(&app->sock_lock); |
68313703 JG |
4608 | if (ret < 0 && |
4609 | ret != -LTTNG_UST_ERR_EXITING && | |
4610 | ret != -EPIPE) { | |
fb45065e MD |
4611 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
4612 | } | |
f37d259d MD |
4613 | } |
4614 | ||
4615 | ret = count; | |
c617c0c6 | 4616 | *fields = tmp_event; |
f37d259d MD |
4617 | |
4618 | DBG2("UST app list event fields done (%zu events)", count); | |
4619 | ||
4620 | rcu_error: | |
4621 | rcu_read_unlock(); | |
4622 | error: | |
840cb59c | 4623 | health_code_update(); |
f37d259d MD |
4624 | return ret; |
4625 | } | |
4626 | ||
5b4a0ec0 DG |
4627 | /* |
4628 | * Free and clean all traceable apps of the global list. | |
4629 | */ | |
4630 | void ust_app_clean_list(void) | |
421cb601 | 4631 | { |
5b4a0ec0 | 4632 | int ret; |
659ed79f | 4633 | struct ust_app *app; |
bec39940 | 4634 | struct lttng_ht_iter iter; |
421cb601 | 4635 | |
5b4a0ec0 | 4636 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 4637 | |
5b4a0ec0 | 4638 | rcu_read_lock(); |
421cb601 | 4639 | |
faadaa3a JG |
4640 | /* Cleanup notify socket hash table */ |
4641 | if (ust_app_ht_by_notify_sock) { | |
4642 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, | |
4643 | notify_sock_n.node) { | |
b69a1b40 JG |
4644 | /* |
4645 | * Assert that all notifiers are gone as all triggers | |
4646 | * are unregistered prior to this clean-up. | |
4647 | */ | |
a0377dfe | 4648 | LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); |
faadaa3a | 4649 | |
faadaa3a JG |
4650 | ust_app_notify_sock_unregister(app->notify_sock); |
4651 | } | |
4652 | } | |
4653 | ||
f1b711c4 MD |
4654 | if (ust_app_ht) { |
4655 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4656 | ret = lttng_ht_del(ust_app_ht, &iter); | |
a0377dfe | 4657 | LTTNG_ASSERT(!ret); |
f1b711c4 MD |
4658 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); |
4659 | } | |
421cb601 DG |
4660 | } |
4661 | ||
852d0037 | 4662 | /* Cleanup socket hash table */ |
f1b711c4 MD |
4663 | if (ust_app_ht_by_sock) { |
4664 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, | |
4665 | sock_n.node) { | |
4666 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); | |
a0377dfe | 4667 | LTTNG_ASSERT(!ret); |
f1b711c4 | 4668 | } |
bec39940 | 4669 | } |
852d0037 | 4670 | |
36b588ed | 4671 | rcu_read_unlock(); |
d88aee68 | 4672 | |
bec39940 | 4673 | /* Destroy is done only when the ht is empty */ |
f1b711c4 | 4674 | if (ust_app_ht) { |
3c339053 | 4675 | lttng_ht_destroy(ust_app_ht); |
f1b711c4 MD |
4676 | } |
4677 | if (ust_app_ht_by_sock) { | |
3c339053 | 4678 | lttng_ht_destroy(ust_app_ht_by_sock); |
f1b711c4 MD |
4679 | } |
4680 | if (ust_app_ht_by_notify_sock) { | |
3c339053 | 4681 | lttng_ht_destroy(ust_app_ht_by_notify_sock); |
f1b711c4 | 4682 | } |
5b4a0ec0 DG |
4683 | } |
4684 | ||
4685 | /* | |
4686 | * Init UST app hash table. | |
4687 | */ | |
57703f6e | 4688 | int ust_app_ht_alloc(void) |
5b4a0ec0 | 4689 | { |
bec39940 | 4690 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4691 | if (!ust_app_ht) { |
4692 | return -1; | |
4693 | } | |
852d0037 | 4694 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4695 | if (!ust_app_ht_by_sock) { |
4696 | return -1; | |
4697 | } | |
d0b96690 | 4698 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
4699 | if (!ust_app_ht_by_notify_sock) { |
4700 | return -1; | |
4701 | } | |
4702 | return 0; | |
421cb601 DG |
4703 | } |
4704 | ||
78f0bacd DG |
4705 | /* |
4706 | * For a specific UST session, disable the channel for all registered apps. | |
4707 | */ | |
35a9059d | 4708 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4709 | struct ltt_ust_channel *uchan) |
4710 | { | |
4711 | int ret = 0; | |
bec39940 DG |
4712 | struct lttng_ht_iter iter; |
4713 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
4714 | struct ust_app *app; |
4715 | struct ust_app_session *ua_sess; | |
8535a6d9 | 4716 | struct ust_app_channel *ua_chan; |
78f0bacd | 4717 | |
a0377dfe | 4718 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4719 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
a991f516 | 4720 | uchan->name, usess->id); |
78f0bacd DG |
4721 | |
4722 | rcu_read_lock(); | |
4723 | ||
4724 | /* For every registered applications */ | |
852d0037 | 4725 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 4726 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
4727 | if (!app->compatible) { |
4728 | /* | |
4729 | * TODO: In time, we should notice the caller of this error by | |
4730 | * telling him that this is a version error. | |
4731 | */ | |
4732 | continue; | |
4733 | } | |
78f0bacd DG |
4734 | ua_sess = lookup_session_by_app(usess, app); |
4735 | if (ua_sess == NULL) { | |
4736 | continue; | |
4737 | } | |
4738 | ||
8535a6d9 | 4739 | /* Get channel */ |
bec39940 DG |
4740 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4741 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 | 4742 | /* If the session if found for the app, the channel must be there */ |
a0377dfe | 4743 | LTTNG_ASSERT(ua_chan_node); |
8535a6d9 DG |
4744 | |
4745 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4746 | /* The channel must not be already disabled */ | |
a0377dfe | 4747 | LTTNG_ASSERT(ua_chan->enabled == 1); |
8535a6d9 DG |
4748 | |
4749 | /* Disable channel onto application */ | |
4750 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
4751 | if (ret < 0) { |
4752 | /* XXX: We might want to report this error at some point... */ | |
4753 | continue; | |
4754 | } | |
4755 | } | |
4756 | ||
4757 | rcu_read_unlock(); | |
78f0bacd DG |
4758 | return ret; |
4759 | } | |
4760 | ||
4761 | /* | |
4762 | * For a specific UST session, enable the channel for all registered apps. | |
4763 | */ | |
35a9059d | 4764 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
4765 | struct ltt_ust_channel *uchan) |
4766 | { | |
4767 | int ret = 0; | |
bec39940 | 4768 | struct lttng_ht_iter iter; |
78f0bacd DG |
4769 | struct ust_app *app; |
4770 | struct ust_app_session *ua_sess; | |
4771 | ||
a0377dfe | 4772 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4773 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
a991f516 | 4774 | uchan->name, usess->id); |
78f0bacd DG |
4775 | |
4776 | rcu_read_lock(); | |
4777 | ||
4778 | /* For every registered applications */ | |
852d0037 | 4779 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4780 | if (!app->compatible) { |
4781 | /* | |
4782 | * TODO: In time, we should notice the caller of this error by | |
4783 | * telling him that this is a version error. | |
4784 | */ | |
4785 | continue; | |
4786 | } | |
78f0bacd DG |
4787 | ua_sess = lookup_session_by_app(usess, app); |
4788 | if (ua_sess == NULL) { | |
4789 | continue; | |
4790 | } | |
4791 | ||
4792 | /* Enable channel onto application */ | |
4793 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
4794 | if (ret < 0) { | |
4795 | /* XXX: We might want to report this error at some point... */ | |
4796 | continue; | |
4797 | } | |
4798 | } | |
4799 | ||
4800 | rcu_read_unlock(); | |
78f0bacd DG |
4801 | return ret; |
4802 | } | |
4803 | ||
b0a40d28 DG |
4804 | /* |
4805 | * Disable an event in a channel and for a specific session. | |
4806 | */ | |
35a9059d DG |
4807 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
4808 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
4809 | { |
4810 | int ret = 0; | |
bec39940 | 4811 | struct lttng_ht_iter iter, uiter; |
700c5a9d | 4812 | struct lttng_ht_node_str *ua_chan_node; |
b0a40d28 DG |
4813 | struct ust_app *app; |
4814 | struct ust_app_session *ua_sess; | |
4815 | struct ust_app_channel *ua_chan; | |
4816 | struct ust_app_event *ua_event; | |
4817 | ||
a0377dfe | 4818 | LTTNG_ASSERT(usess->active); |
b0a40d28 | 4819 | DBG("UST app disabling event %s for all apps in channel " |
d9bf3ca4 MD |
4820 | "%s for session id %" PRIu64, |
4821 | uevent->attr.name, uchan->name, usess->id); | |
b0a40d28 DG |
4822 | |
4823 | rcu_read_lock(); | |
4824 | ||
4825 | /* For all registered applications */ | |
852d0037 | 4826 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4827 | if (!app->compatible) { |
4828 | /* | |
4829 | * TODO: In time, we should notice the caller of this error by | |
4830 | * telling him that this is a version error. | |
4831 | */ | |
4832 | continue; | |
4833 | } | |
b0a40d28 DG |
4834 | ua_sess = lookup_session_by_app(usess, app); |
4835 | if (ua_sess == NULL) { | |
4836 | /* Next app */ | |
4837 | continue; | |
4838 | } | |
4839 | ||
4840 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
4841 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4842 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 4843 | if (ua_chan_node == NULL) { |
d9bf3ca4 | 4844 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
852d0037 | 4845 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
4846 | continue; |
4847 | } | |
4848 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4849 | ||
700c5a9d JR |
4850 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
4851 | uevent->filter, uevent->attr.loglevel, | |
4852 | uevent->exclusion); | |
4853 | if (ua_event == NULL) { | |
b0a40d28 | 4854 | DBG2("Event %s not found in channel %s for app pid %d." |
852d0037 | 4855 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
4856 | continue; |
4857 | } | |
b0a40d28 | 4858 | |
f46376a1 | 4859 | ret = disable_ust_app_event(ua_event, app); |
b0a40d28 DG |
4860 | if (ret < 0) { |
4861 | /* XXX: Report error someday... */ | |
4862 | continue; | |
4863 | } | |
4864 | } | |
4865 | ||
4866 | rcu_read_unlock(); | |
88e3c2f5 JG |
4867 | return ret; |
4868 | } | |
4869 | ||
4870 | /* The ua_sess lock must be held by the caller. */ | |
4871 | static | |
4872 | int ust_app_channel_create(struct ltt_ust_session *usess, | |
4873 | struct ust_app_session *ua_sess, | |
4874 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
4875 | struct ust_app_channel **_ua_chan) | |
4876 | { | |
4877 | int ret = 0; | |
4878 | struct ust_app_channel *ua_chan = NULL; | |
4879 | ||
a0377dfe | 4880 | LTTNG_ASSERT(ua_sess); |
88e3c2f5 JG |
4881 | ASSERT_LOCKED(ua_sess->lock); |
4882 | ||
4883 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, | |
4884 | sizeof(uchan->name))) { | |
4885 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, | |
4886 | &uchan->attr); | |
4887 | ret = 0; | |
4888 | } else { | |
4889 | struct ltt_ust_context *uctx = NULL; | |
4890 | ||
4891 | /* | |
4892 | * Create channel onto application and synchronize its | |
4893 | * configuration. | |
4894 | */ | |
4895 | ret = ust_app_channel_allocate(ua_sess, uchan, | |
fc4b93fa | 4896 | LTTNG_UST_ABI_CHAN_PER_CPU, usess, |
88e3c2f5 | 4897 | &ua_chan); |
88ebf5a7 JR |
4898 | if (ret < 0) { |
4899 | goto error; | |
4900 | } | |
4901 | ||
4902 | ret = ust_app_channel_send(app, usess, | |
4903 | ua_sess, ua_chan); | |
4904 | if (ret) { | |
4905 | goto error; | |
88e3c2f5 JG |
4906 | } |
4907 | ||
4908 | /* Add contexts. */ | |
4909 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
4910 | ret = create_ust_app_channel_context(ua_chan, | |
4911 | &uctx->ctx, app); | |
4912 | if (ret) { | |
88ebf5a7 | 4913 | goto error; |
88e3c2f5 JG |
4914 | } |
4915 | } | |
4916 | } | |
88ebf5a7 JR |
4917 | |
4918 | error: | |
88e3c2f5 JG |
4919 | if (ret < 0) { |
4920 | switch (ret) { | |
4921 | case -ENOTCONN: | |
4922 | /* | |
4923 | * The application's socket is not valid. Either a bad socket | |
4924 | * or a timeout on it. We can't inform the caller that for a | |
4925 | * specific app, the session failed so lets continue here. | |
4926 | */ | |
4927 | ret = 0; /* Not an error. */ | |
4928 | break; | |
4929 | case -ENOMEM: | |
4930 | default: | |
4931 | break; | |
4932 | } | |
4933 | } | |
88ebf5a7 | 4934 | |
88e3c2f5 JG |
4935 | if (ret == 0 && _ua_chan) { |
4936 | /* | |
4937 | * Only return the application's channel on success. Note | |
4938 | * that the channel can still be part of the application's | |
4939 | * channel hashtable on error. | |
4940 | */ | |
4941 | *_ua_chan = ua_chan; | |
4942 | } | |
b0a40d28 DG |
4943 | return ret; |
4944 | } | |
4945 | ||
5b4a0ec0 | 4946 | /* |
edb67388 | 4947 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 4948 | */ |
35a9059d | 4949 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
4950 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4951 | { | |
4952 | int ret = 0; | |
bec39940 | 4953 | struct lttng_ht_iter iter, uiter; |
18eace3b | 4954 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
4955 | struct ust_app *app; |
4956 | struct ust_app_session *ua_sess; | |
4957 | struct ust_app_channel *ua_chan; | |
4958 | struct ust_app_event *ua_event; | |
48842b30 | 4959 | |
a0377dfe | 4960 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 4961 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
a991f516 | 4962 | uevent->attr.name, usess->id); |
48842b30 | 4963 | |
edb67388 DG |
4964 | /* |
4965 | * NOTE: At this point, this function is called only if the session and | |
4966 | * channel passed are already created for all apps. and enabled on the | |
4967 | * tracer also. | |
4968 | */ | |
4969 | ||
48842b30 | 4970 | rcu_read_lock(); |
421cb601 DG |
4971 | |
4972 | /* For all registered applications */ | |
852d0037 | 4973 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4974 | if (!app->compatible) { |
4975 | /* | |
4976 | * TODO: In time, we should notice the caller of this error by | |
4977 | * telling him that this is a version error. | |
4978 | */ | |
4979 | continue; | |
4980 | } | |
edb67388 | 4981 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4982 | if (!ua_sess) { |
4983 | /* The application has problem or is probably dead. */ | |
4984 | continue; | |
4985 | } | |
ba767faf | 4986 | |
d0b96690 DG |
4987 | pthread_mutex_lock(&ua_sess->lock); |
4988 | ||
b161602a MD |
4989 | if (ua_sess->deleted) { |
4990 | pthread_mutex_unlock(&ua_sess->lock); | |
4991 | continue; | |
4992 | } | |
4993 | ||
edb67388 | 4994 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4995 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4996 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
a7169585 MD |
4997 | /* |
4998 | * It is possible that the channel cannot be found is | |
4999 | * the channel/event creation occurs concurrently with | |
5000 | * an application exit. | |
5001 | */ | |
5002 | if (!ua_chan_node) { | |
5003 | pthread_mutex_unlock(&ua_sess->lock); | |
5004 | continue; | |
5005 | } | |
edb67388 DG |
5006 | |
5007 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
5008 | ||
18eace3b DG |
5009 | /* Get event node */ |
5010 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
39c5a3a7 | 5011 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 5012 | if (ua_event == NULL) { |
7f79d3a1 | 5013 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 5014 | "Skipping app", uevent->attr.name, app->pid); |
d0b96690 | 5015 | goto next_app; |
35a9059d | 5016 | } |
35a9059d | 5017 | |
f46376a1 | 5018 | ret = enable_ust_app_event(ua_event, app); |
35a9059d | 5019 | if (ret < 0) { |
d0b96690 | 5020 | pthread_mutex_unlock(&ua_sess->lock); |
7f79d3a1 | 5021 | goto error; |
48842b30 | 5022 | } |
d0b96690 DG |
5023 | next_app: |
5024 | pthread_mutex_unlock(&ua_sess->lock); | |
edb67388 DG |
5025 | } |
5026 | ||
7f79d3a1 | 5027 | error: |
edb67388 | 5028 | rcu_read_unlock(); |
edb67388 DG |
5029 | return ret; |
5030 | } | |
5031 | ||
5032 | /* | |
5033 | * For a specific existing UST session and UST channel, creates the event for | |
5034 | * all registered apps. | |
5035 | */ | |
35a9059d | 5036 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
5037 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
5038 | { | |
5039 | int ret = 0; | |
bec39940 DG |
5040 | struct lttng_ht_iter iter, uiter; |
5041 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
5042 | struct ust_app *app; |
5043 | struct ust_app_session *ua_sess; | |
5044 | struct ust_app_channel *ua_chan; | |
5045 | ||
a0377dfe | 5046 | LTTNG_ASSERT(usess->active); |
d9bf3ca4 | 5047 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
a991f516 | 5048 | uevent->attr.name, usess->id); |
edb67388 | 5049 | |
edb67388 DG |
5050 | rcu_read_lock(); |
5051 | ||
5052 | /* For all registered applications */ | |
852d0037 | 5053 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
5054 | if (!app->compatible) { |
5055 | /* | |
5056 | * TODO: In time, we should notice the caller of this error by | |
5057 | * telling him that this is a version error. | |
5058 | */ | |
5059 | continue; | |
5060 | } | |
edb67388 | 5061 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
5062 | if (!ua_sess) { |
5063 | /* The application has problem or is probably dead. */ | |
5064 | continue; | |
5065 | } | |
48842b30 | 5066 | |
d0b96690 | 5067 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
5068 | |
5069 | if (ua_sess->deleted) { | |
5070 | pthread_mutex_unlock(&ua_sess->lock); | |
5071 | continue; | |
5072 | } | |
5073 | ||
48842b30 | 5074 | /* Lookup channel in the ust app session */ |
bec39940 DG |
5075 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
5076 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 | 5077 | /* If the channel is not found, there is a code flow error */ |
a0377dfe | 5078 | LTTNG_ASSERT(ua_chan_node); |
edb67388 | 5079 | |
48842b30 DG |
5080 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
5081 | ||
f46376a1 | 5082 | ret = create_ust_app_event(ua_chan, uevent, app); |
d0b96690 | 5083 | pthread_mutex_unlock(&ua_sess->lock); |
edb67388 | 5084 | if (ret < 0) { |
49c336c1 | 5085 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
5086 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
5087 | break; | |
5088 | } | |
5089 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 5090 | uevent->attr.name, app->pid); |
5b4a0ec0 | 5091 | continue; |
48842b30 | 5092 | } |
48842b30 | 5093 | } |
5b4a0ec0 | 5094 | |
48842b30 | 5095 | rcu_read_unlock(); |
48842b30 DG |
5096 | return ret; |
5097 | } | |
5098 | ||
5b4a0ec0 DG |
5099 | /* |
5100 | * Start tracing for a specific UST session and app. | |
fad1ed2f JR |
5101 | * |
5102 | * Called with UST app session lock held. | |
5103 | * | |
5b4a0ec0 | 5104 | */ |
b34cbebf | 5105 | static |
421cb601 | 5106 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
5107 | { |
5108 | int ret = 0; | |
48842b30 | 5109 | struct ust_app_session *ua_sess; |
48842b30 | 5110 | |
852d0037 | 5111 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 5112 | |
509cbaf8 MD |
5113 | rcu_read_lock(); |
5114 | ||
e0c7ec2b DG |
5115 | if (!app->compatible) { |
5116 | goto end; | |
5117 | } | |
5118 | ||
421cb601 DG |
5119 | ua_sess = lookup_session_by_app(usess, app); |
5120 | if (ua_sess == NULL) { | |
d42f20df DG |
5121 | /* The session is in teardown process. Ignore and continue. */ |
5122 | goto end; | |
421cb601 | 5123 | } |
48842b30 | 5124 | |
d0b96690 DG |
5125 | pthread_mutex_lock(&ua_sess->lock); |
5126 | ||
b161602a MD |
5127 | if (ua_sess->deleted) { |
5128 | pthread_mutex_unlock(&ua_sess->lock); | |
5129 | goto end; | |
5130 | } | |
5131 | ||
b0a1c741 JR |
5132 | if (ua_sess->enabled) { |
5133 | pthread_mutex_unlock(&ua_sess->lock); | |
5134 | goto end; | |
5135 | } | |
5136 | ||
aea829b3 DG |
5137 | /* Upon restart, we skip the setup, already done */ |
5138 | if (ua_sess->started) { | |
8be98f9a | 5139 | goto skip_setup; |
aea829b3 | 5140 | } |
8be98f9a | 5141 | |
840cb59c | 5142 | health_code_update(); |
86acf0da | 5143 | |
8be98f9a | 5144 | skip_setup: |
a945cdc7 | 5145 | /* This starts the UST tracing */ |
fb45065e | 5146 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5147 | ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle); |
fb45065e | 5148 | pthread_mutex_unlock(&app->sock_lock); |
421cb601 | 5149 | if (ret < 0) { |
be355079 JR |
5150 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
5151 | DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d", | |
5152 | app->pid, app->sock); | |
5153 | pthread_mutex_unlock(&ua_sess->lock); | |
5154 | goto end; | |
5155 | } else if (ret == -EAGAIN) { | |
5156 | WARN("UST app start session failed. Communication time out: pid = %d, sock = %d", | |
5157 | app->pid, app->sock); | |
3757b385 DG |
5158 | pthread_mutex_unlock(&ua_sess->lock); |
5159 | goto end; | |
be355079 JR |
5160 | |
5161 | } else { | |
5162 | ERR("UST app start session failed with ret %d: pid = %d, sock = %d", | |
5163 | ret, app->pid, app->sock); | |
ffe60014 | 5164 | } |
d0b96690 | 5165 | goto error_unlock; |
421cb601 | 5166 | } |
5b4a0ec0 | 5167 | |
55c3953d DG |
5168 | /* Indicate that the session has been started once */ |
5169 | ua_sess->started = 1; | |
b0a1c741 | 5170 | ua_sess->enabled = 1; |
55c3953d | 5171 | |
d0b96690 DG |
5172 | pthread_mutex_unlock(&ua_sess->lock); |
5173 | ||
840cb59c | 5174 | health_code_update(); |
86acf0da | 5175 | |
421cb601 | 5176 | /* Quiescent wait after starting trace */ |
fb45065e | 5177 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5178 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5179 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5180 | if (ret < 0) { |
5181 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
5182 | DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d", | |
5183 | app->pid, app->sock); | |
5184 | } else if (ret == -EAGAIN) { | |
5185 | WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d", | |
5186 | app->pid, app->sock); | |
5187 | } else { | |
5188 | ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d", | |
5189 | ret, app->pid, app->sock); | |
5190 | } | |
ffe60014 | 5191 | } |
48842b30 | 5192 | |
e0c7ec2b DG |
5193 | end: |
5194 | rcu_read_unlock(); | |
840cb59c | 5195 | health_code_update(); |
421cb601 | 5196 | return 0; |
48842b30 | 5197 | |
d0b96690 DG |
5198 | error_unlock: |
5199 | pthread_mutex_unlock(&ua_sess->lock); | |
509cbaf8 | 5200 | rcu_read_unlock(); |
840cb59c | 5201 | health_code_update(); |
421cb601 DG |
5202 | return -1; |
5203 | } | |
48842b30 | 5204 | |
8be98f9a MD |
5205 | /* |
5206 | * Stop tracing for a specific UST session and app. | |
5207 | */ | |
b34cbebf | 5208 | static |
8be98f9a MD |
5209 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
5210 | { | |
5211 | int ret = 0; | |
5212 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 5213 | ust_registry_session *registry; |
8be98f9a | 5214 | |
852d0037 | 5215 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
5216 | |
5217 | rcu_read_lock(); | |
5218 | ||
e0c7ec2b | 5219 | if (!app->compatible) { |
d88aee68 | 5220 | goto end_no_session; |
e0c7ec2b DG |
5221 | } |
5222 | ||
8be98f9a MD |
5223 | ua_sess = lookup_session_by_app(usess, app); |
5224 | if (ua_sess == NULL) { | |
d88aee68 | 5225 | goto end_no_session; |
8be98f9a MD |
5226 | } |
5227 | ||
d88aee68 DG |
5228 | pthread_mutex_lock(&ua_sess->lock); |
5229 | ||
b161602a MD |
5230 | if (ua_sess->deleted) { |
5231 | pthread_mutex_unlock(&ua_sess->lock); | |
5232 | goto end_no_session; | |
5233 | } | |
5234 | ||
9bc07046 DG |
5235 | /* |
5236 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
5237 | * that was never started. It's possible since we can have a fail start |
5238 | * from either the application manager thread or the command thread. Simply | |
5239 | * indicate that this is a stop error. | |
9bc07046 | 5240 | */ |
f9dfc3d9 | 5241 | if (!ua_sess->started) { |
c45536e1 DG |
5242 | goto error_rcu_unlock; |
5243 | } | |
7db205b5 | 5244 | |
840cb59c | 5245 | health_code_update(); |
86acf0da | 5246 | |
9d6c7d3f | 5247 | /* This inhibits UST tracing */ |
fb45065e | 5248 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5249 | ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle); |
fb45065e | 5250 | pthread_mutex_unlock(&app->sock_lock); |
9d6c7d3f | 5251 | if (ret < 0) { |
be355079 JR |
5252 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
5253 | DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d", | |
5254 | app->pid, app->sock); | |
3757b385 | 5255 | goto end_unlock; |
be355079 JR |
5256 | } else if (ret == -EAGAIN) { |
5257 | WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d", | |
5258 | app->pid, app->sock); | |
5259 | goto end_unlock; | |
5260 | ||
5261 | } else { | |
5262 | ERR("UST app stop session failed with ret %d: pid = %d, sock = %d", | |
5263 | ret, app->pid, app->sock); | |
ffe60014 | 5264 | } |
9d6c7d3f DG |
5265 | goto error_rcu_unlock; |
5266 | } | |
5267 | ||
840cb59c | 5268 | health_code_update(); |
b0a1c741 | 5269 | ua_sess->enabled = 0; |
86acf0da | 5270 | |
9d6c7d3f | 5271 | /* Quiescent wait after stopping trace */ |
fb45065e | 5272 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5273 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5274 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5275 | if (ret < 0) { |
5276 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
9324443a | 5277 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
be355079 JR |
5278 | app->pid, app->sock); |
5279 | } else if (ret == -EAGAIN) { | |
9324443a | 5280 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
be355079 JR |
5281 | app->pid, app->sock); |
5282 | } else { | |
9324443a | 5283 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
be355079 JR |
5284 | ret, app->pid, app->sock); |
5285 | } | |
ffe60014 | 5286 | } |
9d6c7d3f | 5287 | |
840cb59c | 5288 | health_code_update(); |
86acf0da | 5289 | |
b34cbebf | 5290 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
5291 | |
5292 | /* The UST app session is held registry shall not be null. */ | |
a0377dfe | 5293 | LTTNG_ASSERT(registry); |
1b532a60 | 5294 | |
ce34fcd0 MD |
5295 | /* Push metadata for application before freeing the application. */ |
5296 | (void) push_metadata(registry, ua_sess->consumer); | |
b34cbebf | 5297 | |
3757b385 | 5298 | end_unlock: |
b34cbebf MD |
5299 | pthread_mutex_unlock(&ua_sess->lock); |
5300 | end_no_session: | |
5301 | rcu_read_unlock(); | |
5302 | health_code_update(); | |
5303 | return 0; | |
5304 | ||
5305 | error_rcu_unlock: | |
5306 | pthread_mutex_unlock(&ua_sess->lock); | |
5307 | rcu_read_unlock(); | |
5308 | health_code_update(); | |
5309 | return -1; | |
5310 | } | |
5311 | ||
b34cbebf | 5312 | static |
c4b88406 MD |
5313 | int ust_app_flush_app_session(struct ust_app *app, |
5314 | struct ust_app_session *ua_sess) | |
b34cbebf | 5315 | { |
c4b88406 | 5316 | int ret, retval = 0; |
b34cbebf | 5317 | struct lttng_ht_iter iter; |
b34cbebf | 5318 | struct ust_app_channel *ua_chan; |
c4b88406 | 5319 | struct consumer_socket *socket; |
b34cbebf | 5320 | |
c4b88406 | 5321 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
b34cbebf MD |
5322 | |
5323 | rcu_read_lock(); | |
5324 | ||
5325 | if (!app->compatible) { | |
c4b88406 | 5326 | goto end_not_compatible; |
b34cbebf MD |
5327 | } |
5328 | ||
5329 | pthread_mutex_lock(&ua_sess->lock); | |
5330 | ||
b161602a MD |
5331 | if (ua_sess->deleted) { |
5332 | goto end_deleted; | |
5333 | } | |
5334 | ||
b34cbebf MD |
5335 | health_code_update(); |
5336 | ||
9d6c7d3f | 5337 | /* Flushing buffers */ |
c4b88406 MD |
5338 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
5339 | ua_sess->consumer); | |
ce34fcd0 MD |
5340 | |
5341 | /* Flush buffers and push metadata. */ | |
5342 | switch (ua_sess->buffer_type) { | |
5343 | case LTTNG_BUFFER_PER_PID: | |
5344 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, | |
5345 | node.node) { | |
5346 | health_code_update(); | |
ce34fcd0 MD |
5347 | ret = consumer_flush_channel(socket, ua_chan->key); |
5348 | if (ret) { | |
5349 | ERR("Error flushing consumer channel"); | |
5350 | retval = -1; | |
5351 | continue; | |
5352 | } | |
8be98f9a | 5353 | } |
ce34fcd0 MD |
5354 | break; |
5355 | case LTTNG_BUFFER_PER_UID: | |
5356 | default: | |
a0377dfe | 5357 | abort(); |
ce34fcd0 | 5358 | break; |
8be98f9a | 5359 | } |
8be98f9a | 5360 | |
840cb59c | 5361 | health_code_update(); |
86acf0da | 5362 | |
b161602a | 5363 | end_deleted: |
d88aee68 | 5364 | pthread_mutex_unlock(&ua_sess->lock); |
ce34fcd0 | 5365 | |
c4b88406 MD |
5366 | end_not_compatible: |
5367 | rcu_read_unlock(); | |
5368 | health_code_update(); | |
5369 | return retval; | |
5370 | } | |
5371 | ||
5372 | /* | |
ce34fcd0 MD |
5373 | * Flush buffers for all applications for a specific UST session. |
5374 | * Called with UST session lock held. | |
c4b88406 MD |
5375 | */ |
5376 | static | |
ce34fcd0 | 5377 | int ust_app_flush_session(struct ltt_ust_session *usess) |
c4b88406 MD |
5378 | |
5379 | { | |
99b1411c | 5380 | int ret = 0; |
c4b88406 | 5381 | |
ce34fcd0 | 5382 | DBG("Flushing session buffers for all ust apps"); |
c4b88406 MD |
5383 | |
5384 | rcu_read_lock(); | |
5385 | ||
ce34fcd0 MD |
5386 | /* Flush buffers and push metadata. */ |
5387 | switch (usess->buffer_type) { | |
5388 | case LTTNG_BUFFER_PER_UID: | |
5389 | { | |
5390 | struct buffer_reg_uid *reg; | |
5391 | struct lttng_ht_iter iter; | |
5392 | ||
5393 | /* Flush all per UID buffers associated to that session. */ | |
5394 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
aeeb48c6 | 5395 | ust_registry_session *ust_session_reg; |
3273699d | 5396 | struct buffer_reg_channel *buf_reg_chan; |
ce34fcd0 MD |
5397 | struct consumer_socket *socket; |
5398 | ||
5399 | /* Get consumer socket to use to push the metadata.*/ | |
5400 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
5401 | usess->consumer); | |
5402 | if (!socket) { | |
5403 | /* Ignore request if no consumer is found for the session. */ | |
5404 | continue; | |
5405 | } | |
5406 | ||
5407 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 5408 | buf_reg_chan, node.node) { |
ce34fcd0 MD |
5409 | /* |
5410 | * The following call will print error values so the return | |
5411 | * code is of little importance because whatever happens, we | |
5412 | * have to try them all. | |
5413 | */ | |
3273699d | 5414 | (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key); |
ce34fcd0 MD |
5415 | } |
5416 | ||
5417 | ust_session_reg = reg->registry->reg.ust; | |
5418 | /* Push metadata. */ | |
5419 | (void) push_metadata(ust_session_reg, usess->consumer); | |
5420 | } | |
ce34fcd0 MD |
5421 | break; |
5422 | } | |
5423 | case LTTNG_BUFFER_PER_PID: | |
5424 | { | |
5425 | struct ust_app_session *ua_sess; | |
5426 | struct lttng_ht_iter iter; | |
5427 | struct ust_app *app; | |
5428 | ||
5429 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5430 | ua_sess = lookup_session_by_app(usess, app); | |
5431 | if (ua_sess == NULL) { | |
5432 | continue; | |
5433 | } | |
5434 | (void) ust_app_flush_app_session(app, ua_sess); | |
5435 | } | |
5436 | break; | |
5437 | } | |
5438 | default: | |
99b1411c | 5439 | ret = -1; |
a0377dfe | 5440 | abort(); |
ce34fcd0 | 5441 | break; |
c4b88406 | 5442 | } |
c4b88406 | 5443 | |
7db205b5 | 5444 | rcu_read_unlock(); |
840cb59c | 5445 | health_code_update(); |
c4b88406 | 5446 | return ret; |
8be98f9a MD |
5447 | } |
5448 | ||
0dd01979 MD |
5449 | static |
5450 | int ust_app_clear_quiescent_app_session(struct ust_app *app, | |
5451 | struct ust_app_session *ua_sess) | |
5452 | { | |
5453 | int ret = 0; | |
5454 | struct lttng_ht_iter iter; | |
5455 | struct ust_app_channel *ua_chan; | |
5456 | struct consumer_socket *socket; | |
5457 | ||
5458 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); | |
5459 | ||
5460 | rcu_read_lock(); | |
5461 | ||
5462 | if (!app->compatible) { | |
5463 | goto end_not_compatible; | |
5464 | } | |
5465 | ||
5466 | pthread_mutex_lock(&ua_sess->lock); | |
5467 | ||
5468 | if (ua_sess->deleted) { | |
5469 | goto end_unlock; | |
5470 | } | |
5471 | ||
5472 | health_code_update(); | |
5473 | ||
5474 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
5475 | ua_sess->consumer); | |
5476 | if (!socket) { | |
5477 | ERR("Failed to find consumer (%" PRIu32 ") socket", | |
5478 | app->bits_per_long); | |
5479 | ret = -1; | |
5480 | goto end_unlock; | |
5481 | } | |
5482 | ||
5483 | /* Clear quiescent state. */ | |
5484 | switch (ua_sess->buffer_type) { | |
5485 | case LTTNG_BUFFER_PER_PID: | |
5486 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, | |
5487 | ua_chan, node.node) { | |
5488 | health_code_update(); | |
5489 | ret = consumer_clear_quiescent_channel(socket, | |
5490 | ua_chan->key); | |
5491 | if (ret) { | |
5492 | ERR("Error clearing quiescent state for consumer channel"); | |
5493 | ret = -1; | |
5494 | continue; | |
5495 | } | |
5496 | } | |
5497 | break; | |
5498 | case LTTNG_BUFFER_PER_UID: | |
5499 | default: | |
a0377dfe | 5500 | abort(); |
0dd01979 MD |
5501 | ret = -1; |
5502 | break; | |
5503 | } | |
5504 | ||
5505 | health_code_update(); | |
5506 | ||
5507 | end_unlock: | |
5508 | pthread_mutex_unlock(&ua_sess->lock); | |
5509 | ||
5510 | end_not_compatible: | |
5511 | rcu_read_unlock(); | |
5512 | health_code_update(); | |
5513 | return ret; | |
5514 | } | |
5515 | ||
5516 | /* | |
5517 | * Clear quiescent state in each stream for all applications for a | |
5518 | * specific UST session. | |
5519 | * Called with UST session lock held. | |
5520 | */ | |
5521 | static | |
5522 | int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) | |
5523 | ||
5524 | { | |
5525 | int ret = 0; | |
5526 | ||
5527 | DBG("Clearing stream quiescent state for all ust apps"); | |
5528 | ||
5529 | rcu_read_lock(); | |
5530 | ||
5531 | switch (usess->buffer_type) { | |
5532 | case LTTNG_BUFFER_PER_UID: | |
5533 | { | |
5534 | struct lttng_ht_iter iter; | |
5535 | struct buffer_reg_uid *reg; | |
5536 | ||
5537 | /* | |
5538 | * Clear quiescent for all per UID buffers associated to | |
5539 | * that session. | |
5540 | */ | |
5541 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
5542 | struct consumer_socket *socket; | |
3273699d | 5543 | struct buffer_reg_channel *buf_reg_chan; |
0dd01979 MD |
5544 | |
5545 | /* Get associated consumer socket.*/ | |
5546 | socket = consumer_find_socket_by_bitness( | |
5547 | reg->bits_per_long, usess->consumer); | |
5548 | if (!socket) { | |
5549 | /* | |
5550 | * Ignore request if no consumer is found for | |
5551 | * the session. | |
5552 | */ | |
5553 | continue; | |
5554 | } | |
5555 | ||
5556 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
3273699d | 5557 | &iter.iter, buf_reg_chan, node.node) { |
0dd01979 MD |
5558 | /* |
5559 | * The following call will print error values so | |
5560 | * the return code is of little importance | |
5561 | * because whatever happens, we have to try them | |
5562 | * all. | |
5563 | */ | |
5564 | (void) consumer_clear_quiescent_channel(socket, | |
3273699d | 5565 | buf_reg_chan->consumer_key); |
0dd01979 MD |
5566 | } |
5567 | } | |
5568 | break; | |
5569 | } | |
5570 | case LTTNG_BUFFER_PER_PID: | |
5571 | { | |
5572 | struct ust_app_session *ua_sess; | |
5573 | struct lttng_ht_iter iter; | |
5574 | struct ust_app *app; | |
5575 | ||
5576 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, | |
5577 | pid_n.node) { | |
5578 | ua_sess = lookup_session_by_app(usess, app); | |
5579 | if (ua_sess == NULL) { | |
5580 | continue; | |
5581 | } | |
5582 | (void) ust_app_clear_quiescent_app_session(app, | |
5583 | ua_sess); | |
5584 | } | |
5585 | break; | |
5586 | } | |
5587 | default: | |
5588 | ret = -1; | |
a0377dfe | 5589 | abort(); |
0dd01979 MD |
5590 | break; |
5591 | } | |
5592 | ||
5593 | rcu_read_unlock(); | |
5594 | health_code_update(); | |
5595 | return ret; | |
5596 | } | |
5597 | ||
84cd17c6 MD |
5598 | /* |
5599 | * Destroy a specific UST session in apps. | |
5600 | */ | |
3353de95 | 5601 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 | 5602 | { |
ffe60014 | 5603 | int ret; |
84cd17c6 | 5604 | struct ust_app_session *ua_sess; |
bec39940 | 5605 | struct lttng_ht_iter iter; |
d9bf3ca4 | 5606 | struct lttng_ht_node_u64 *node; |
84cd17c6 | 5607 | |
852d0037 | 5608 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
5609 | |
5610 | rcu_read_lock(); | |
5611 | ||
e0c7ec2b DG |
5612 | if (!app->compatible) { |
5613 | goto end; | |
5614 | } | |
5615 | ||
84cd17c6 | 5616 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 5617 | node = lttng_ht_iter_get_node_u64(&iter); |
84cd17c6 | 5618 | if (node == NULL) { |
d42f20df DG |
5619 | /* Session is being or is deleted. */ |
5620 | goto end; | |
84cd17c6 MD |
5621 | } |
5622 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
c4a1715b | 5623 | |
840cb59c | 5624 | health_code_update(); |
d0b96690 | 5625 | destroy_app_session(app, ua_sess); |
84cd17c6 | 5626 | |
840cb59c | 5627 | health_code_update(); |
7db205b5 | 5628 | |
84cd17c6 | 5629 | /* Quiescent wait after stopping trace */ |
fb45065e | 5630 | pthread_mutex_lock(&app->sock_lock); |
b623cb6a | 5631 | ret = lttng_ust_ctl_wait_quiescent(app->sock); |
fb45065e | 5632 | pthread_mutex_unlock(&app->sock_lock); |
be355079 JR |
5633 | if (ret < 0) { |
5634 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
9324443a | 5635 | DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d", |
be355079 JR |
5636 | app->pid, app->sock); |
5637 | } else if (ret == -EAGAIN) { | |
9324443a | 5638 | WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d", |
be355079 JR |
5639 | app->pid, app->sock); |
5640 | } else { | |
9324443a | 5641 | ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d", |
be355079 JR |
5642 | ret, app->pid, app->sock); |
5643 | } | |
ffe60014 | 5644 | } |
e0c7ec2b DG |
5645 | end: |
5646 | rcu_read_unlock(); | |
840cb59c | 5647 | health_code_update(); |
84cd17c6 | 5648 | return 0; |
84cd17c6 MD |
5649 | } |
5650 | ||
5b4a0ec0 DG |
5651 | /* |
5652 | * Start tracing for the UST session. | |
5653 | */ | |
421cb601 DG |
5654 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
5655 | { | |
bec39940 | 5656 | struct lttng_ht_iter iter; |
421cb601 | 5657 | struct ust_app *app; |
48842b30 | 5658 | |
421cb601 DG |
5659 | DBG("Starting all UST traces"); |
5660 | ||
bb2452c8 MD |
5661 | /* |
5662 | * Even though the start trace might fail, flag this session active so | |
5663 | * other application coming in are started by default. | |
5664 | */ | |
5665 | usess->active = 1; | |
5666 | ||
421cb601 | 5667 | rcu_read_lock(); |
421cb601 | 5668 | |
0dd01979 MD |
5669 | /* |
5670 | * In a start-stop-start use-case, we need to clear the quiescent state | |
5671 | * of each channel set by the prior stop command, thus ensuring that a | |
5672 | * following stop or destroy is sure to grab a timestamp_end near those | |
5673 | * operations, even if the packet is empty. | |
5674 | */ | |
5675 | (void) ust_app_clear_quiescent_session(usess); | |
5676 | ||
0498a00c MD |
5677 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5678 | ust_app_global_update(usess, app); | |
5679 | } | |
5680 | ||
48842b30 DG |
5681 | rcu_read_unlock(); |
5682 | ||
5683 | return 0; | |
5684 | } | |
487cf67c | 5685 | |
8be98f9a MD |
5686 | /* |
5687 | * Start tracing for the UST session. | |
ce34fcd0 | 5688 | * Called with UST session lock held. |
8be98f9a MD |
5689 | */ |
5690 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
5691 | { | |
5692 | int ret = 0; | |
bec39940 | 5693 | struct lttng_ht_iter iter; |
8be98f9a MD |
5694 | struct ust_app *app; |
5695 | ||
5696 | DBG("Stopping all UST traces"); | |
5697 | ||
bb2452c8 MD |
5698 | /* |
5699 | * Even though the stop trace might fail, flag this session inactive so | |
5700 | * other application coming in are not started by default. | |
5701 | */ | |
5702 | usess->active = 0; | |
5703 | ||
8be98f9a MD |
5704 | rcu_read_lock(); |
5705 | ||
b34cbebf MD |
5706 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
5707 | ret = ust_app_stop_trace(usess, app); | |
5708 | if (ret < 0) { | |
5709 | /* Continue to next apps even on error */ | |
5710 | continue; | |
5711 | } | |
5712 | } | |
5713 | ||
ce34fcd0 | 5714 | (void) ust_app_flush_session(usess); |
8be98f9a MD |
5715 | |
5716 | rcu_read_unlock(); | |
5717 | ||
5718 | return 0; | |
5719 | } | |
5720 | ||
84cd17c6 MD |
5721 | /* |
5722 | * Destroy app UST session. | |
5723 | */ | |
5724 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
5725 | { | |
5726 | int ret = 0; | |
bec39940 | 5727 | struct lttng_ht_iter iter; |
84cd17c6 MD |
5728 | struct ust_app *app; |
5729 | ||
5730 | DBG("Destroy all UST traces"); | |
5731 | ||
5732 | rcu_read_lock(); | |
5733 | ||
852d0037 | 5734 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 5735 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
5736 | if (ret < 0) { |
5737 | /* Continue to next apps even on error */ | |
5738 | continue; | |
5739 | } | |
5740 | } | |
5741 | ||
5742 | rcu_read_unlock(); | |
5743 | ||
5744 | return 0; | |
5745 | } | |
5746 | ||
88e3c2f5 | 5747 | /* The ua_sess lock must be held by the caller. */ |
a9ad0c8f | 5748 | static |
88e3c2f5 JG |
5749 | int find_or_create_ust_app_channel( |
5750 | struct ltt_ust_session *usess, | |
5751 | struct ust_app_session *ua_sess, | |
5752 | struct ust_app *app, | |
5753 | struct ltt_ust_channel *uchan, | |
5754 | struct ust_app_channel **ua_chan) | |
487cf67c | 5755 | { |
55c54cce | 5756 | int ret = 0; |
88e3c2f5 JG |
5757 | struct lttng_ht_iter iter; |
5758 | struct lttng_ht_node_str *ua_chan_node; | |
5759 | ||
5760 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); | |
5761 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
5762 | if (ua_chan_node) { | |
5763 | *ua_chan = caa_container_of(ua_chan_node, | |
5764 | struct ust_app_channel, node); | |
5765 | goto end; | |
5766 | } | |
5767 | ||
5768 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); | |
5769 | if (ret) { | |
5770 | goto end; | |
5771 | } | |
5772 | end: | |
5773 | return ret; | |
5774 | } | |
5775 | ||
5776 | static | |
5777 | int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, | |
f46376a1 | 5778 | struct ltt_ust_event *uevent, |
88e3c2f5 JG |
5779 | struct ust_app *app) |
5780 | { | |
5781 | int ret = 0; | |
5782 | struct ust_app_event *ua_event = NULL; | |
5783 | ||
5784 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
5785 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); | |
5786 | if (!ua_event) { | |
f46376a1 | 5787 | ret = create_ust_app_event(ua_chan, uevent, app); |
88e3c2f5 JG |
5788 | if (ret < 0) { |
5789 | goto end; | |
5790 | } | |
5791 | } else { | |
5792 | if (ua_event->enabled != uevent->enabled) { | |
5793 | ret = uevent->enabled ? | |
f46376a1 MJ |
5794 | enable_ust_app_event(ua_event, app) : |
5795 | disable_ust_app_event(ua_event, app); | |
88e3c2f5 JG |
5796 | } |
5797 | } | |
5798 | ||
5799 | end: | |
5800 | return ret; | |
5801 | } | |
5802 | ||
993578ff JR |
5803 | /* Called with RCU read-side lock held. */ |
5804 | static | |
5805 | void ust_app_synchronize_event_notifier_rules(struct ust_app *app) | |
5806 | { | |
5807 | int ret = 0; | |
5808 | enum lttng_error_code ret_code; | |
5809 | enum lttng_trigger_status t_status; | |
5810 | struct lttng_ht_iter app_trigger_iter; | |
5811 | struct lttng_triggers *triggers = NULL; | |
5812 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5813 | unsigned int count, i; | |
5814 | ||
48b7cdc2 FD |
5815 | ASSERT_RCU_READ_LOCKED(); |
5816 | ||
783db316 MD |
5817 | if (!ust_app_supports_notifiers(app)) { |
5818 | goto end; | |
5819 | } | |
5820 | ||
993578ff JR |
5821 | /* |
5822 | * Currrently, registering or unregistering a trigger with an | |
5823 | * event rule condition causes a full synchronization of the event | |
5824 | * notifiers. | |
5825 | * | |
5826 | * The first step attempts to add an event notifier for all registered | |
5827 | * triggers that apply to the user space tracers. Then, the | |
5828 | * application's event notifiers rules are all checked against the list | |
5829 | * of registered triggers. Any event notifier that doesn't have a | |
5830 | * matching trigger can be assumed to have been disabled. | |
5831 | * | |
5832 | * All of this is inefficient, but is put in place to get the feature | |
5833 | * rolling as it is simpler at this moment. It will be optimized Soon™ | |
5834 | * to allow the state of enabled | |
5835 | * event notifiers to be synchronized in a piece-wise way. | |
5836 | */ | |
5837 | ||
5838 | /* Get all triggers using uid 0 (root) */ | |
5839 | ret_code = notification_thread_command_list_triggers( | |
412d7227 | 5840 | the_notification_thread_handle, 0, &triggers); |
993578ff | 5841 | if (ret_code != LTTNG_OK) { |
993578ff JR |
5842 | goto end; |
5843 | } | |
5844 | ||
a0377dfe | 5845 | LTTNG_ASSERT(triggers); |
993578ff JR |
5846 | |
5847 | t_status = lttng_triggers_get_count(triggers, &count); | |
5848 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
993578ff JR |
5849 | goto end; |
5850 | } | |
5851 | ||
5852 | for (i = 0; i < count; i++) { | |
5853 | struct lttng_condition *condition; | |
5854 | struct lttng_event_rule *event_rule; | |
5855 | struct lttng_trigger *trigger; | |
5856 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; | |
5857 | enum lttng_condition_status condition_status; | |
5858 | uint64_t token; | |
5859 | ||
5860 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); | |
a0377dfe | 5861 | LTTNG_ASSERT(trigger); |
993578ff JR |
5862 | |
5863 | token = lttng_trigger_get_tracer_token(trigger); | |
5864 | condition = lttng_trigger_get_condition(trigger); | |
5865 | ||
8dbb86b8 JR |
5866 | if (lttng_condition_get_type(condition) != |
5867 | LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) { | |
993578ff JR |
5868 | /* Does not apply */ |
5869 | continue; | |
5870 | } | |
5871 | ||
8dbb86b8 JR |
5872 | condition_status = |
5873 | lttng_condition_event_rule_matches_borrow_rule_mutable( | |
5874 | condition, &event_rule); | |
a0377dfe | 5875 | LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK); |
993578ff JR |
5876 | |
5877 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { | |
5878 | /* Skip kernel related triggers. */ | |
5879 | continue; | |
5880 | } | |
5881 | ||
5882 | /* | |
5883 | * Find or create the associated token event rule. The caller | |
5884 | * holds the RCU read lock, so this is safe to call without | |
5885 | * explicitly acquiring it here. | |
5886 | */ | |
5887 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( | |
5888 | app->token_to_event_notifier_rule_ht, token); | |
5889 | if (!looked_up_event_notifier_rule) { | |
267d66aa | 5890 | ret = create_ust_app_event_notifier_rule(trigger, app); |
993578ff JR |
5891 | if (ret < 0) { |
5892 | goto end; | |
5893 | } | |
5894 | } | |
5895 | } | |
5896 | ||
5897 | rcu_read_lock(); | |
5898 | /* Remove all unknown event sources from the app. */ | |
5899 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
5900 | &app_trigger_iter.iter, event_notifier_rule, | |
5901 | node.node) { | |
5902 | const uint64_t app_token = event_notifier_rule->token; | |
5903 | bool found = false; | |
5904 | ||
5905 | /* | |
5906 | * Check if the app event trigger still exists on the | |
5907 | * notification side. | |
5908 | */ | |
5909 | for (i = 0; i < count; i++) { | |
5910 | uint64_t notification_thread_token; | |
5911 | const struct lttng_trigger *trigger = | |
5912 | lttng_triggers_get_at_index( | |
5913 | triggers, i); | |
5914 | ||
a0377dfe | 5915 | LTTNG_ASSERT(trigger); |
993578ff JR |
5916 | |
5917 | notification_thread_token = | |
5918 | lttng_trigger_get_tracer_token(trigger); | |
5919 | ||
5920 | if (notification_thread_token == app_token) { | |
5921 | found = true; | |
5922 | break; | |
5923 | } | |
5924 | } | |
5925 | ||
5926 | if (found) { | |
5927 | /* Still valid. */ | |
5928 | continue; | |
5929 | } | |
5930 | ||
5931 | /* | |
5932 | * This trigger was unregistered, disable it on the tracer's | |
5933 | * side. | |
5934 | */ | |
5935 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, | |
5936 | &app_trigger_iter); | |
a0377dfe | 5937 | LTTNG_ASSERT(ret == 0); |
993578ff JR |
5938 | |
5939 | /* Callee logs errors. */ | |
5940 | (void) disable_ust_object(app, event_notifier_rule->obj); | |
5941 | ||
5942 | delete_ust_app_event_notifier_rule( | |
5943 | app->sock, event_notifier_rule, app); | |
5944 | } | |
5945 | ||
5946 | rcu_read_unlock(); | |
5947 | ||
5948 | end: | |
5949 | lttng_triggers_destroy(triggers); | |
5950 | return; | |
5951 | } | |
5952 | ||
88e3c2f5 | 5953 | /* |
a84d1024 | 5954 | * RCU read lock must be held by the caller. |
88e3c2f5 JG |
5955 | */ |
5956 | static | |
a84d1024 FD |
5957 | void ust_app_synchronize_all_channels(struct ltt_ust_session *usess, |
5958 | struct ust_app_session *ua_sess, | |
88e3c2f5 JG |
5959 | struct ust_app *app) |
5960 | { | |
5961 | int ret = 0; | |
5962 | struct cds_lfht_iter uchan_iter; | |
5963 | struct ltt_ust_channel *uchan; | |
1f3580c7 | 5964 | |
a0377dfe FD |
5965 | LTTNG_ASSERT(usess); |
5966 | LTTNG_ASSERT(ua_sess); | |
5967 | LTTNG_ASSERT(app); | |
48b7cdc2 | 5968 | ASSERT_RCU_READ_LOCKED(); |
ef67c072 | 5969 | |
88e3c2f5 JG |
5970 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, |
5971 | uchan, node.node) { | |
5972 | struct ust_app_channel *ua_chan; | |
5973 | struct cds_lfht_iter uevent_iter; | |
5974 | struct ltt_ust_event *uevent; | |
487cf67c | 5975 | |
31746f93 | 5976 | /* |
88e3c2f5 JG |
5977 | * Search for a matching ust_app_channel. If none is found, |
5978 | * create it. Creating the channel will cause the ua_chan | |
5979 | * structure to be allocated, the channel buffers to be | |
5980 | * allocated (if necessary) and sent to the application, and | |
5981 | * all enabled contexts will be added to the channel. | |
31746f93 | 5982 | */ |
f3db82be | 5983 | ret = find_or_create_ust_app_channel(usess, ua_sess, |
88e3c2f5 JG |
5984 | app, uchan, &ua_chan); |
5985 | if (ret) { | |
5986 | /* Tracer is probably gone or ENOMEM. */ | |
a84d1024 | 5987 | goto end; |
727d5404 DG |
5988 | } |
5989 | ||
88e3c2f5 JG |
5990 | if (!ua_chan) { |
5991 | /* ua_chan will be NULL for the metadata channel */ | |
5992 | continue; | |
5993 | } | |
727d5404 | 5994 | |
88e3c2f5 | 5995 | cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent, |
bec39940 | 5996 | node.node) { |
88e3c2f5 | 5997 | ret = ust_app_channel_synchronize_event(ua_chan, |
f46376a1 | 5998 | uevent, app); |
88e3c2f5 | 5999 | if (ret) { |
a84d1024 | 6000 | goto end; |
487cf67c | 6001 | } |
36dc12cc | 6002 | } |
d0b96690 | 6003 | |
88e3c2f5 JG |
6004 | if (ua_chan->enabled != uchan->enabled) { |
6005 | ret = uchan->enabled ? | |
6006 | enable_ust_app_channel(ua_sess, uchan, app) : | |
6007 | disable_ust_app_channel(ua_sess, ua_chan, app); | |
6008 | if (ret) { | |
a84d1024 | 6009 | goto end; |
88e3c2f5 JG |
6010 | } |
6011 | } | |
36dc12cc | 6012 | } |
a84d1024 FD |
6013 | end: |
6014 | return; | |
6015 | } | |
6016 | ||
6017 | /* | |
6018 | * The caller must ensure that the application is compatible and is tracked | |
6019 | * by the process attribute trackers. | |
6020 | */ | |
6021 | static | |
6022 | void ust_app_synchronize(struct ltt_ust_session *usess, | |
6023 | struct ust_app *app) | |
6024 | { | |
6025 | int ret = 0; | |
6026 | struct ust_app_session *ua_sess = NULL; | |
6027 | ||
6028 | /* | |
6029 | * The application's configuration should only be synchronized for | |
6030 | * active sessions. | |
6031 | */ | |
a0377dfe | 6032 | LTTNG_ASSERT(usess->active); |
a84d1024 FD |
6033 | |
6034 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL); | |
6035 | if (ret < 0) { | |
6036 | /* Tracer is probably gone or ENOMEM. */ | |
50f71cad FD |
6037 | if (ua_sess) { |
6038 | destroy_app_session(app, ua_sess); | |
6039 | } | |
6040 | goto end; | |
a84d1024 | 6041 | } |
a0377dfe | 6042 | LTTNG_ASSERT(ua_sess); |
a84d1024 FD |
6043 | |
6044 | pthread_mutex_lock(&ua_sess->lock); | |
6045 | if (ua_sess->deleted) { | |
50f71cad | 6046 | goto deleted_session; |
a84d1024 FD |
6047 | } |
6048 | ||
6049 | rcu_read_lock(); | |
6050 | ||
6051 | ust_app_synchronize_all_channels(usess, ua_sess, app); | |
ef67c072 JG |
6052 | |
6053 | /* | |
6054 | * Create the metadata for the application. This returns gracefully if a | |
6055 | * metadata was already set for the session. | |
6056 | * | |
6057 | * The metadata channel must be created after the data channels as the | |
6058 | * consumer daemon assumes this ordering. When interacting with a relay | |
6059 | * daemon, the consumer will use this assumption to send the | |
6060 | * "STREAMS_SENT" message to the relay daemon. | |
6061 | */ | |
6062 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); | |
6063 | if (ret < 0) { | |
50f71cad FD |
6064 | ERR("Metadata creation failed for app sock %d for session id %" PRIu64, |
6065 | app->sock, usess->id); | |
ef67c072 JG |
6066 | } |
6067 | ||
88e3c2f5 | 6068 | rcu_read_unlock(); |
0498a00c | 6069 | |
50f71cad | 6070 | deleted_session: |
d0b96690 | 6071 | pthread_mutex_unlock(&ua_sess->lock); |
50f71cad | 6072 | end: |
487cf67c DG |
6073 | return; |
6074 | } | |
55cc08a6 | 6075 | |
a9ad0c8f MD |
6076 | static |
6077 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) | |
6078 | { | |
6079 | struct ust_app_session *ua_sess; | |
6080 | ||
6081 | ua_sess = lookup_session_by_app(usess, app); | |
6082 | if (ua_sess == NULL) { | |
6083 | return; | |
6084 | } | |
6085 | destroy_app_session(app, ua_sess); | |
6086 | } | |
6087 | ||
6088 | /* | |
6089 | * Add channels/events from UST global domain to registered apps at sock. | |
6090 | * | |
6091 | * Called with session lock held. | |
6092 | * Called with RCU read-side lock held. | |
6093 | */ | |
6094 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) | |
6095 | { | |
a0377dfe FD |
6096 | LTTNG_ASSERT(usess); |
6097 | LTTNG_ASSERT(usess->active); | |
48b7cdc2 | 6098 | ASSERT_RCU_READ_LOCKED(); |
a9ad0c8f MD |
6099 | |
6100 | DBG2("UST app global update for app sock %d for session id %" PRIu64, | |
6101 | app->sock, usess->id); | |
6102 | ||
6103 | if (!app->compatible) { | |
6104 | return; | |
6105 | } | |
159b042f JG |
6106 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, |
6107 | usess, app->pid) && | |
55c9e7ca | 6108 | trace_ust_id_tracker_lookup( |
159b042f JG |
6109 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, |
6110 | usess, app->uid) && | |
55c9e7ca | 6111 | trace_ust_id_tracker_lookup( |
159b042f JG |
6112 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, |
6113 | usess, app->gid)) { | |
88e3c2f5 JG |
6114 | /* |
6115 | * Synchronize the application's internal tracing configuration | |
6116 | * and start tracing. | |
6117 | */ | |
6118 | ust_app_synchronize(usess, app); | |
6119 | ust_app_start_trace(usess, app); | |
a9ad0c8f MD |
6120 | } else { |
6121 | ust_app_global_destroy(usess, app); | |
6122 | } | |
6123 | } | |
6124 | ||
993578ff JR |
6125 | /* |
6126 | * Add all event notifiers to an application. | |
6127 | * | |
6128 | * Called with session lock held. | |
6129 | * Called with RCU read-side lock held. | |
6130 | */ | |
6131 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) | |
6132 | { | |
48b7cdc2 FD |
6133 | ASSERT_RCU_READ_LOCKED(); |
6134 | ||
9324443a | 6135 | DBG2("UST application global event notifier rules update: app = '%s', pid = %d", |
be355079 | 6136 | app->name, app->pid); |
993578ff | 6137 | |
783db316 | 6138 | if (!app->compatible || !ust_app_supports_notifiers(app)) { |
993578ff JR |
6139 | return; |
6140 | } | |
6141 | ||
6142 | if (app->event_notifier_group.object == NULL) { | |
9324443a | 6143 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d", |
be355079 | 6144 | app->name, app->pid); |
993578ff JR |
6145 | return; |
6146 | } | |
6147 | ||
6148 | ust_app_synchronize_event_notifier_rules(app); | |
6149 | } | |
6150 | ||
a9ad0c8f MD |
6151 | /* |
6152 | * Called with session lock held. | |
6153 | */ | |
6154 | void ust_app_global_update_all(struct ltt_ust_session *usess) | |
6155 | { | |
6156 | struct lttng_ht_iter iter; | |
6157 | struct ust_app *app; | |
6158 | ||
6159 | rcu_read_lock(); | |
6160 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6161 | ust_app_global_update(usess, app); | |
6162 | } | |
6163 | rcu_read_unlock(); | |
6164 | } | |
6165 | ||
993578ff JR |
6166 | void ust_app_global_update_all_event_notifier_rules(void) |
6167 | { | |
6168 | struct lttng_ht_iter iter; | |
6169 | struct ust_app *app; | |
6170 | ||
6171 | rcu_read_lock(); | |
6172 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6173 | ust_app_global_update_event_notifier_rules(app); | |
6174 | } | |
6175 | ||
6176 | rcu_read_unlock(); | |
6177 | } | |
6178 | ||
55cc08a6 DG |
6179 | /* |
6180 | * Add context to a specific channel for global UST domain. | |
6181 | */ | |
6182 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
6183 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
6184 | { | |
6185 | int ret = 0; | |
bec39940 DG |
6186 | struct lttng_ht_node_str *ua_chan_node; |
6187 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
6188 | struct ust_app_channel *ua_chan = NULL; |
6189 | struct ust_app_session *ua_sess; | |
6190 | struct ust_app *app; | |
6191 | ||
a0377dfe | 6192 | LTTNG_ASSERT(usess->active); |
0498a00c | 6193 | |
55cc08a6 | 6194 | rcu_read_lock(); |
852d0037 | 6195 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
6196 | if (!app->compatible) { |
6197 | /* | |
6198 | * TODO: In time, we should notice the caller of this error by | |
6199 | * telling him that this is a version error. | |
6200 | */ | |
6201 | continue; | |
6202 | } | |
55cc08a6 DG |
6203 | ua_sess = lookup_session_by_app(usess, app); |
6204 | if (ua_sess == NULL) { | |
6205 | continue; | |
6206 | } | |
6207 | ||
d0b96690 | 6208 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
6209 | |
6210 | if (ua_sess->deleted) { | |
6211 | pthread_mutex_unlock(&ua_sess->lock); | |
6212 | continue; | |
6213 | } | |
6214 | ||
55cc08a6 | 6215 | /* Lookup channel in the ust app session */ |
bec39940 DG |
6216 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
6217 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 | 6218 | if (ua_chan_node == NULL) { |
d0b96690 | 6219 | goto next_app; |
55cc08a6 DG |
6220 | } |
6221 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
6222 | node); | |
c9edf082 | 6223 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
55cc08a6 | 6224 | if (ret < 0) { |
d0b96690 | 6225 | goto next_app; |
55cc08a6 | 6226 | } |
d0b96690 DG |
6227 | next_app: |
6228 | pthread_mutex_unlock(&ua_sess->lock); | |
55cc08a6 DG |
6229 | } |
6230 | ||
55cc08a6 | 6231 | rcu_read_unlock(); |
76d45b40 DG |
6232 | return ret; |
6233 | } | |
7f79d3a1 | 6234 | |
d0b96690 DG |
6235 | /* |
6236 | * Receive registration and populate the given msg structure. | |
6237 | * | |
6238 | * On success return 0 else a negative value returned by the ustctl call. | |
6239 | */ | |
6240 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) | |
6241 | { | |
6242 | int ret; | |
6243 | uint32_t pid, ppid, uid, gid; | |
6244 | ||
a0377dfe | 6245 | LTTNG_ASSERT(msg); |
d0b96690 | 6246 | |
b623cb6a | 6247 | ret = lttng_ust_ctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, |
d0b96690 DG |
6248 | &pid, &ppid, &uid, &gid, |
6249 | &msg->bits_per_long, | |
6250 | &msg->uint8_t_alignment, | |
6251 | &msg->uint16_t_alignment, | |
6252 | &msg->uint32_t_alignment, | |
6253 | &msg->uint64_t_alignment, | |
6254 | &msg->long_alignment, | |
6255 | &msg->byte_order, | |
6256 | msg->name); | |
6257 | if (ret < 0) { | |
6258 | switch (-ret) { | |
6259 | case EPIPE: | |
6260 | case ECONNRESET: | |
6261 | case LTTNG_UST_ERR_EXITING: | |
6262 | DBG3("UST app recv reg message failed. Application died"); | |
6263 | break; | |
6264 | case LTTNG_UST_ERR_UNSUP_MAJOR: | |
6265 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", | |
6266 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, | |
6267 | LTTNG_UST_ABI_MINOR_VERSION); | |
6268 | break; | |
6269 | default: | |
6270 | ERR("UST app recv reg message failed with ret %d", ret); | |
6271 | break; | |
6272 | } | |
6273 | goto error; | |
6274 | } | |
6275 | msg->pid = (pid_t) pid; | |
6276 | msg->ppid = (pid_t) ppid; | |
6277 | msg->uid = (uid_t) uid; | |
6278 | msg->gid = (gid_t) gid; | |
6279 | ||
6280 | error: | |
6281 | return ret; | |
6282 | } | |
6283 | ||
10b56aef MD |
6284 | /* |
6285 | * Return a ust app session object using the application object and the | |
6286 | * session object descriptor has a key. If not found, NULL is returned. | |
6287 | * A RCU read side lock MUST be acquired when calling this function. | |
6288 | */ | |
6289 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, | |
6290 | int objd) | |
6291 | { | |
6292 | struct lttng_ht_node_ulong *node; | |
6293 | struct lttng_ht_iter iter; | |
6294 | struct ust_app_session *ua_sess = NULL; | |
6295 | ||
a0377dfe | 6296 | LTTNG_ASSERT(app); |
48b7cdc2 | 6297 | ASSERT_RCU_READ_LOCKED(); |
10b56aef MD |
6298 | |
6299 | lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter); | |
6300 | node = lttng_ht_iter_get_node_ulong(&iter); | |
6301 | if (node == NULL) { | |
6302 | DBG2("UST app session find by objd %d not found", objd); | |
6303 | goto error; | |
6304 | } | |
6305 | ||
6306 | ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node); | |
6307 | ||
6308 | error: | |
6309 | return ua_sess; | |
6310 | } | |
6311 | ||
d88aee68 DG |
6312 | /* |
6313 | * Return a ust app channel object using the application object and the channel | |
6314 | * object descriptor has a key. If not found, NULL is returned. A RCU read side | |
6315 | * lock MUST be acquired before calling this function. | |
6316 | */ | |
d0b96690 DG |
6317 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
6318 | int objd) | |
6319 | { | |
6320 | struct lttng_ht_node_ulong *node; | |
6321 | struct lttng_ht_iter iter; | |
6322 | struct ust_app_channel *ua_chan = NULL; | |
6323 | ||
a0377dfe | 6324 | LTTNG_ASSERT(app); |
48b7cdc2 | 6325 | ASSERT_RCU_READ_LOCKED(); |
d0b96690 DG |
6326 | |
6327 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); | |
6328 | node = lttng_ht_iter_get_node_ulong(&iter); | |
6329 | if (node == NULL) { | |
6330 | DBG2("UST app channel find by objd %d not found", objd); | |
6331 | goto error; | |
6332 | } | |
6333 | ||
6334 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); | |
6335 | ||
6336 | error: | |
6337 | return ua_chan; | |
6338 | } | |
6339 | ||
c559ee63 MD |
6340 | /* |
6341 | * Fixup legacy context fields for comparison: | |
6342 | * - legacy array becomes array_nestable, | |
6343 | * - legacy struct becomes struct_nestable, | |
6344 | * - legacy variant becomes variant_nestable, | |
6345 | * legacy sequences are not emitted in LTTng-UST contexts. | |
6346 | */ | |
6347 | static int ust_app_fixup_legacy_context_fields(size_t *_nr_fields, | |
6348 | struct lttng_ust_ctl_field **_fields) | |
6349 | { | |
6350 | struct lttng_ust_ctl_field *fields = *_fields, *new_fields = NULL; | |
6351 | size_t nr_fields = *_nr_fields, new_nr_fields = 0, i, j; | |
6352 | bool found = false; | |
6353 | int ret = 0; | |
6354 | ||
6355 | for (i = 0; i < nr_fields; i++) { | |
6356 | const struct lttng_ust_ctl_field *field = &fields[i]; | |
6357 | ||
6358 | switch (field->type.atype) { | |
6359 | case lttng_ust_ctl_atype_sequence: | |
6360 | ERR("Unexpected legacy sequence context."); | |
6361 | ret = -EINVAL; | |
6362 | goto end; | |
6363 | case lttng_ust_ctl_atype_array: | |
6364 | switch (field->type.u.legacy.array.elem_type.atype) { | |
6365 | case lttng_ust_ctl_atype_integer: | |
6366 | break; | |
6367 | default: | |
6368 | ERR("Unexpected legacy array element type in context."); | |
6369 | ret = -EINVAL; | |
6370 | goto end; | |
6371 | } | |
6372 | found = true; | |
6373 | /* One field for array_nested, one field for elem type. */ | |
6374 | new_nr_fields += 2; | |
6375 | break; | |
6376 | ||
6377 | case lttng_ust_ctl_atype_struct: /* Fallthrough */ | |
6378 | case lttng_ust_ctl_atype_variant: | |
6379 | found = true; | |
6380 | new_nr_fields++; | |
6381 | break; | |
6382 | default: | |
6383 | new_nr_fields++; | |
6384 | break; | |
6385 | } | |
6386 | } | |
6387 | if (!found) { | |
6388 | goto end; | |
6389 | } | |
64803277 SM |
6390 | |
6391 | new_fields = calloc<lttng_ust_ctl_field>(new_nr_fields); | |
c559ee63 MD |
6392 | if (!new_fields) { |
6393 | ret = -ENOMEM; | |
6394 | goto end; | |
6395 | } | |
64803277 | 6396 | |
c559ee63 MD |
6397 | for (i = 0, j = 0; i < nr_fields; i++, j++) { |
6398 | const struct lttng_ust_ctl_field *field = &fields[i]; | |
6399 | struct lttng_ust_ctl_field *new_field = &new_fields[j]; | |
6400 | ||
6401 | switch (field->type.atype) { | |
6402 | case lttng_ust_ctl_atype_array: | |
6403 | /* One field for array_nested, one field for elem type. */ | |
6404 | strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1); | |
6405 | new_field->type.atype = lttng_ust_ctl_atype_array_nestable; | |
6406 | new_field->type.u.array_nestable.length = field->type.u.legacy.array.length; | |
6407 | new_field->type.u.array_nestable.alignment = 0; | |
6408 | new_field = &new_fields[++j]; /* elem type */ | |
6409 | new_field->type.atype = field->type.u.legacy.array.elem_type.atype; | |
6410 | assert(new_field->type.atype == lttng_ust_ctl_atype_integer); | |
6411 | new_field->type.u.integer = field->type.u.legacy.array.elem_type.u.basic.integer; | |
6412 | break; | |
6413 | case lttng_ust_ctl_atype_struct: | |
6414 | strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1); | |
6415 | new_field->type.atype = lttng_ust_ctl_atype_struct_nestable; | |
6416 | new_field->type.u.struct_nestable.nr_fields = field->type.u.legacy._struct.nr_fields; | |
6417 | new_field->type.u.struct_nestable.alignment = 0; | |
6418 | break; | |
6419 | case lttng_ust_ctl_atype_variant: | |
6420 | strncpy(new_field->name, field->name, LTTNG_UST_ABI_SYM_NAME_LEN - 1); | |
6421 | new_field->type.atype = lttng_ust_ctl_atype_variant_nestable; | |
6422 | new_field->type.u.variant_nestable.nr_choices = field->type.u.legacy.variant.nr_choices; | |
6423 | strncpy(new_field->type.u.variant_nestable.tag_name, | |
6424 | field->type.u.legacy.variant.tag_name, | |
6425 | LTTNG_UST_ABI_SYM_NAME_LEN - 1); | |
6426 | new_field->type.u.variant_nestable.alignment = 0; | |
6427 | break; | |
6428 | default: | |
6429 | *new_field = *field; | |
6430 | break; | |
6431 | } | |
6432 | } | |
6433 | free(fields); | |
6434 | *_fields = new_fields; | |
6435 | *_nr_fields = new_nr_fields; | |
6436 | end: | |
6437 | return ret; | |
6438 | } | |
6439 | ||
d88aee68 DG |
6440 | /* |
6441 | * Reply to a register channel notification from an application on the notify | |
6442 | * socket. The channel metadata is also created. | |
6443 | * | |
6444 | * The session UST registry lock is acquired in this function. | |
6445 | * | |
6446 | * On success 0 is returned else a negative value. | |
6447 | */ | |
8eede835 | 6448 | static int reply_ust_register_channel(int sock, int cobjd, |
b623cb6a | 6449 | size_t nr_fields, struct lttng_ust_ctl_field *fields) |
d0b96690 DG |
6450 | { |
6451 | int ret, ret_code = 0; | |
294e218e | 6452 | uint32_t chan_id; |
7972aab2 | 6453 | uint64_t chan_reg_key; |
c559ee63 | 6454 | enum lttng_ust_ctl_channel_header type = LTTNG_UST_CTL_CHANNEL_HEADER_UNKNOWN; |
d0b96690 DG |
6455 | struct ust_app *app; |
6456 | struct ust_app_channel *ua_chan; | |
6457 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 6458 | ust_registry_session *registry; |
3273699d | 6459 | struct ust_registry_channel *ust_reg_chan; |
d0b96690 DG |
6460 | |
6461 | rcu_read_lock(); | |
6462 | ||
6463 | /* Lookup application. If not found, there is a code flow error. */ | |
6464 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6465 | if (!app) { |
fad1ed2f | 6466 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 | 6467 | sock); |
48c77bf7 | 6468 | ret = -1; |
d88aee68 DG |
6469 | goto error_rcu_unlock; |
6470 | } | |
d0b96690 | 6471 | |
4950b860 | 6472 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6473 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6474 | if (!ua_chan) { |
fad1ed2f | 6475 | DBG("Application channel is being torn down. Abort event notify"); |
4950b860 MD |
6476 | ret = 0; |
6477 | goto error_rcu_unlock; | |
6478 | } | |
6479 | ||
a0377dfe | 6480 | LTTNG_ASSERT(ua_chan->session); |
d0b96690 | 6481 | ua_sess = ua_chan->session; |
d0b96690 | 6482 | |
7972aab2 DG |
6483 | /* Get right session registry depending on the session buffer type. */ |
6484 | registry = get_session_registry(ua_sess); | |
fad1ed2f JR |
6485 | if (!registry) { |
6486 | DBG("Application session is being torn down. Abort event notify"); | |
6487 | ret = 0; | |
6488 | goto error_rcu_unlock; | |
6489 | }; | |
45893984 | 6490 | |
7972aab2 DG |
6491 | /* Depending on the buffer type, a different channel key is used. */ |
6492 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
6493 | chan_reg_key = ua_chan->tracing_channel_id; | |
d0b96690 | 6494 | } else { |
7972aab2 | 6495 | chan_reg_key = ua_chan->key; |
d0b96690 DG |
6496 | } |
6497 | ||
aeeb48c6 | 6498 | pthread_mutex_lock(®istry->_lock); |
7972aab2 | 6499 | |
3273699d | 6500 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
a0377dfe | 6501 | LTTNG_ASSERT(ust_reg_chan); |
7972aab2 | 6502 | |
fb277293 MD |
6503 | /* Channel id is set during the object creation. */ |
6504 | chan_id = ust_reg_chan->chan_id; | |
6505 | ||
c559ee63 MD |
6506 | ret = ust_app_fixup_legacy_context_fields(&nr_fields, &fields); |
6507 | if (ret < 0) { | |
6508 | ERR("Registering application channel due to legacy context fields fixup error: pid = %d, sock = %d", | |
6509 | app->pid, app->sock); | |
6510 | ret_code = -EINVAL; | |
6511 | goto reply; | |
6512 | } | |
3273699d | 6513 | if (!ust_reg_chan->register_done) { |
294e218e MD |
6514 | /* |
6515 | * TODO: eventually use the registry event count for | |
6516 | * this channel to better guess header type for per-pid | |
6517 | * buffers. | |
6518 | */ | |
b623cb6a | 6519 | type = LTTNG_UST_CTL_CHANNEL_HEADER_LARGE; |
3273699d FD |
6520 | ust_reg_chan->nr_ctx_fields = nr_fields; |
6521 | ust_reg_chan->ctx_fields = fields; | |
fad1ed2f | 6522 | fields = NULL; |
3273699d | 6523 | ust_reg_chan->header_type = type; |
d0b96690 | 6524 | } else { |
7972aab2 | 6525 | /* Get current already assigned values. */ |
3273699d | 6526 | type = ust_reg_chan->header_type; |
fb277293 MD |
6527 | /* |
6528 | * Validate that the context fields match between | |
6529 | * registry and newcoming application. | |
6530 | */ | |
6531 | if (!match_lttng_ust_ctl_field_array(ust_reg_chan->ctx_fields, | |
6532 | ust_reg_chan->nr_ctx_fields, | |
6533 | fields, nr_fields)) { | |
6534 | ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d", | |
6535 | app->pid, app->sock); | |
6536 | ret_code = -EINVAL; | |
6537 | goto reply; | |
6538 | } | |
d0b96690 | 6539 | } |
d0b96690 DG |
6540 | |
6541 | /* Append to metadata */ | |
3273699d FD |
6542 | if (!ust_reg_chan->metadata_dumped) { |
6543 | ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan); | |
d0b96690 DG |
6544 | if (ret_code) { |
6545 | ERR("Error appending channel metadata (errno = %d)", ret_code); | |
6546 | goto reply; | |
6547 | } | |
6548 | } | |
6549 | ||
6550 | reply: | |
7972aab2 | 6551 | DBG3("UST app replying to register channel key %" PRIu64 |
be355079 | 6552 | " with id %u, type = %d, ret = %d", chan_reg_key, chan_id, type, |
7972aab2 | 6553 | ret_code); |
d0b96690 | 6554 | |
b623cb6a | 6555 | ret = lttng_ust_ctl_reply_register_channel(sock, chan_id, type, ret_code); |
d0b96690 | 6556 | if (ret < 0) { |
be355079 JR |
6557 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6558 | DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d", | |
6559 | app->pid, app->sock); | |
6560 | } else if (ret == -EAGAIN) { | |
6561 | WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d", | |
6562 | app->pid, app->sock); | |
d0b96690 | 6563 | } else { |
be355079 JR |
6564 | ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d", |
6565 | ret, app->pid, app->sock); | |
d0b96690 DG |
6566 | } |
6567 | goto error; | |
6568 | } | |
6569 | ||
7972aab2 | 6570 | /* This channel registry registration is completed. */ |
3273699d | 6571 | ust_reg_chan->register_done = 1; |
7972aab2 | 6572 | |
d0b96690 | 6573 | error: |
aeeb48c6 | 6574 | pthread_mutex_unlock(®istry->_lock); |
d88aee68 | 6575 | error_rcu_unlock: |
d0b96690 | 6576 | rcu_read_unlock(); |
fad1ed2f | 6577 | free(fields); |
d0b96690 DG |
6578 | return ret; |
6579 | } | |
6580 | ||
d88aee68 DG |
6581 | /* |
6582 | * Add event to the UST channel registry. When the event is added to the | |
6583 | * registry, the metadata is also created. Once done, this replies to the | |
6584 | * application with the appropriate error code. | |
6585 | * | |
6586 | * The session UST registry lock is acquired in the function. | |
6587 | * | |
6588 | * On success 0 is returned else a negative value. | |
6589 | */ | |
d0b96690 | 6590 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
b623cb6a | 6591 | char *sig, size_t nr_fields, struct lttng_ust_ctl_field *fields, |
2106efa0 | 6592 | int loglevel_value, char *model_emf_uri) |
d0b96690 DG |
6593 | { |
6594 | int ret, ret_code; | |
6595 | uint32_t event_id = 0; | |
7972aab2 | 6596 | uint64_t chan_reg_key; |
d0b96690 DG |
6597 | struct ust_app *app; |
6598 | struct ust_app_channel *ua_chan; | |
6599 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 6600 | ust_registry_session *registry; |
d0b96690 DG |
6601 | |
6602 | rcu_read_lock(); | |
6603 | ||
6604 | /* Lookup application. If not found, there is a code flow error. */ | |
6605 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 6606 | if (!app) { |
fad1ed2f | 6607 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 | 6608 | sock); |
48c77bf7 | 6609 | ret = -1; |
d88aee68 DG |
6610 | goto error_rcu_unlock; |
6611 | } | |
d0b96690 | 6612 | |
4950b860 | 6613 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 6614 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 6615 | if (!ua_chan) { |
fad1ed2f | 6616 | DBG("Application channel is being torn down. Abort event notify"); |
4950b860 MD |
6617 | ret = 0; |
6618 | goto error_rcu_unlock; | |
6619 | } | |
6620 | ||
a0377dfe | 6621 | LTTNG_ASSERT(ua_chan->session); |
d0b96690 DG |
6622 | ua_sess = ua_chan->session; |
6623 | ||
7972aab2 | 6624 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
6625 | if (!registry) { |
6626 | DBG("Application session is being torn down. Abort event notify"); | |
6627 | ret = 0; | |
6628 | goto error_rcu_unlock; | |
6629 | } | |
7972aab2 DG |
6630 | |
6631 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
6632 | chan_reg_key = ua_chan->tracing_channel_id; | |
6633 | } else { | |
6634 | chan_reg_key = ua_chan->key; | |
6635 | } | |
6636 | ||
aeeb48c6 | 6637 | pthread_mutex_lock(®istry->_lock); |
d0b96690 | 6638 | |
d5d629b5 DG |
6639 | /* |
6640 | * From this point on, this call acquires the ownership of the sig, fields | |
6641 | * and model_emf_uri meaning any free are done inside it if needed. These | |
6642 | * three variables MUST NOT be read/write after this. | |
6643 | */ | |
7972aab2 | 6644 | ret_code = ust_registry_create_event(registry, chan_reg_key, |
2106efa0 PP |
6645 | sobjd, cobjd, name, sig, nr_fields, fields, |
6646 | loglevel_value, model_emf_uri, ua_sess->buffer_type, | |
6647 | &event_id, app); | |
fad1ed2f JR |
6648 | sig = NULL; |
6649 | fields = NULL; | |
6650 | model_emf_uri = NULL; | |
d0b96690 DG |
6651 | |
6652 | /* | |
6653 | * The return value is returned to ustctl so in case of an error, the | |
6654 | * application can be notified. In case of an error, it's important not to | |
6655 | * return a negative error or else the application will get closed. | |
6656 | */ | |
b623cb6a | 6657 | ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code); |
d0b96690 | 6658 | if (ret < 0) { |
be355079 JR |
6659 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6660 | DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.", | |
6661 | app->pid, app->sock); | |
6662 | } else if (ret == -EAGAIN) { | |
6663 | WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d", | |
6664 | app->pid, app->sock); | |
d0b96690 | 6665 | } else { |
be355079 JR |
6666 | ERR("UST app reply event failed with ret %d: pid = %d, sock = %d", |
6667 | ret, app->pid, app->sock); | |
d0b96690 DG |
6668 | } |
6669 | /* | |
6670 | * No need to wipe the create event since the application socket will | |
6671 | * get close on error hence cleaning up everything by itself. | |
6672 | */ | |
6673 | goto error; | |
6674 | } | |
6675 | ||
7972aab2 DG |
6676 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
6677 | name, event_id); | |
d88aee68 | 6678 | |
d0b96690 | 6679 | error: |
aeeb48c6 | 6680 | pthread_mutex_unlock(®istry->_lock); |
d88aee68 | 6681 | error_rcu_unlock: |
d0b96690 | 6682 | rcu_read_unlock(); |
fad1ed2f JR |
6683 | free(sig); |
6684 | free(fields); | |
6685 | free(model_emf_uri); | |
d0b96690 DG |
6686 | return ret; |
6687 | } | |
6688 | ||
10b56aef MD |
6689 | /* |
6690 | * Add enum to the UST session registry. Once done, this replies to the | |
6691 | * application with the appropriate error code. | |
6692 | * | |
6693 | * The session UST registry lock is acquired within this function. | |
6694 | * | |
6695 | * On success 0 is returned else a negative value. | |
6696 | */ | |
6697 | static int add_enum_ust_registry(int sock, int sobjd, char *name, | |
b623cb6a | 6698 | struct lttng_ust_ctl_enum_entry *entries, size_t nr_entries) |
10b56aef MD |
6699 | { |
6700 | int ret = 0, ret_code; | |
6701 | struct ust_app *app; | |
6702 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 6703 | ust_registry_session *registry; |
10b56aef MD |
6704 | uint64_t enum_id = -1ULL; |
6705 | ||
6706 | rcu_read_lock(); | |
6707 | ||
6708 | /* Lookup application. If not found, there is a code flow error. */ | |
6709 | app = find_app_by_notify_sock(sock); | |
6710 | if (!app) { | |
6711 | /* Return an error since this is not an error */ | |
6712 | DBG("Application socket %d is being torn down. Aborting enum registration", | |
6713 | sock); | |
6714 | free(entries); | |
48c77bf7 | 6715 | ret = -1; |
10b56aef MD |
6716 | goto error_rcu_unlock; |
6717 | } | |
6718 | ||
6719 | /* Lookup session by UST object descriptor. */ | |
6720 | ua_sess = find_session_by_objd(app, sobjd); | |
6721 | if (!ua_sess) { | |
6722 | /* Return an error since this is not an error */ | |
acfb63a8 | 6723 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
10b56aef MD |
6724 | free(entries); |
6725 | goto error_rcu_unlock; | |
6726 | } | |
6727 | ||
6728 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 6729 | if (!registry) { |
acfb63a8 | 6730 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); |
fad1ed2f JR |
6731 | free(entries); |
6732 | goto error_rcu_unlock; | |
6733 | } | |
10b56aef | 6734 | |
aeeb48c6 | 6735 | pthread_mutex_lock(®istry->_lock); |
10b56aef MD |
6736 | |
6737 | /* | |
6738 | * From this point on, the callee acquires the ownership of | |
6739 | * entries. The variable entries MUST NOT be read/written after | |
6740 | * call. | |
6741 | */ | |
6742 | ret_code = ust_registry_create_or_find_enum(registry, sobjd, name, | |
6743 | entries, nr_entries, &enum_id); | |
6744 | entries = NULL; | |
6745 | ||
6746 | /* | |
6747 | * The return value is returned to ustctl so in case of an error, the | |
6748 | * application can be notified. In case of an error, it's important not to | |
6749 | * return a negative error or else the application will get closed. | |
6750 | */ | |
b623cb6a | 6751 | ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, ret_code); |
10b56aef | 6752 | if (ret < 0) { |
be355079 JR |
6753 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6754 | DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d", | |
6755 | app->pid, app->sock); | |
6756 | } else if (ret == -EAGAIN) { | |
6757 | WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d", | |
6758 | app->pid, app->sock); | |
10b56aef | 6759 | } else { |
be355079 JR |
6760 | ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d", |
6761 | ret, app->pid, app->sock); | |
10b56aef MD |
6762 | } |
6763 | /* | |
6764 | * No need to wipe the create enum since the application socket will | |
6765 | * get close on error hence cleaning up everything by itself. | |
6766 | */ | |
6767 | goto error; | |
6768 | } | |
6769 | ||
6770 | DBG3("UST registry enum %s added successfully or already found", name); | |
6771 | ||
6772 | error: | |
aeeb48c6 | 6773 | pthread_mutex_unlock(®istry->_lock); |
10b56aef MD |
6774 | error_rcu_unlock: |
6775 | rcu_read_unlock(); | |
6776 | return ret; | |
6777 | } | |
6778 | ||
d88aee68 DG |
6779 | /* |
6780 | * Handle application notification through the given notify socket. | |
6781 | * | |
6782 | * Return 0 on success or else a negative value. | |
6783 | */ | |
d0b96690 DG |
6784 | int ust_app_recv_notify(int sock) |
6785 | { | |
6786 | int ret; | |
b623cb6a | 6787 | enum lttng_ust_ctl_notify_cmd cmd; |
d0b96690 DG |
6788 | |
6789 | DBG3("UST app receiving notify from sock %d", sock); | |
6790 | ||
b623cb6a | 6791 | ret = lttng_ust_ctl_recv_notify(sock, &cmd); |
d0b96690 | 6792 | if (ret < 0) { |
be355079 JR |
6793 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6794 | DBG3("UST app recv notify failed. Application died: sock = %d", | |
6795 | sock); | |
6796 | } else if (ret == -EAGAIN) { | |
6797 | WARN("UST app recv notify failed. Communication time out: sock = %d", | |
6798 | sock); | |
d0b96690 | 6799 | } else { |
be355079 JR |
6800 | ERR("UST app recv notify failed with ret %d: sock = %d", |
6801 | ret, sock); | |
d0b96690 DG |
6802 | } |
6803 | goto error; | |
6804 | } | |
6805 | ||
6806 | switch (cmd) { | |
b623cb6a | 6807 | case LTTNG_UST_CTL_NOTIFY_CMD_EVENT: |
d0b96690 | 6808 | { |
2106efa0 | 6809 | int sobjd, cobjd, loglevel_value; |
fc4b93fa | 6810 | char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri; |
d0b96690 | 6811 | size_t nr_fields; |
b623cb6a | 6812 | struct lttng_ust_ctl_field *fields; |
d0b96690 DG |
6813 | |
6814 | DBG2("UST app ustctl register event received"); | |
6815 | ||
b623cb6a | 6816 | ret = lttng_ust_ctl_recv_register_event(sock, &sobjd, &cobjd, name, |
2106efa0 PP |
6817 | &loglevel_value, &sig, &nr_fields, &fields, |
6818 | &model_emf_uri); | |
d0b96690 | 6819 | if (ret < 0) { |
be355079 JR |
6820 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6821 | DBG3("UST app recv event failed. Application died: sock = %d", | |
6822 | sock); | |
6823 | } else if (ret == -EAGAIN) { | |
6824 | WARN("UST app recv event failed. Communication time out: sock = %d", | |
6825 | sock); | |
d0b96690 | 6826 | } else { |
be355079 JR |
6827 | ERR("UST app recv event failed with ret %d: sock = %d", |
6828 | ret, sock); | |
d0b96690 DG |
6829 | } |
6830 | goto error; | |
6831 | } | |
6832 | ||
d5d629b5 DG |
6833 | /* |
6834 | * Add event to the UST registry coming from the notify socket. This | |
6835 | * call will free if needed the sig, fields and model_emf_uri. This | |
6836 | * code path loses the ownsership of these variables and transfer them | |
6837 | * to the this function. | |
6838 | */ | |
d0b96690 | 6839 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
2106efa0 | 6840 | fields, loglevel_value, model_emf_uri); |
d0b96690 DG |
6841 | if (ret < 0) { |
6842 | goto error; | |
6843 | } | |
6844 | ||
6845 | break; | |
6846 | } | |
b623cb6a | 6847 | case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL: |
d0b96690 DG |
6848 | { |
6849 | int sobjd, cobjd; | |
6850 | size_t nr_fields; | |
b623cb6a | 6851 | struct lttng_ust_ctl_field *fields; |
d0b96690 DG |
6852 | |
6853 | DBG2("UST app ustctl register channel received"); | |
6854 | ||
b623cb6a | 6855 | ret = lttng_ust_ctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, |
d0b96690 DG |
6856 | &fields); |
6857 | if (ret < 0) { | |
be355079 JR |
6858 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6859 | DBG3("UST app recv channel failed. Application died: sock = %d", | |
6860 | sock); | |
6861 | } else if (ret == -EAGAIN) { | |
6862 | WARN("UST app recv channel failed. Communication time out: sock = %d", | |
6863 | sock); | |
d0b96690 | 6864 | } else { |
9324443a | 6865 | ERR("UST app recv channel failed with ret %d: sock = %d", |
be355079 | 6866 | ret, sock); |
d0b96690 DG |
6867 | } |
6868 | goto error; | |
6869 | } | |
6870 | ||
d5d629b5 DG |
6871 | /* |
6872 | * The fields ownership are transfered to this function call meaning | |
6873 | * that if needed it will be freed. After this, it's invalid to access | |
6874 | * fields or clean it up. | |
6875 | */ | |
8eede835 | 6876 | ret = reply_ust_register_channel(sock, cobjd, nr_fields, |
d0b96690 DG |
6877 | fields); |
6878 | if (ret < 0) { | |
6879 | goto error; | |
6880 | } | |
6881 | ||
6882 | break; | |
6883 | } | |
b623cb6a | 6884 | case LTTNG_UST_CTL_NOTIFY_CMD_ENUM: |
10b56aef MD |
6885 | { |
6886 | int sobjd; | |
fc4b93fa | 6887 | char name[LTTNG_UST_ABI_SYM_NAME_LEN]; |
10b56aef | 6888 | size_t nr_entries; |
b623cb6a | 6889 | struct lttng_ust_ctl_enum_entry *entries; |
10b56aef MD |
6890 | |
6891 | DBG2("UST app ustctl register enum received"); | |
6892 | ||
b623cb6a | 6893 | ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name, |
10b56aef MD |
6894 | &entries, &nr_entries); |
6895 | if (ret < 0) { | |
be355079 JR |
6896 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
6897 | DBG3("UST app recv enum failed. Application died: sock = %d", | |
6898 | sock); | |
6899 | } else if (ret == -EAGAIN) { | |
6900 | WARN("UST app recv enum failed. Communication time out: sock = %d", | |
6901 | sock); | |
10b56aef | 6902 | } else { |
be355079 JR |
6903 | ERR("UST app recv enum failed with ret %d: sock = %d", |
6904 | ret, sock); | |
10b56aef MD |
6905 | } |
6906 | goto error; | |
6907 | } | |
6908 | ||
6909 | /* Callee assumes ownership of entries */ | |
6910 | ret = add_enum_ust_registry(sock, sobjd, name, | |
6911 | entries, nr_entries); | |
6912 | if (ret < 0) { | |
6913 | goto error; | |
6914 | } | |
6915 | ||
6916 | break; | |
6917 | } | |
d0b96690 DG |
6918 | default: |
6919 | /* Should NEVER happen. */ | |
a0377dfe | 6920 | abort(); |
d0b96690 DG |
6921 | } |
6922 | ||
6923 | error: | |
6924 | return ret; | |
6925 | } | |
d88aee68 DG |
6926 | |
6927 | /* | |
6928 | * Once the notify socket hangs up, this is called. First, it tries to find the | |
6929 | * corresponding application. On failure, the call_rcu to close the socket is | |
6930 | * executed. If an application is found, it tries to delete it from the notify | |
6931 | * socket hash table. Whathever the result, it proceeds to the call_rcu. | |
6932 | * | |
6933 | * Note that an object needs to be allocated here so on ENOMEM failure, the | |
6934 | * call RCU is not done but the rest of the cleanup is. | |
6935 | */ | |
6936 | void ust_app_notify_sock_unregister(int sock) | |
6937 | { | |
6938 | int err_enomem = 0; | |
6939 | struct lttng_ht_iter iter; | |
6940 | struct ust_app *app; | |
6941 | struct ust_app_notify_sock_obj *obj; | |
6942 | ||
a0377dfe | 6943 | LTTNG_ASSERT(sock >= 0); |
d88aee68 DG |
6944 | |
6945 | rcu_read_lock(); | |
6946 | ||
64803277 | 6947 | obj = zmalloc<ust_app_notify_sock_obj>(); |
d88aee68 DG |
6948 | if (!obj) { |
6949 | /* | |
6950 | * An ENOMEM is kind of uncool. If this strikes we continue the | |
6951 | * procedure but the call_rcu will not be called. In this case, we | |
6952 | * accept the fd leak rather than possibly creating an unsynchronized | |
6953 | * state between threads. | |
6954 | * | |
6955 | * TODO: The notify object should be created once the notify socket is | |
6956 | * registered and stored independantely from the ust app object. The | |
6957 | * tricky part is to synchronize the teardown of the application and | |
6958 | * this notify object. Let's keep that in mind so we can avoid this | |
6959 | * kind of shenanigans with ENOMEM in the teardown path. | |
6960 | */ | |
6961 | err_enomem = 1; | |
6962 | } else { | |
6963 | obj->fd = sock; | |
6964 | } | |
6965 | ||
6966 | DBG("UST app notify socket unregister %d", sock); | |
6967 | ||
6968 | /* | |
6969 | * Lookup application by notify socket. If this fails, this means that the | |
6970 | * hash table delete has already been done by the application | |
6971 | * unregistration process so we can safely close the notify socket in a | |
6972 | * call RCU. | |
6973 | */ | |
6974 | app = find_app_by_notify_sock(sock); | |
6975 | if (!app) { | |
6976 | goto close_socket; | |
6977 | } | |
6978 | ||
6979 | iter.iter.node = &app->notify_sock_n.node; | |
6980 | ||
6981 | /* | |
6982 | * Whatever happens here either we fail or succeed, in both cases we have | |
6983 | * to close the socket after a grace period to continue to the call RCU | |
6984 | * here. If the deletion is successful, the application is not visible | |
6985 | * anymore by other threads and is it fails it means that it was already | |
6986 | * deleted from the hash table so either way we just have to close the | |
6987 | * socket. | |
6988 | */ | |
6989 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
6990 | ||
6991 | close_socket: | |
6992 | rcu_read_unlock(); | |
6993 | ||
6994 | /* | |
6995 | * Close socket after a grace period to avoid for the socket to be reused | |
6996 | * before the application object is freed creating potential race between | |
6997 | * threads trying to add unique in the global hash table. | |
6998 | */ | |
6999 | if (!err_enomem) { | |
7000 | call_rcu(&obj->head, close_notify_sock_rcu); | |
7001 | } | |
7002 | } | |
f45e313d DG |
7003 | |
7004 | /* | |
7005 | * Destroy a ust app data structure and free its memory. | |
7006 | */ | |
7007 | void ust_app_destroy(struct ust_app *app) | |
7008 | { | |
7009 | if (!app) { | |
7010 | return; | |
7011 | } | |
7012 | ||
7013 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
7014 | } | |
6dc3064a DG |
7015 | |
7016 | /* | |
7017 | * Take a snapshot for a given UST session. The snapshot is sent to the given | |
7018 | * output. | |
7019 | * | |
9a654598 | 7020 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 7021 | */ |
fb9a95c4 JG |
7022 | enum lttng_error_code ust_app_snapshot_record( |
7023 | const struct ltt_ust_session *usess, | |
f46376a1 | 7024 | const struct consumer_output *output, |
d07ceecd | 7025 | uint64_t nb_packets_per_stream) |
6dc3064a DG |
7026 | { |
7027 | int ret = 0; | |
9a654598 | 7028 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
7029 | struct lttng_ht_iter iter; |
7030 | struct ust_app *app; | |
affce97e | 7031 | char *trace_path = NULL; |
6dc3064a | 7032 | |
a0377dfe FD |
7033 | LTTNG_ASSERT(usess); |
7034 | LTTNG_ASSERT(output); | |
6dc3064a DG |
7035 | |
7036 | rcu_read_lock(); | |
7037 | ||
8c924c7b MD |
7038 | switch (usess->buffer_type) { |
7039 | case LTTNG_BUFFER_PER_UID: | |
7040 | { | |
7041 | struct buffer_reg_uid *reg; | |
6dc3064a | 7042 | |
8c924c7b | 7043 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
3273699d | 7044 | struct buffer_reg_channel *buf_reg_chan; |
8c924c7b | 7045 | struct consumer_socket *socket; |
3b967712 | 7046 | char pathname[PATH_MAX]; |
5da88b0f | 7047 | size_t consumer_path_offset = 0; |
6dc3064a | 7048 | |
aeeb48c6 | 7049 | if (!reg->registry->reg.ust->_metadata_key) { |
2b269489 JR |
7050 | /* Skip since no metadata is present */ |
7051 | continue; | |
7052 | } | |
7053 | ||
8c924c7b MD |
7054 | /* Get consumer socket to use to push the metadata.*/ |
7055 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7056 | usess->consumer); | |
7057 | if (!socket) { | |
9a654598 | 7058 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
7059 | goto error; |
7060 | } | |
6dc3064a | 7061 | |
8c924c7b MD |
7062 | memset(pathname, 0, sizeof(pathname)); |
7063 | ret = snprintf(pathname, sizeof(pathname), | |
bd666153 | 7064 | DEFAULT_UST_TRACE_UID_PATH, |
8c924c7b MD |
7065 | reg->uid, reg->bits_per_long); |
7066 | if (ret < 0) { | |
7067 | PERROR("snprintf snapshot path"); | |
9a654598 | 7068 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
7069 | goto error; |
7070 | } | |
affce97e JG |
7071 | /* Free path allowed on previous iteration. */ |
7072 | free(trace_path); | |
5da88b0f MD |
7073 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
7074 | &consumer_path_offset); | |
3b967712 MD |
7075 | if (!trace_path) { |
7076 | status = LTTNG_ERR_INVALID; | |
7077 | goto error; | |
7078 | } | |
f3db82be | 7079 | /* Add the UST default trace dir to path. */ |
8c924c7b | 7080 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
3273699d | 7081 | buf_reg_chan, node.node) { |
9a654598 | 7082 | status = consumer_snapshot_channel(socket, |
3273699d | 7083 | buf_reg_chan->consumer_key, |
f46376a1 | 7084 | output, 0, &trace_path[consumer_path_offset], |
d2956687 | 7085 | nb_packets_per_stream); |
9a654598 | 7086 | if (status != LTTNG_OK) { |
8c924c7b MD |
7087 | goto error; |
7088 | } | |
7089 | } | |
9a654598 | 7090 | status = consumer_snapshot_channel(socket, |
aeeb48c6 | 7091 | reg->registry->reg.ust->_metadata_key, output, 1, |
f46376a1 | 7092 | &trace_path[consumer_path_offset], 0); |
9a654598 | 7093 | if (status != LTTNG_OK) { |
8c924c7b MD |
7094 | goto error; |
7095 | } | |
af706bb7 | 7096 | } |
8c924c7b MD |
7097 | break; |
7098 | } | |
7099 | case LTTNG_BUFFER_PER_PID: | |
7100 | { | |
7101 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7102 | struct consumer_socket *socket; | |
7103 | struct lttng_ht_iter chan_iter; | |
7104 | struct ust_app_channel *ua_chan; | |
7105 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 7106 | ust_registry_session *registry; |
3b967712 | 7107 | char pathname[PATH_MAX]; |
5da88b0f | 7108 | size_t consumer_path_offset = 0; |
8c924c7b MD |
7109 | |
7110 | ua_sess = lookup_session_by_app(usess, app); | |
7111 | if (!ua_sess) { | |
7112 | /* Session not associated with this app. */ | |
7113 | continue; | |
7114 | } | |
af706bb7 | 7115 | |
8c924c7b MD |
7116 | /* Get the right consumer socket for the application. */ |
7117 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
348a81dc | 7118 | output); |
8c924c7b | 7119 | if (!socket) { |
9a654598 | 7120 | status = LTTNG_ERR_INVALID; |
5c786ded JD |
7121 | goto error; |
7122 | } | |
7123 | ||
8c924c7b MD |
7124 | /* Add the UST default trace dir to path. */ |
7125 | memset(pathname, 0, sizeof(pathname)); | |
bd666153 | 7126 | ret = snprintf(pathname, sizeof(pathname), "%s", |
8c924c7b | 7127 | ua_sess->path); |
6dc3064a | 7128 | if (ret < 0) { |
9a654598 | 7129 | status = LTTNG_ERR_INVALID; |
8c924c7b | 7130 | PERROR("snprintf snapshot path"); |
6dc3064a DG |
7131 | goto error; |
7132 | } | |
affce97e JG |
7133 | /* Free path allowed on previous iteration. */ |
7134 | free(trace_path); | |
5da88b0f MD |
7135 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
7136 | &consumer_path_offset); | |
3b967712 MD |
7137 | if (!trace_path) { |
7138 | status = LTTNG_ERR_INVALID; | |
7139 | goto error; | |
7140 | } | |
f3db82be | 7141 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
8c924c7b | 7142 | ua_chan, node.node) { |
9a654598 | 7143 | status = consumer_snapshot_channel(socket, |
470cc211 | 7144 | ua_chan->key, output, 0, |
f46376a1 | 7145 | &trace_path[consumer_path_offset], |
d2956687 | 7146 | nb_packets_per_stream); |
9a654598 JG |
7147 | switch (status) { |
7148 | case LTTNG_OK: | |
7149 | break; | |
7150 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
7151 | continue; | |
7152 | default: | |
8c924c7b MD |
7153 | goto error; |
7154 | } | |
7155 | } | |
7156 | ||
7157 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 7158 | if (!registry) { |
9bbfb88c MD |
7159 | DBG("Application session is being torn down. Skip application."); |
7160 | continue; | |
fad1ed2f | 7161 | } |
9a654598 | 7162 | status = consumer_snapshot_channel(socket, |
aeeb48c6 | 7163 | registry->_metadata_key, output, 1, |
f46376a1 | 7164 | &trace_path[consumer_path_offset], 0); |
9a654598 JG |
7165 | switch (status) { |
7166 | case LTTNG_OK: | |
7167 | break; | |
7168 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
7169 | continue; | |
7170 | default: | |
8c924c7b MD |
7171 | goto error; |
7172 | } | |
7173 | } | |
7174 | break; | |
7175 | } | |
7176 | default: | |
a0377dfe | 7177 | abort(); |
8c924c7b | 7178 | break; |
6dc3064a DG |
7179 | } |
7180 | ||
7181 | error: | |
affce97e | 7182 | free(trace_path); |
6dc3064a | 7183 | rcu_read_unlock(); |
9a654598 | 7184 | return status; |
6dc3064a | 7185 | } |
5c786ded JD |
7186 | |
7187 | /* | |
d07ceecd | 7188 | * Return the size taken by one more packet per stream. |
5c786ded | 7189 | */ |
fb9a95c4 JG |
7190 | uint64_t ust_app_get_size_one_more_packet_per_stream( |
7191 | const struct ltt_ust_session *usess, uint64_t cur_nr_packets) | |
5c786ded | 7192 | { |
d07ceecd | 7193 | uint64_t tot_size = 0; |
5c786ded JD |
7194 | struct ust_app *app; |
7195 | struct lttng_ht_iter iter; | |
7196 | ||
a0377dfe | 7197 | LTTNG_ASSERT(usess); |
5c786ded JD |
7198 | |
7199 | switch (usess->buffer_type) { | |
7200 | case LTTNG_BUFFER_PER_UID: | |
7201 | { | |
7202 | struct buffer_reg_uid *reg; | |
7203 | ||
7204 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7205 | struct buffer_reg_channel *buf_reg_chan; |
5c786ded | 7206 | |
b7064eaa | 7207 | rcu_read_lock(); |
5c786ded | 7208 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
3273699d FD |
7209 | buf_reg_chan, node.node) { |
7210 | if (cur_nr_packets >= buf_reg_chan->num_subbuf) { | |
d07ceecd MD |
7211 | /* |
7212 | * Don't take channel into account if we | |
7213 | * already grab all its packets. | |
7214 | */ | |
7215 | continue; | |
7216 | } | |
3273699d | 7217 | tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count; |
5c786ded | 7218 | } |
b7064eaa | 7219 | rcu_read_unlock(); |
5c786ded JD |
7220 | } |
7221 | break; | |
7222 | } | |
7223 | case LTTNG_BUFFER_PER_PID: | |
7224 | { | |
7225 | rcu_read_lock(); | |
7226 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7227 | struct ust_app_channel *ua_chan; | |
7228 | struct ust_app_session *ua_sess; | |
7229 | struct lttng_ht_iter chan_iter; | |
7230 | ||
7231 | ua_sess = lookup_session_by_app(usess, app); | |
7232 | if (!ua_sess) { | |
7233 | /* Session not associated with this app. */ | |
7234 | continue; | |
7235 | } | |
7236 | ||
7237 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7238 | ua_chan, node.node) { | |
d07ceecd MD |
7239 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
7240 | /* | |
7241 | * Don't take channel into account if we | |
7242 | * already grab all its packets. | |
7243 | */ | |
7244 | continue; | |
7245 | } | |
7246 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; | |
5c786ded JD |
7247 | } |
7248 | } | |
7249 | rcu_read_unlock(); | |
7250 | break; | |
7251 | } | |
7252 | default: | |
a0377dfe | 7253 | abort(); |
5c786ded JD |
7254 | break; |
7255 | } | |
7256 | ||
d07ceecd | 7257 | return tot_size; |
5c786ded | 7258 | } |
fb83fe64 JD |
7259 | |
7260 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, | |
7261 | struct cds_list_head *buffer_reg_uid_list, | |
7262 | struct consumer_output *consumer, uint64_t uchan_id, | |
7263 | int overwrite, uint64_t *discarded, uint64_t *lost) | |
7264 | { | |
7265 | int ret; | |
7266 | uint64_t consumer_chan_key; | |
7267 | ||
70dd8162 MD |
7268 | *discarded = 0; |
7269 | *lost = 0; | |
7270 | ||
fb83fe64 | 7271 | ret = buffer_reg_uid_consumer_channel_key( |
76604852 | 7272 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
fb83fe64 | 7273 | if (ret < 0) { |
70dd8162 MD |
7274 | /* Not found */ |
7275 | ret = 0; | |
fb83fe64 JD |
7276 | goto end; |
7277 | } | |
7278 | ||
7279 | if (overwrite) { | |
7280 | ret = consumer_get_lost_packets(ust_session_id, | |
7281 | consumer_chan_key, consumer, lost); | |
7282 | } else { | |
7283 | ret = consumer_get_discarded_events(ust_session_id, | |
7284 | consumer_chan_key, consumer, discarded); | |
7285 | } | |
7286 | ||
7287 | end: | |
7288 | return ret; | |
7289 | } | |
7290 | ||
7291 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, | |
7292 | struct ltt_ust_channel *uchan, | |
7293 | struct consumer_output *consumer, int overwrite, | |
7294 | uint64_t *discarded, uint64_t *lost) | |
7295 | { | |
7296 | int ret = 0; | |
7297 | struct lttng_ht_iter iter; | |
7298 | struct lttng_ht_node_str *ua_chan_node; | |
7299 | struct ust_app *app; | |
7300 | struct ust_app_session *ua_sess; | |
7301 | struct ust_app_channel *ua_chan; | |
7302 | ||
70dd8162 MD |
7303 | *discarded = 0; |
7304 | *lost = 0; | |
7305 | ||
fb83fe64 JD |
7306 | rcu_read_lock(); |
7307 | /* | |
70dd8162 MD |
7308 | * Iterate over every registered applications. Sum counters for |
7309 | * all applications containing requested session and channel. | |
fb83fe64 JD |
7310 | */ |
7311 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7312 | struct lttng_ht_iter uiter; | |
7313 | ||
7314 | ua_sess = lookup_session_by_app(usess, app); | |
7315 | if (ua_sess == NULL) { | |
7316 | continue; | |
7317 | } | |
7318 | ||
7319 | /* Get channel */ | |
ee022399 | 7320 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
fb83fe64 JD |
7321 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
7322 | /* If the session is found for the app, the channel must be there */ | |
a0377dfe | 7323 | LTTNG_ASSERT(ua_chan_node); |
fb83fe64 JD |
7324 | |
7325 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
7326 | ||
7327 | if (overwrite) { | |
70dd8162 MD |
7328 | uint64_t _lost; |
7329 | ||
fb83fe64 | 7330 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, |
70dd8162 MD |
7331 | consumer, &_lost); |
7332 | if (ret < 0) { | |
7333 | break; | |
7334 | } | |
7335 | (*lost) += _lost; | |
fb83fe64 | 7336 | } else { |
70dd8162 MD |
7337 | uint64_t _discarded; |
7338 | ||
fb83fe64 | 7339 | ret = consumer_get_discarded_events(usess->id, |
70dd8162 MD |
7340 | ua_chan->key, consumer, &_discarded); |
7341 | if (ret < 0) { | |
7342 | break; | |
7343 | } | |
7344 | (*discarded) += _discarded; | |
fb83fe64 | 7345 | } |
fb83fe64 JD |
7346 | } |
7347 | ||
fb83fe64 JD |
7348 | rcu_read_unlock(); |
7349 | return ret; | |
7350 | } | |
c2561365 JD |
7351 | |
7352 | static | |
7353 | int ust_app_regenerate_statedump(struct ltt_ust_session *usess, | |
7354 | struct ust_app *app) | |
7355 | { | |
7356 | int ret = 0; | |
7357 | struct ust_app_session *ua_sess; | |
7358 | ||
7359 | DBG("Regenerating the metadata for ust app pid %d", app->pid); | |
7360 | ||
7361 | rcu_read_lock(); | |
7362 | ||
7363 | ua_sess = lookup_session_by_app(usess, app); | |
7364 | if (ua_sess == NULL) { | |
7365 | /* The session is in teardown process. Ignore and continue. */ | |
7366 | goto end; | |
7367 | } | |
7368 | ||
7369 | pthread_mutex_lock(&ua_sess->lock); | |
7370 | ||
7371 | if (ua_sess->deleted) { | |
7372 | goto end_unlock; | |
7373 | } | |
7374 | ||
7375 | pthread_mutex_lock(&app->sock_lock); | |
b623cb6a | 7376 | ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle); |
c2561365 JD |
7377 | pthread_mutex_unlock(&app->sock_lock); |
7378 | ||
7379 | end_unlock: | |
7380 | pthread_mutex_unlock(&ua_sess->lock); | |
7381 | ||
7382 | end: | |
7383 | rcu_read_unlock(); | |
7384 | health_code_update(); | |
7385 | return ret; | |
7386 | } | |
7387 | ||
7388 | /* | |
7389 | * Regenerate the statedump for each app in the session. | |
7390 | */ | |
7391 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) | |
7392 | { | |
7393 | int ret = 0; | |
7394 | struct lttng_ht_iter iter; | |
7395 | struct ust_app *app; | |
7396 | ||
7397 | DBG("Regenerating the metadata for all UST apps"); | |
7398 | ||
7399 | rcu_read_lock(); | |
7400 | ||
7401 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7402 | if (!app->compatible) { | |
7403 | continue; | |
7404 | } | |
7405 | ||
7406 | ret = ust_app_regenerate_statedump(usess, app); | |
7407 | if (ret < 0) { | |
7408 | /* Continue to the next app even on error */ | |
7409 | continue; | |
7410 | } | |
7411 | } | |
7412 | ||
7413 | rcu_read_unlock(); | |
7414 | ||
7415 | return 0; | |
7416 | } | |
5c408ad8 JD |
7417 | |
7418 | /* | |
7419 | * Rotate all the channels of a session. | |
7420 | * | |
6f6d3b69 | 7421 | * Return LTTNG_OK on success or else an LTTng error code. |
5c408ad8 | 7422 | */ |
6f6d3b69 | 7423 | enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) |
5c408ad8 | 7424 | { |
6f6d3b69 MD |
7425 | int ret; |
7426 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
5c408ad8 JD |
7427 | struct lttng_ht_iter iter; |
7428 | struct ust_app *app; | |
7429 | struct ltt_ust_session *usess = session->ust_session; | |
5c408ad8 | 7430 | |
a0377dfe | 7431 | LTTNG_ASSERT(usess); |
5c408ad8 JD |
7432 | |
7433 | rcu_read_lock(); | |
7434 | ||
7435 | switch (usess->buffer_type) { | |
7436 | case LTTNG_BUFFER_PER_UID: | |
7437 | { | |
7438 | struct buffer_reg_uid *reg; | |
7439 | ||
7440 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7441 | struct buffer_reg_channel *buf_reg_chan; |
5c408ad8 JD |
7442 | struct consumer_socket *socket; |
7443 | ||
7444 | /* Get consumer socket to use to push the metadata.*/ | |
7445 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7446 | usess->consumer); | |
7447 | if (!socket) { | |
6f6d3b69 | 7448 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
7449 | goto error; |
7450 | } | |
7451 | ||
5c408ad8 JD |
7452 | /* Rotate the data channels. */ |
7453 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 7454 | buf_reg_chan, node.node) { |
5c408ad8 | 7455 | ret = consumer_rotate_channel(socket, |
3273699d | 7456 | buf_reg_chan->consumer_key, |
d2956687 JG |
7457 | usess->consumer, |
7458 | /* is_metadata_channel */ false); | |
5c408ad8 | 7459 | if (ret < 0) { |
6f6d3b69 | 7460 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
7461 | goto error; |
7462 | } | |
7463 | } | |
7464 | ||
db786d44 JR |
7465 | /* |
7466 | * The metadata channel might not be present. | |
7467 | * | |
7468 | * Consumer stream allocation can be done | |
7469 | * asynchronously and can fail on intermediary | |
7470 | * operations (i.e add context) and lead to data | |
7471 | * channels created with no metadata channel. | |
7472 | */ | |
aeeb48c6 | 7473 | if (!reg->registry->reg.ust->_metadata_key) { |
db786d44 JR |
7474 | /* Skip since no metadata is present. */ |
7475 | continue; | |
7476 | } | |
7477 | ||
5c408ad8 JD |
7478 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); |
7479 | ||
7480 | ret = consumer_rotate_channel(socket, | |
aeeb48c6 | 7481 | reg->registry->reg.ust->_metadata_key, |
d2956687 JG |
7482 | usess->consumer, |
7483 | /* is_metadata_channel */ true); | |
5c408ad8 | 7484 | if (ret < 0) { |
6f6d3b69 | 7485 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
7486 | goto error; |
7487 | } | |
5c408ad8 JD |
7488 | } |
7489 | break; | |
7490 | } | |
7491 | case LTTNG_BUFFER_PER_PID: | |
7492 | { | |
7493 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7494 | struct consumer_socket *socket; | |
7495 | struct lttng_ht_iter chan_iter; | |
7496 | struct ust_app_channel *ua_chan; | |
7497 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 7498 | ust_registry_session *registry; |
5c408ad8 JD |
7499 | |
7500 | ua_sess = lookup_session_by_app(usess, app); | |
7501 | if (!ua_sess) { | |
7502 | /* Session not associated with this app. */ | |
7503 | continue; | |
7504 | } | |
5c408ad8 JD |
7505 | |
7506 | /* Get the right consumer socket for the application. */ | |
7507 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
7508 | usess->consumer); | |
7509 | if (!socket) { | |
6f6d3b69 | 7510 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
7511 | goto error; |
7512 | } | |
7513 | ||
7514 | registry = get_session_registry(ua_sess); | |
7515 | if (!registry) { | |
6f6d3b69 MD |
7516 | DBG("Application session is being torn down. Skip application."); |
7517 | continue; | |
5c408ad8 JD |
7518 | } |
7519 | ||
5c408ad8 JD |
7520 | /* Rotate the data channels. */ |
7521 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7522 | ua_chan, node.node) { | |
470cc211 JG |
7523 | ret = consumer_rotate_channel(socket, |
7524 | ua_chan->key, | |
d2956687 JG |
7525 | ua_sess->consumer, |
7526 | /* is_metadata_channel */ false); | |
5c408ad8 | 7527 | if (ret < 0) { |
6f6d3b69 MD |
7528 | /* Per-PID buffer and application going away. */ |
7529 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7530 | continue; | |
7531 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7532 | goto error; |
7533 | } | |
7534 | } | |
7535 | ||
7536 | /* Rotate the metadata channel. */ | |
7537 | (void) push_metadata(registry, usess->consumer); | |
470cc211 | 7538 | ret = consumer_rotate_channel(socket, |
aeeb48c6 | 7539 | registry->_metadata_key, |
d2956687 JG |
7540 | ua_sess->consumer, |
7541 | /* is_metadata_channel */ true); | |
5c408ad8 | 7542 | if (ret < 0) { |
6f6d3b69 MD |
7543 | /* Per-PID buffer and application going away. */ |
7544 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
7545 | continue; | |
7546 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
7547 | goto error; |
7548 | } | |
5c408ad8 JD |
7549 | } |
7550 | break; | |
7551 | } | |
7552 | default: | |
a0377dfe | 7553 | abort(); |
5c408ad8 JD |
7554 | break; |
7555 | } | |
7556 | ||
6f6d3b69 | 7557 | cmd_ret = LTTNG_OK; |
5c408ad8 JD |
7558 | |
7559 | error: | |
7560 | rcu_read_unlock(); | |
6f6d3b69 | 7561 | return cmd_ret; |
5c408ad8 | 7562 | } |
d2956687 JG |
7563 | |
7564 | enum lttng_error_code ust_app_create_channel_subdirectories( | |
7565 | const struct ltt_ust_session *usess) | |
7566 | { | |
7567 | enum lttng_error_code ret = LTTNG_OK; | |
7568 | struct lttng_ht_iter iter; | |
7569 | enum lttng_trace_chunk_status chunk_status; | |
7570 | char *pathname_index; | |
7571 | int fmt_ret; | |
7572 | ||
a0377dfe | 7573 | LTTNG_ASSERT(usess->current_trace_chunk); |
d2956687 JG |
7574 | rcu_read_lock(); |
7575 | ||
7576 | switch (usess->buffer_type) { | |
7577 | case LTTNG_BUFFER_PER_UID: | |
7578 | { | |
7579 | struct buffer_reg_uid *reg; | |
7580 | ||
7581 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7582 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7583 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7584 | reg->uid, reg->bits_per_long); |
7585 | if (fmt_ret < 0) { | |
7586 | ERR("Failed to format channel index directory"); | |
7587 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7588 | goto error; | |
7589 | } | |
7590 | ||
7591 | /* | |
7592 | * Create the index subdirectory which will take care | |
7593 | * of implicitly creating the channel's path. | |
7594 | */ | |
7595 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7596 | usess->current_trace_chunk, | |
7597 | pathname_index); | |
7598 | free(pathname_index); | |
7599 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7600 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7601 | goto error; | |
7602 | } | |
7603 | } | |
7604 | break; | |
7605 | } | |
7606 | case LTTNG_BUFFER_PER_PID: | |
7607 | { | |
7608 | struct ust_app *app; | |
7609 | ||
495dece5 MD |
7610 | /* |
7611 | * Create the toplevel ust/ directory in case no apps are running. | |
7612 | */ | |
7613 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7614 | usess->current_trace_chunk, | |
7615 | DEFAULT_UST_TRACE_DIR); | |
7616 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7617 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7618 | goto error; | |
7619 | } | |
7620 | ||
d2956687 JG |
7621 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
7622 | pid_n.node) { | |
7623 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 7624 | ust_registry_session *registry; |
d2956687 JG |
7625 | |
7626 | ua_sess = lookup_session_by_app(usess, app); | |
7627 | if (!ua_sess) { | |
7628 | /* Session not associated with this app. */ | |
7629 | continue; | |
7630 | } | |
7631 | ||
7632 | registry = get_session_registry(ua_sess); | |
7633 | if (!registry) { | |
7634 | DBG("Application session is being torn down. Skip application."); | |
7635 | continue; | |
7636 | } | |
7637 | ||
7638 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 7639 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
d2956687 JG |
7640 | ua_sess->path); |
7641 | if (fmt_ret < 0) { | |
7642 | ERR("Failed to format channel index directory"); | |
7643 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7644 | goto error; | |
7645 | } | |
7646 | /* | |
7647 | * Create the index subdirectory which will take care | |
7648 | * of implicitly creating the channel's path. | |
7649 | */ | |
7650 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
7651 | usess->current_trace_chunk, | |
7652 | pathname_index); | |
7653 | free(pathname_index); | |
7654 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
7655 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
7656 | goto error; | |
7657 | } | |
7658 | } | |
7659 | break; | |
7660 | } | |
7661 | default: | |
7662 | abort(); | |
7663 | } | |
7664 | ||
7665 | ret = LTTNG_OK; | |
7666 | error: | |
7667 | rcu_read_unlock(); | |
7668 | return ret; | |
7669 | } | |
4a9b9759 MD |
7670 | |
7671 | /* | |
7672 | * Clear all the channels of a session. | |
7673 | * | |
7674 | * Return LTTNG_OK on success or else an LTTng error code. | |
7675 | */ | |
7676 | enum lttng_error_code ust_app_clear_session(struct ltt_session *session) | |
7677 | { | |
7678 | int ret; | |
7679 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
7680 | struct lttng_ht_iter iter; | |
7681 | struct ust_app *app; | |
7682 | struct ltt_ust_session *usess = session->ust_session; | |
7683 | ||
a0377dfe | 7684 | LTTNG_ASSERT(usess); |
4a9b9759 MD |
7685 | |
7686 | rcu_read_lock(); | |
7687 | ||
7688 | if (usess->active) { | |
7689 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); | |
7690 | cmd_ret = LTTNG_ERR_FATAL; | |
7691 | goto end; | |
7692 | } | |
7693 | ||
7694 | switch (usess->buffer_type) { | |
7695 | case LTTNG_BUFFER_PER_UID: | |
7696 | { | |
7697 | struct buffer_reg_uid *reg; | |
7698 | ||
7699 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7700 | struct buffer_reg_channel *buf_reg_chan; |
4a9b9759 MD |
7701 | struct consumer_socket *socket; |
7702 | ||
7703 | /* Get consumer socket to use to push the metadata.*/ | |
7704 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7705 | usess->consumer); | |
7706 | if (!socket) { | |
7707 | cmd_ret = LTTNG_ERR_INVALID; | |
7708 | goto error_socket; | |
7709 | } | |
7710 | ||
7711 | /* Clear the data channels. */ | |
7712 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
3273699d | 7713 | buf_reg_chan, node.node) { |
4a9b9759 | 7714 | ret = consumer_clear_channel(socket, |
3273699d | 7715 | buf_reg_chan->consumer_key); |
4a9b9759 MD |
7716 | if (ret < 0) { |
7717 | goto error; | |
7718 | } | |
7719 | } | |
7720 | ||
7721 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); | |
7722 | ||
7723 | /* | |
7724 | * Clear the metadata channel. | |
7725 | * Metadata channel is not cleared per se but we still need to | |
7726 | * perform a rotation operation on it behind the scene. | |
7727 | */ | |
7728 | ret = consumer_clear_channel(socket, | |
aeeb48c6 | 7729 | reg->registry->reg.ust->_metadata_key); |
4a9b9759 MD |
7730 | if (ret < 0) { |
7731 | goto error; | |
7732 | } | |
7733 | } | |
7734 | break; | |
7735 | } | |
7736 | case LTTNG_BUFFER_PER_PID: | |
7737 | { | |
7738 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7739 | struct consumer_socket *socket; | |
7740 | struct lttng_ht_iter chan_iter; | |
7741 | struct ust_app_channel *ua_chan; | |
7742 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 7743 | ust_registry_session *registry; |
4a9b9759 MD |
7744 | |
7745 | ua_sess = lookup_session_by_app(usess, app); | |
7746 | if (!ua_sess) { | |
7747 | /* Session not associated with this app. */ | |
7748 | continue; | |
7749 | } | |
7750 | ||
7751 | /* Get the right consumer socket for the application. */ | |
7752 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
7753 | usess->consumer); | |
7754 | if (!socket) { | |
7755 | cmd_ret = LTTNG_ERR_INVALID; | |
7756 | goto error_socket; | |
7757 | } | |
7758 | ||
7759 | registry = get_session_registry(ua_sess); | |
7760 | if (!registry) { | |
7761 | DBG("Application session is being torn down. Skip application."); | |
7762 | continue; | |
7763 | } | |
7764 | ||
7765 | /* Clear the data channels. */ | |
7766 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
7767 | ua_chan, node.node) { | |
7768 | ret = consumer_clear_channel(socket, ua_chan->key); | |
7769 | if (ret < 0) { | |
7770 | /* Per-PID buffer and application going away. */ | |
7771 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7772 | continue; | |
7773 | } | |
7774 | goto error; | |
7775 | } | |
7776 | } | |
7777 | ||
7778 | (void) push_metadata(registry, usess->consumer); | |
7779 | ||
7780 | /* | |
7781 | * Clear the metadata channel. | |
7782 | * Metadata channel is not cleared per se but we still need to | |
7783 | * perform rotation operation on it behind the scene. | |
7784 | */ | |
aeeb48c6 | 7785 | ret = consumer_clear_channel(socket, registry->_metadata_key); |
4a9b9759 MD |
7786 | if (ret < 0) { |
7787 | /* Per-PID buffer and application going away. */ | |
7788 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
7789 | continue; | |
7790 | } | |
7791 | goto error; | |
7792 | } | |
7793 | } | |
7794 | break; | |
7795 | } | |
7796 | default: | |
a0377dfe | 7797 | abort(); |
4a9b9759 MD |
7798 | break; |
7799 | } | |
7800 | ||
7801 | cmd_ret = LTTNG_OK; | |
7802 | goto end; | |
7803 | ||
7804 | error: | |
7805 | switch (-ret) { | |
7806 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
7807 | cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; | |
7808 | break; | |
7809 | default: | |
7810 | cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; | |
7811 | } | |
7812 | ||
7813 | error_socket: | |
7814 | end: | |
7815 | rcu_read_unlock(); | |
7816 | return cmd_ret; | |
7817 | } | |
04ed9e10 JG |
7818 | |
7819 | /* | |
7820 | * This function skips the metadata channel as the begin/end timestamps of a | |
7821 | * metadata packet are useless. | |
7822 | * | |
7823 | * Moreover, opening a packet after a "clear" will cause problems for live | |
7824 | * sessions as it will introduce padding that was not part of the first trace | |
7825 | * chunk. The relay daemon expects the content of the metadata stream of | |
7826 | * successive metadata trace chunks to be strict supersets of one another. | |
7827 | * | |
7828 | * For example, flushing a packet at the beginning of the metadata stream of | |
7829 | * a trace chunk resulting from a "clear" session command will cause the | |
7830 | * size of the metadata stream of the new trace chunk to not match the size of | |
7831 | * the metadata stream of the original chunk. This will confuse the relay | |
7832 | * daemon as the same "offset" in a metadata stream will no longer point | |
7833 | * to the same content. | |
7834 | */ | |
7835 | enum lttng_error_code ust_app_open_packets(struct ltt_session *session) | |
7836 | { | |
7837 | enum lttng_error_code ret = LTTNG_OK; | |
7838 | struct lttng_ht_iter iter; | |
7839 | struct ltt_ust_session *usess = session->ust_session; | |
7840 | ||
a0377dfe | 7841 | LTTNG_ASSERT(usess); |
04ed9e10 JG |
7842 | |
7843 | rcu_read_lock(); | |
7844 | ||
7845 | switch (usess->buffer_type) { | |
7846 | case LTTNG_BUFFER_PER_UID: | |
7847 | { | |
7848 | struct buffer_reg_uid *reg; | |
7849 | ||
7850 | cds_list_for_each_entry ( | |
7851 | reg, &usess->buffer_reg_uid_list, lnode) { | |
3273699d | 7852 | struct buffer_reg_channel *buf_reg_chan; |
04ed9e10 JG |
7853 | struct consumer_socket *socket; |
7854 | ||
7855 | socket = consumer_find_socket_by_bitness( | |
7856 | reg->bits_per_long, usess->consumer); | |
7857 | if (!socket) { | |
7858 | ret = LTTNG_ERR_FATAL; | |
7859 | goto error; | |
7860 | } | |
7861 | ||
7862 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
3273699d | 7863 | &iter.iter, buf_reg_chan, node.node) { |
04ed9e10 JG |
7864 | const int open_ret = |
7865 | consumer_open_channel_packets( | |
7866 | socket, | |
3273699d | 7867 | buf_reg_chan->consumer_key); |
04ed9e10 JG |
7868 | |
7869 | if (open_ret < 0) { | |
7870 | ret = LTTNG_ERR_UNK; | |
7871 | goto error; | |
7872 | } | |
7873 | } | |
7874 | } | |
7875 | break; | |
7876 | } | |
7877 | case LTTNG_BUFFER_PER_PID: | |
7878 | { | |
7879 | struct ust_app *app; | |
7880 | ||
7881 | cds_lfht_for_each_entry ( | |
7882 | ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7883 | struct consumer_socket *socket; | |
7884 | struct lttng_ht_iter chan_iter; | |
7885 | struct ust_app_channel *ua_chan; | |
7886 | struct ust_app_session *ua_sess; | |
aeeb48c6 | 7887 | ust_registry_session *registry; |
04ed9e10 JG |
7888 | |
7889 | ua_sess = lookup_session_by_app(usess, app); | |
7890 | if (!ua_sess) { | |
7891 | /* Session not associated with this app. */ | |
7892 | continue; | |
7893 | } | |
7894 | ||
7895 | /* Get the right consumer socket for the application. */ | |
7896 | socket = consumer_find_socket_by_bitness( | |
7897 | app->bits_per_long, usess->consumer); | |
7898 | if (!socket) { | |
7899 | ret = LTTNG_ERR_FATAL; | |
7900 | goto error; | |
7901 | } | |
7902 | ||
7903 | registry = get_session_registry(ua_sess); | |
7904 | if (!registry) { | |
7905 | DBG("Application session is being torn down. Skip application."); | |
7906 | continue; | |
7907 | } | |
7908 | ||
7909 | cds_lfht_for_each_entry(ua_sess->channels->ht, | |
7910 | &chan_iter.iter, ua_chan, node.node) { | |
7911 | const int open_ret = | |
7912 | consumer_open_channel_packets( | |
7913 | socket, | |
7914 | ua_chan->key); | |
7915 | ||
7916 | if (open_ret < 0) { | |
7917 | /* | |
7918 | * Per-PID buffer and application going | |
7919 | * away. | |
7920 | */ | |
97a171e1 | 7921 | if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
04ed9e10 JG |
7922 | continue; |
7923 | } | |
7924 | ||
7925 | ret = LTTNG_ERR_UNK; | |
7926 | goto error; | |
7927 | } | |
7928 | } | |
7929 | } | |
7930 | break; | |
7931 | } | |
7932 | default: | |
7933 | abort(); | |
7934 | break; | |
7935 | } | |
7936 | ||
7937 | error: | |
7938 | rcu_read_unlock(); | |
7939 | return ret; | |
7940 | } |