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