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