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 |
ebdb334b JR |
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> |
ebdb334b | 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 | 27 | #include <common/hashtable/utils.h> |
ebdb334b JR |
28 | #include <common/shm.h> |
29 | #include <lttng/lttng-error.h> | |
993578ff JR |
30 | #include <lttng/event-rule/event-rule.h> |
31 | #include <lttng/event-rule/event-rule-internal.h> | |
32 | #include <lttng/event-rule/tracepoint.h> | |
33 | #include <lttng/condition/condition.h> | |
ebdb334b JR |
34 | #include <lttng/condition/on-event-internal.h> |
35 | #include <lttng/condition/on-event.h> | |
36 | #include <lttng/map/map.h> | |
37 | #include <lttng/map/map-internal.h> | |
38 | #include <lttng/map/map-query-internal.h> | |
39 | #include <lttng/map-key.h> | |
40 | #include <lttng/map-key-internal.h> | |
f83be61d | 41 | #include <lttng/trigger/trigger-internal.h> |
86acf0da | 42 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 43 | |
7972aab2 | 44 | #include "buffer-registry.h" |
ebdb334b | 45 | #include "condition-internal.h" |
86acf0da | 46 | #include "fd-limit.h" |
8782cc74 | 47 | #include "health-sessiond.h" |
56fff090 | 48 | #include "ust-app.h" |
48842b30 | 49 | #include "ust-consumer.h" |
75018ab6 JG |
50 | #include "lttng-ust-ctl.h" |
51 | #include "lttng-ust-error.h" | |
0b2dc8df | 52 | #include "utils.h" |
fb83fe64 | 53 | #include "session.h" |
e9404c27 JG |
54 | #include "lttng-sessiond.h" |
55 | #include "notification-thread-commands.h" | |
5c408ad8 | 56 | #include "rotate.h" |
44760c20 | 57 | #include "event.h" |
ebdb334b JR |
58 | #include "event-notifier-error-accounting.h" |
59 | #include "map.h" | |
60 | ||
d80a6244 | 61 | |
44cdb3a2 MJ |
62 | struct lttng_ht *ust_app_ht; |
63 | struct lttng_ht *ust_app_ht_by_sock; | |
64 | struct lttng_ht *ust_app_ht_by_notify_sock; | |
65 | ||
c4b88406 MD |
66 | static |
67 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); | |
68 | ||
d9bf3ca4 MD |
69 | /* Next available channel key. Access under next_channel_key_lock. */ |
70 | static uint64_t _next_channel_key; | |
71 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
72 | ||
ebdb334b JR |
73 | /* Next available map key. Access under next_map_key_lock. */ |
74 | static uint64_t _next_map_key; | |
75 | static pthread_mutex_t next_map_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
76 | ||
d9bf3ca4 MD |
77 | /* Next available session ID. Access under next_session_id_lock. */ |
78 | static uint64_t _next_session_id; | |
79 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
ffe60014 DG |
80 | |
81 | /* | |
d9bf3ca4 | 82 | * Return the incremented value of next_channel_key. |
ffe60014 | 83 | */ |
d9bf3ca4 | 84 | static uint64_t get_next_channel_key(void) |
ffe60014 | 85 | { |
d9bf3ca4 MD |
86 | uint64_t ret; |
87 | ||
88 | pthread_mutex_lock(&next_channel_key_lock); | |
89 | ret = ++_next_channel_key; | |
90 | pthread_mutex_unlock(&next_channel_key_lock); | |
91 | return ret; | |
ffe60014 DG |
92 | } |
93 | ||
ebdb334b JR |
94 | /* |
95 | * Return the incremented value of next_map_key. | |
96 | */ | |
97 | static uint64_t get_next_map_key(void) | |
98 | { | |
99 | uint64_t ret; | |
100 | ||
101 | pthread_mutex_lock(&next_map_key_lock); | |
102 | ret = ++_next_map_key; | |
103 | pthread_mutex_unlock(&next_map_key_lock); | |
104 | return ret; | |
105 | } | |
106 | ||
ffe60014 | 107 | /* |
7972aab2 | 108 | * Return the atomically incremented value of next_session_id. |
ffe60014 | 109 | */ |
d9bf3ca4 | 110 | static uint64_t get_next_session_id(void) |
ffe60014 | 111 | { |
d9bf3ca4 MD |
112 | uint64_t ret; |
113 | ||
114 | pthread_mutex_lock(&next_session_id_lock); | |
115 | ret = ++_next_session_id; | |
116 | pthread_mutex_unlock(&next_session_id_lock); | |
117 | return ret; | |
ffe60014 DG |
118 | } |
119 | ||
d65d2de8 DG |
120 | static void copy_channel_attr_to_ustctl( |
121 | struct ustctl_consumer_channel_attr *attr, | |
122 | struct lttng_ust_channel_attr *uattr) | |
123 | { | |
124 | /* Copy event attributes since the layout is different. */ | |
125 | attr->subbuf_size = uattr->subbuf_size; | |
126 | attr->num_subbuf = uattr->num_subbuf; | |
127 | attr->overwrite = uattr->overwrite; | |
128 | attr->switch_timer_interval = uattr->switch_timer_interval; | |
129 | attr->read_timer_interval = uattr->read_timer_interval; | |
130 | attr->output = uattr->output; | |
491d1539 | 131 | attr->blocking_timeout = uattr->u.s.blocking_timeout; |
d65d2de8 DG |
132 | } |
133 | ||
025faf73 DG |
134 | /* |
135 | * Match function for the hash table lookup. | |
136 | * | |
137 | * It matches an ust app event based on three attributes which are the event | |
138 | * name, the filter bytecode and the loglevel. | |
139 | */ | |
18eace3b DG |
140 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
141 | { | |
142 | struct ust_app_event *event; | |
143 | const struct ust_app_ht_key *key; | |
2106efa0 | 144 | int ev_loglevel_value; |
18eace3b DG |
145 | |
146 | assert(node); | |
147 | assert(_key); | |
148 | ||
149 | event = caa_container_of(node, struct ust_app_event, node.node); | |
150 | key = _key; | |
2106efa0 | 151 | ev_loglevel_value = event->attr.loglevel; |
18eace3b | 152 | |
ebdb334b JR |
153 | /* |
154 | * Match the 5 elements of the key: | |
155 | * tracer token, name, filter, loglevel, exclusions | |
156 | */ | |
157 | ||
158 | if (event->attr.token != key->tracer_token) { | |
159 | goto no_match; | |
160 | } | |
18eace3b DG |
161 | |
162 | /* Event name */ | |
163 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
164 | goto no_match; | |
165 | } | |
166 | ||
167 | /* Event loglevel. */ | |
2106efa0 | 168 | if (ev_loglevel_value != key->loglevel_type) { |
025faf73 | 169 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
2106efa0 PP |
170 | && key->loglevel_type == 0 && |
171 | ev_loglevel_value == -1) { | |
025faf73 DG |
172 | /* |
173 | * Match is accepted. This is because on event creation, the | |
174 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and | |
175 | * -1 are accepted for this loglevel type since 0 is the one set by | |
176 | * the API when receiving an enable event. | |
177 | */ | |
178 | } else { | |
179 | goto no_match; | |
180 | } | |
18eace3b DG |
181 | } |
182 | ||
183 | /* One of the filters is NULL, fail. */ | |
184 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
185 | goto no_match; | |
186 | } | |
187 | ||
025faf73 DG |
188 | if (key->filter && event->filter) { |
189 | /* Both filters exists, check length followed by the bytecode. */ | |
190 | if (event->filter->len != key->filter->len || | |
191 | memcmp(event->filter->data, key->filter->data, | |
192 | event->filter->len) != 0) { | |
193 | goto no_match; | |
194 | } | |
18eace3b DG |
195 | } |
196 | ||
1af53eb5 JI |
197 | /* One of the exclusions is NULL, fail. */ |
198 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
199 | goto no_match; | |
200 | } | |
201 | ||
202 | if (key->exclusion && event->exclusion) { | |
203 | /* Both exclusions exists, check count followed by the names. */ | |
204 | if (event->exclusion->count != key->exclusion->count || | |
205 | memcmp(event->exclusion->names, key->exclusion->names, | |
206 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { | |
207 | goto no_match; | |
208 | } | |
209 | } | |
210 | ||
211 | ||
025faf73 | 212 | /* Match. */ |
18eace3b DG |
213 | return 1; |
214 | ||
215 | no_match: | |
216 | return 0; | |
18eace3b DG |
217 | } |
218 | ||
025faf73 DG |
219 | /* |
220 | * Unique add of an ust app event in the given ht. This uses the custom | |
221 | * ht_match_ust_app_event match function and the event name as hash. | |
222 | */ | |
ebdb334b | 223 | static void add_unique_ust_app_event(struct lttng_ht *events_ht, |
18eace3b DG |
224 | struct ust_app_event *event) |
225 | { | |
226 | struct cds_lfht_node *node_ptr; | |
227 | struct ust_app_ht_key key; | |
228 | ||
ebdb334b | 229 | assert(events_ht); |
18eace3b DG |
230 | assert(event); |
231 | ||
232 | key.name = event->attr.name; | |
233 | key.filter = event->filter; | |
2106efa0 | 234 | key.loglevel_type = event->attr.loglevel; |
91c89f23 | 235 | key.exclusion = event->exclusion; |
ebdb334b | 236 | key.tracer_token = event->attr.token; |
18eace3b | 237 | |
ebdb334b JR |
238 | node_ptr = cds_lfht_add_unique(events_ht->ht, |
239 | events_ht->hash_fct(event->node.key, lttng_ht_seed), | |
18eace3b DG |
240 | ht_match_ust_app_event, &key, &event->node.node); |
241 | assert(node_ptr == &event->node.node); | |
242 | } | |
243 | ||
d88aee68 DG |
244 | /* |
245 | * Close the notify socket from the given RCU head object. This MUST be called | |
246 | * through a call_rcu(). | |
247 | */ | |
248 | static void close_notify_sock_rcu(struct rcu_head *head) | |
249 | { | |
250 | int ret; | |
251 | struct ust_app_notify_sock_obj *obj = | |
252 | caa_container_of(head, struct ust_app_notify_sock_obj, head); | |
253 | ||
254 | /* Must have a valid fd here. */ | |
255 | assert(obj->fd >= 0); | |
256 | ||
257 | ret = close(obj->fd); | |
258 | if (ret) { | |
259 | ERR("close notify sock %d RCU", obj->fd); | |
260 | } | |
261 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
262 | ||
263 | free(obj); | |
264 | } | |
265 | ||
7972aab2 DG |
266 | /* |
267 | * Return the session registry according to the buffer type of the given | |
268 | * session. | |
269 | * | |
270 | * A registry per UID object MUST exists before calling this function or else | |
271 | * it assert() if not found. RCU read side lock must be acquired. | |
272 | */ | |
273 | static struct ust_registry_session *get_session_registry( | |
274 | struct ust_app_session *ua_sess) | |
275 | { | |
276 | struct ust_registry_session *registry = NULL; | |
277 | ||
278 | assert(ua_sess); | |
279 | ||
280 | switch (ua_sess->buffer_type) { | |
281 | case LTTNG_BUFFER_PER_PID: | |
282 | { | |
283 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
284 | if (!reg_pid) { | |
285 | goto error; | |
286 | } | |
287 | registry = reg_pid->registry->reg.ust; | |
288 | break; | |
289 | } | |
290 | case LTTNG_BUFFER_PER_UID: | |
291 | { | |
292 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( | |
470cc211 | 293 | ua_sess->tracing_id, ua_sess->bits_per_long, |
ff588497 | 294 | lttng_credentials_get_uid(&ua_sess->real_credentials)); |
7972aab2 DG |
295 | if (!reg_uid) { |
296 | goto error; | |
297 | } | |
298 | registry = reg_uid->registry->reg.ust; | |
299 | break; | |
300 | } | |
301 | default: | |
302 | assert(0); | |
303 | }; | |
304 | ||
305 | error: | |
306 | return registry; | |
307 | } | |
308 | ||
55cc08a6 DG |
309 | /* |
310 | * Delete ust context safely. RCU read lock must be held before calling | |
311 | * this function. | |
312 | */ | |
313 | static | |
fb45065e MD |
314 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, |
315 | struct ust_app *app) | |
55cc08a6 | 316 | { |
ffe60014 DG |
317 | int ret; |
318 | ||
319 | assert(ua_ctx); | |
320 | ||
55cc08a6 | 321 | if (ua_ctx->obj) { |
fb45065e | 322 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 323 | ret = ustctl_release_object(sock, ua_ctx->obj); |
fb45065e | 324 | pthread_mutex_unlock(&app->sock_lock); |
d0b96690 DG |
325 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
326 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", | |
327 | sock, ua_ctx->obj->handle, ret); | |
ffe60014 | 328 | } |
55cc08a6 DG |
329 | free(ua_ctx->obj); |
330 | } | |
331 | free(ua_ctx); | |
332 | } | |
333 | ||
d80a6244 DG |
334 | /* |
335 | * Delete ust app event safely. RCU read lock must be held before calling | |
336 | * this function. | |
337 | */ | |
8b366481 | 338 | static |
fb45065e MD |
339 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event, |
340 | struct ust_app *app) | |
d80a6244 | 341 | { |
ffe60014 DG |
342 | int ret; |
343 | ||
344 | assert(ua_event); | |
345 | ||
53a80697 | 346 | free(ua_event->filter); |
951f0b71 JI |
347 | if (ua_event->exclusion != NULL) |
348 | free(ua_event->exclusion); | |
edb67388 | 349 | if (ua_event->obj != NULL) { |
fb45065e | 350 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 351 | ret = ustctl_release_object(sock, ua_event->obj); |
fb45065e | 352 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
353 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
354 | ERR("UST app sock %d release event obj failed with ret %d", | |
355 | sock, ret); | |
356 | } | |
edb67388 DG |
357 | free(ua_event->obj); |
358 | } | |
d80a6244 DG |
359 | free(ua_event); |
360 | } | |
361 | ||
993578ff JR |
362 | /* |
363 | * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called | |
364 | * through a call_rcu(). | |
365 | */ | |
366 | static | |
367 | void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head) | |
368 | { | |
369 | struct ust_app_event_notifier_rule *obj = caa_container_of( | |
370 | head, struct ust_app_event_notifier_rule, rcu_head); | |
371 | ||
372 | free(obj); | |
373 | } | |
374 | ||
375 | /* | |
376 | * Delete ust app event notifier rule safely. | |
377 | */ | |
378 | static void delete_ust_app_event_notifier_rule(int sock, | |
379 | struct ust_app_event_notifier_rule *ua_event_notifier_rule, | |
380 | struct ust_app *app) | |
381 | { | |
382 | int ret; | |
383 | ||
384 | assert(ua_event_notifier_rule); | |
385 | ||
386 | if (ua_event_notifier_rule->exclusion != NULL) { | |
387 | free(ua_event_notifier_rule->exclusion); | |
388 | } | |
389 | ||
390 | if (ua_event_notifier_rule->obj != NULL) { | |
391 | pthread_mutex_lock(&app->sock_lock); | |
392 | ret = ustctl_release_object(sock, ua_event_notifier_rule->obj); | |
393 | pthread_mutex_unlock(&app->sock_lock); | |
394 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
395 | ERR("Failed to release event notifier object: app = '%s' (ppid %d), ret = %d", | |
396 | app->name, (int) app->ppid, ret); | |
397 | } | |
398 | ||
399 | free(ua_event_notifier_rule->obj); | |
400 | } | |
401 | ||
267d66aa | 402 | lttng_trigger_put(ua_event_notifier_rule->trigger); |
993578ff JR |
403 | call_rcu(&ua_event_notifier_rule->rcu_head, |
404 | free_ust_app_event_notifier_rule_rcu); | |
405 | } | |
406 | ||
d80a6244 | 407 | /* |
7972aab2 DG |
408 | * Release ust data object of the given stream. |
409 | * | |
410 | * Return 0 on success or else a negative value. | |
d80a6244 | 411 | */ |
fb45065e MD |
412 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, |
413 | struct ust_app *app) | |
d80a6244 | 414 | { |
7972aab2 | 415 | int ret = 0; |
ffe60014 DG |
416 | |
417 | assert(stream); | |
418 | ||
8b366481 | 419 | if (stream->obj) { |
fb45065e | 420 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 421 | ret = ustctl_release_object(sock, stream->obj); |
fb45065e | 422 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
423 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
424 | ERR("UST app sock %d release stream obj failed with ret %d", | |
425 | sock, ret); | |
426 | } | |
4063050c | 427 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
428 | free(stream->obj); |
429 | } | |
7972aab2 DG |
430 | |
431 | return ret; | |
432 | } | |
433 | ||
ebdb334b JR |
434 | /* |
435 | * Release ust data object of the given map_counter. | |
436 | * | |
437 | * Return 0 on success or else a negative value. | |
438 | */ | |
439 | static int release_ust_app_map_counter(int sock, struct ust_app_map_counter *map_counter, | |
440 | struct ust_app *app) | |
441 | { | |
442 | int ret = 0; | |
443 | ||
444 | assert(map_counter); | |
445 | ||
446 | if (map_counter->obj) { | |
447 | pthread_mutex_lock(&app->sock_lock); | |
448 | ret = ustctl_release_object(sock, map_counter->obj); | |
449 | pthread_mutex_unlock(&app->sock_lock); | |
450 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
451 | ERR("UST app sock %d release map_counter obj failed with ret %d", | |
452 | sock, ret); | |
453 | } | |
454 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
455 | free(map_counter->obj); | |
456 | } | |
457 | ||
458 | return ret; | |
459 | } | |
460 | ||
7972aab2 DG |
461 | /* |
462 | * Delete ust app stream safely. RCU read lock must be held before calling | |
463 | * this function. | |
464 | */ | |
465 | static | |
fb45065e MD |
466 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream, |
467 | struct ust_app *app) | |
7972aab2 DG |
468 | { |
469 | assert(stream); | |
470 | ||
fb45065e | 471 | (void) release_ust_app_stream(sock, stream, app); |
84cd17c6 | 472 | free(stream); |
d80a6244 DG |
473 | } |
474 | ||
ebdb334b JR |
475 | /* |
476 | * Delete ust app map_counter safely. RCU read lock must be held before calling | |
477 | * this function. | |
478 | */ | |
479 | static | |
480 | void delete_ust_app_map_counter(int sock, struct ust_app_map_counter *map_counter, | |
481 | struct ust_app *app) | |
482 | { | |
483 | assert(map_counter); | |
484 | ||
485 | (void) release_ust_app_map_counter(sock, map_counter, app); | |
486 | free(map_counter); | |
487 | } | |
488 | ||
36b588ed MD |
489 | /* |
490 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
491 | * section and outside of call_rcu thread, so we postpone its execution |
492 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
493 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
494 | */ |
495 | static | |
496 | void delete_ust_app_channel_rcu(struct rcu_head *head) | |
497 | { | |
498 | struct ust_app_channel *ua_chan = | |
499 | caa_container_of(head, struct ust_app_channel, rcu_head); | |
500 | ||
0b2dc8df MD |
501 | ht_cleanup_push(ua_chan->ctx); |
502 | ht_cleanup_push(ua_chan->events); | |
36b588ed MD |
503 | free(ua_chan); |
504 | } | |
505 | ||
ebdb334b JR |
506 | /* |
507 | * We need to execute ht_destroy outside of RCU read-side critical | |
508 | * section and outside of call_rcu thread, so we postpone its execution | |
509 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
510 | * the many callers of delete_ust_app_session(). | |
511 | */ | |
512 | static | |
513 | void delete_ust_app_map_rcu(struct rcu_head *head) | |
514 | { | |
515 | struct ust_app_map *ua_map = | |
516 | caa_container_of(head, struct ust_app_map, rcu_head); | |
517 | ||
518 | ht_cleanup_push(ua_map->events); | |
519 | free(ua_map); | |
520 | } | |
521 | ||
fb83fe64 JD |
522 | /* |
523 | * Extract the lost packet or discarded events counter when the channel is | |
524 | * being deleted and store the value in the parent channel so we can | |
525 | * access it from lttng list and at stop/destroy. | |
82cac6d2 JG |
526 | * |
527 | * The session list lock must be held by the caller. | |
fb83fe64 JD |
528 | */ |
529 | static | |
530 | void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan) | |
531 | { | |
532 | uint64_t discarded = 0, lost = 0; | |
533 | struct ltt_session *session; | |
534 | struct ltt_ust_channel *uchan; | |
535 | ||
536 | if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) { | |
537 | return; | |
538 | } | |
539 | ||
540 | rcu_read_lock(); | |
541 | session = session_find_by_id(ua_chan->session->tracing_id); | |
d68ec974 JG |
542 | if (!session || !session->ust_session) { |
543 | /* | |
544 | * Not finding the session is not an error because there are | |
545 | * multiple ways the channels can be torn down. | |
546 | * | |
547 | * 1) The session daemon can initiate the destruction of the | |
548 | * ust app session after receiving a destroy command or | |
549 | * during its shutdown/teardown. | |
550 | * 2) The application, since we are in per-pid tracing, is | |
551 | * unregistering and tearing down its ust app session. | |
552 | * | |
553 | * Both paths are protected by the session list lock which | |
554 | * ensures that the accounting of lost packets and discarded | |
555 | * events is done exactly once. The session is then unpublished | |
556 | * from the session list, resulting in this condition. | |
557 | */ | |
fb83fe64 JD |
558 | goto end; |
559 | } | |
560 | ||
561 | if (ua_chan->attr.overwrite) { | |
562 | consumer_get_lost_packets(ua_chan->session->tracing_id, | |
563 | ua_chan->key, session->ust_session->consumer, | |
564 | &lost); | |
565 | } else { | |
566 | consumer_get_discarded_events(ua_chan->session->tracing_id, | |
567 | ua_chan->key, session->ust_session->consumer, | |
568 | &discarded); | |
569 | } | |
570 | uchan = trace_ust_find_channel_by_name( | |
571 | session->ust_session->domain_global.channels, | |
572 | ua_chan->name); | |
573 | if (!uchan) { | |
574 | ERR("Missing UST channel to store discarded counters"); | |
575 | goto end; | |
576 | } | |
577 | ||
578 | uchan->per_pid_closed_app_discarded += discarded; | |
579 | uchan->per_pid_closed_app_lost += lost; | |
580 | ||
581 | end: | |
582 | rcu_read_unlock(); | |
e32d7f27 JG |
583 | if (session) { |
584 | session_put(session); | |
585 | } | |
fb83fe64 JD |
586 | } |
587 | ||
d80a6244 DG |
588 | /* |
589 | * Delete ust app channel safely. RCU read lock must be held before calling | |
590 | * this function. | |
82cac6d2 JG |
591 | * |
592 | * The session list lock must be held by the caller. | |
d80a6244 | 593 | */ |
8b366481 | 594 | static |
d0b96690 DG |
595 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
596 | struct ust_app *app) | |
d80a6244 DG |
597 | { |
598 | int ret; | |
bec39940 | 599 | struct lttng_ht_iter iter; |
d80a6244 | 600 | struct ust_app_event *ua_event; |
55cc08a6 | 601 | struct ust_app_ctx *ua_ctx; |
030a66fa | 602 | struct ust_app_stream *stream, *stmp; |
7972aab2 | 603 | struct ust_registry_session *registry; |
d80a6244 | 604 | |
ffe60014 DG |
605 | assert(ua_chan); |
606 | ||
607 | DBG3("UST app deleting channel %s", ua_chan->name); | |
608 | ||
55cc08a6 | 609 | /* Wipe stream */ |
d80a6244 | 610 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
84cd17c6 | 611 | cds_list_del(&stream->list); |
fb45065e | 612 | delete_ust_app_stream(sock, stream, app); |
d80a6244 DG |
613 | } |
614 | ||
55cc08a6 | 615 | /* Wipe context */ |
bec39940 | 616 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
31746f93 | 617 | cds_list_del(&ua_ctx->list); |
bec39940 | 618 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
55cc08a6 | 619 | assert(!ret); |
fb45065e | 620 | delete_ust_app_ctx(sock, ua_ctx, app); |
55cc08a6 | 621 | } |
d80a6244 | 622 | |
55cc08a6 | 623 | /* Wipe events */ |
bec39940 DG |
624 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
625 | node.node) { | |
626 | ret = lttng_ht_del(ua_chan->events, &iter); | |
525b0740 | 627 | assert(!ret); |
fb45065e | 628 | delete_ust_app_event(sock, ua_event, app); |
d80a6244 | 629 | } |
edb67388 | 630 | |
c8335706 MD |
631 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
632 | /* Wipe and free registry from session registry. */ | |
633 | registry = get_session_registry(ua_chan->session); | |
634 | if (registry) { | |
e9404c27 | 635 | ust_registry_channel_del_free(registry, ua_chan->key, |
e38d96f9 MD |
636 | sock >= 0); |
637 | } | |
45798a31 JG |
638 | /* |
639 | * A negative socket can be used by the caller when | |
640 | * cleaning-up a ua_chan in an error path. Skip the | |
641 | * accounting in this case. | |
642 | */ | |
e38d96f9 MD |
643 | if (sock >= 0) { |
644 | save_per_pid_lost_discarded_counters(ua_chan); | |
c8335706 | 645 | } |
7972aab2 | 646 | } |
d0b96690 | 647 | |
edb67388 | 648 | if (ua_chan->obj != NULL) { |
d0b96690 DG |
649 | /* Remove channel from application UST object descriptor. */ |
650 | iter.iter.node = &ua_chan->ust_objd_node.node; | |
ebdb334b | 651 | ret = lttng_ht_del(app->ust_chan_objd, &iter); |
c6e62271 | 652 | assert(!ret); |
fb45065e | 653 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 654 | ret = ustctl_release_object(sock, ua_chan->obj); |
fb45065e | 655 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
656 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
657 | ERR("UST app sock %d release channel obj failed with ret %d", | |
658 | sock, ret); | |
659 | } | |
7972aab2 | 660 | lttng_fd_put(LTTNG_FD_APPS, 1); |
edb67388 DG |
661 | free(ua_chan->obj); |
662 | } | |
36b588ed | 663 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
d80a6244 DG |
664 | } |
665 | ||
ebdb334b JR |
666 | static |
667 | void copy_ust_app_map_values(int sock, struct ust_app_map *ua_map, | |
668 | struct ust_app *app) | |
669 | { | |
670 | struct ltt_ust_map_dead_pid_kv_values *kv_pair_list = ua_map->dead_app_kv_values; | |
671 | struct ust_registry_session *ust_reg_sess; | |
672 | struct ust_registry_map *ust_reg_map; | |
673 | struct ust_registry_map_index_ht_entry *map_index_entry; | |
674 | struct lttng_ht_iter key_iter; | |
675 | struct lttng_ht *dead_app_kv_values; | |
676 | ||
677 | assert(app->buffer_type == LTTNG_BUFFER_PER_PID); | |
678 | ust_reg_sess = get_session_registry(ua_map->session); | |
679 | ||
680 | pthread_mutex_lock(&ust_reg_sess->lock); | |
681 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); | |
682 | pthread_mutex_unlock(&ust_reg_sess->lock); | |
683 | assert(ust_reg_map); | |
684 | ||
685 | DBG("Aggregating dead map values"); | |
686 | ||
687 | pthread_mutex_lock(&kv_pair_list->lock); | |
688 | ||
689 | if (app->bits_per_long == 32) { | |
690 | dead_app_kv_values = kv_pair_list->dead_app_kv_values_32bits; | |
691 | } else { | |
692 | dead_app_kv_values = kv_pair_list->dead_app_kv_values_64bits; | |
693 | } | |
694 | ||
695 | /* Iterate over all the formated_key -> counter index */ | |
696 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, | |
697 | &key_iter.iter, map_index_entry, node.node) { | |
698 | bool overflow = 0, underflow = 0; | |
699 | int64_t local_value = 0; | |
700 | int ret; | |
701 | size_t dimension_indexes[1] = {map_index_entry->index}; | |
702 | ||
703 | ret = ustctl_counter_aggregate(ua_map->map_handle, | |
704 | dimension_indexes, &local_value, &overflow, | |
705 | &underflow); | |
706 | if (ret) { | |
707 | ERR("Error getting counter value from the tracer: key = '%s'", | |
708 | map_index_entry->formated_key); | |
709 | ret = -1; | |
710 | goto end; | |
711 | } | |
712 | ||
713 | map_add_or_increment_map_values(dead_app_kv_values, | |
714 | map_index_entry->formated_key, local_value, | |
715 | underflow, overflow); | |
716 | ||
717 | } | |
718 | ||
719 | end: | |
720 | pthread_mutex_unlock(&kv_pair_list->lock); | |
721 | return; | |
722 | } | |
723 | /* | |
724 | * Delete ust app map safely. RCU read lock must be held before calling | |
725 | * this function. | |
726 | * | |
727 | * The session list lock must be held by the caller. | |
728 | */ | |
729 | static | |
730 | void delete_ust_app_map(int sock, struct ust_app_map *ua_map, | |
731 | struct ust_app *app) | |
732 | { | |
733 | int ret; | |
734 | struct lttng_ht_iter iter; | |
735 | struct ust_app_event *ua_event; | |
736 | struct ust_app_map_counter *map_counter, *ctmp; | |
737 | struct ust_registry_session *registry; | |
738 | ||
739 | assert(ua_map); | |
740 | ||
741 | DBG3("UST app deleting map %s", ua_map->name); | |
742 | ||
743 | /* Wipe stream */ | |
744 | cds_list_for_each_entry_safe(map_counter, ctmp, &ua_map->counters.head, list) { | |
745 | cds_list_del(&map_counter->list); | |
746 | delete_ust_app_map_counter(sock, map_counter, app); | |
747 | } | |
748 | ||
749 | /* Wipe events */ | |
750 | cds_lfht_for_each_entry(ua_map->events->ht, &iter.iter, ua_event, | |
751 | node.node) { | |
752 | ret = lttng_ht_del(ua_map->events, &iter); | |
753 | assert(!ret); | |
754 | delete_ust_app_event(sock, ua_event, app); | |
755 | } | |
756 | ||
757 | if (ua_map->session->buffer_type == LTTNG_BUFFER_PER_PID) { | |
758 | /* Wipe and free registry from session registry. */ | |
759 | registry = get_session_registry(ua_map->session); | |
760 | if (registry) { | |
761 | ust_registry_map_del_free(registry, ua_map->key); | |
762 | } | |
763 | } | |
764 | ||
765 | if (ua_map->obj != NULL) { | |
766 | /* Remove map from application UST object descriptor. */ | |
767 | iter.iter.node = &ua_map->ust_objd_node.node; | |
768 | ret = lttng_ht_del(app->ust_map_objd, &iter); | |
769 | assert(!ret); | |
770 | pthread_mutex_lock(&app->sock_lock); | |
771 | ret = ustctl_release_object(sock, ua_map->obj); | |
772 | pthread_mutex_unlock(&app->sock_lock); | |
773 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
774 | ERR("UST app sock %d release map obj failed with ret %d", | |
775 | sock, ret); | |
776 | } | |
777 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
778 | free(ua_map->obj); | |
779 | } | |
780 | call_rcu(&ua_map->rcu_head, delete_ust_app_map_rcu); | |
781 | } | |
782 | ||
fb45065e MD |
783 | int ust_app_register_done(struct ust_app *app) |
784 | { | |
785 | int ret; | |
786 | ||
787 | pthread_mutex_lock(&app->sock_lock); | |
788 | ret = ustctl_register_done(app->sock); | |
789 | pthread_mutex_unlock(&app->sock_lock); | |
790 | return ret; | |
791 | } | |
792 | ||
793 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data) | |
794 | { | |
795 | int ret, sock; | |
796 | ||
797 | if (app) { | |
798 | pthread_mutex_lock(&app->sock_lock); | |
799 | sock = app->sock; | |
800 | } else { | |
801 | sock = -1; | |
802 | } | |
803 | ret = ustctl_release_object(sock, data); | |
804 | if (app) { | |
805 | pthread_mutex_unlock(&app->sock_lock); | |
806 | } | |
807 | return ret; | |
808 | } | |
809 | ||
331744e3 | 810 | /* |
1b532a60 DG |
811 | * Push metadata to consumer socket. |
812 | * | |
dc2bbdae MD |
813 | * RCU read-side lock must be held to guarantee existance of socket. |
814 | * Must be called with the ust app session lock held. | |
815 | * Must be called with the registry lock held. | |
331744e3 JD |
816 | * |
817 | * On success, return the len of metadata pushed or else a negative value. | |
2c57e06d MD |
818 | * Returning a -EPIPE return value means we could not send the metadata, |
819 | * but it can be caused by recoverable errors (e.g. the application has | |
820 | * terminated concurrently). | |
331744e3 JD |
821 | */ |
822 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, | |
823 | struct consumer_socket *socket, int send_zero_data) | |
824 | { | |
825 | int ret; | |
826 | char *metadata_str = NULL; | |
c585821b | 827 | size_t len, offset, new_metadata_len_sent; |
331744e3 | 828 | ssize_t ret_val; |
93ec662e | 829 | uint64_t metadata_key, metadata_version; |
331744e3 JD |
830 | |
831 | assert(registry); | |
832 | assert(socket); | |
1b532a60 | 833 | |
c585821b MD |
834 | metadata_key = registry->metadata_key; |
835 | ||
ce34fcd0 | 836 | /* |
dc2bbdae MD |
837 | * Means that no metadata was assigned to the session. This can |
838 | * happens if no start has been done previously. | |
ce34fcd0 | 839 | */ |
c585821b | 840 | if (!metadata_key) { |
ce34fcd0 MD |
841 | return 0; |
842 | } | |
843 | ||
331744e3 JD |
844 | offset = registry->metadata_len_sent; |
845 | len = registry->metadata_len - registry->metadata_len_sent; | |
c585821b | 846 | new_metadata_len_sent = registry->metadata_len; |
93ec662e | 847 | metadata_version = registry->metadata_version; |
331744e3 JD |
848 | if (len == 0) { |
849 | DBG3("No metadata to push for metadata key %" PRIu64, | |
850 | registry->metadata_key); | |
851 | ret_val = len; | |
852 | if (send_zero_data) { | |
853 | DBG("No metadata to push"); | |
854 | goto push_data; | |
855 | } | |
856 | goto end; | |
857 | } | |
858 | ||
859 | /* Allocate only what we have to send. */ | |
860 | metadata_str = zmalloc(len); | |
861 | if (!metadata_str) { | |
862 | PERROR("zmalloc ust app metadata string"); | |
863 | ret_val = -ENOMEM; | |
864 | goto error; | |
865 | } | |
c585821b | 866 | /* Copy what we haven't sent out. */ |
331744e3 | 867 | memcpy(metadata_str, registry->metadata + offset, len); |
331744e3 JD |
868 | |
869 | push_data: | |
c585821b MD |
870 | pthread_mutex_unlock(®istry->lock); |
871 | /* | |
872 | * We need to unlock the registry while we push metadata to | |
873 | * break a circular dependency between the consumerd metadata | |
874 | * lock and the sessiond registry lock. Indeed, pushing metadata | |
875 | * to the consumerd awaits that it gets pushed all the way to | |
876 | * relayd, but doing so requires grabbing the metadata lock. If | |
877 | * a concurrent metadata request is being performed by | |
878 | * consumerd, this can try to grab the registry lock on the | |
879 | * sessiond while holding the metadata lock on the consumer | |
880 | * daemon. Those push and pull schemes are performed on two | |
881 | * different bidirectionnal communication sockets. | |
882 | */ | |
883 | ret = consumer_push_metadata(socket, metadata_key, | |
93ec662e | 884 | metadata_str, len, offset, metadata_version); |
c585821b | 885 | pthread_mutex_lock(®istry->lock); |
331744e3 | 886 | if (ret < 0) { |
000baf6a | 887 | /* |
dc2bbdae MD |
888 | * There is an acceptable race here between the registry |
889 | * metadata key assignment and the creation on the | |
890 | * consumer. The session daemon can concurrently push | |
891 | * metadata for this registry while being created on the | |
892 | * consumer since the metadata key of the registry is | |
893 | * assigned *before* it is setup to avoid the consumer | |
894 | * to ask for metadata that could possibly be not found | |
895 | * in the session daemon. | |
000baf6a | 896 | * |
dc2bbdae MD |
897 | * The metadata will get pushed either by the session |
898 | * being stopped or the consumer requesting metadata if | |
899 | * that race is triggered. | |
000baf6a DG |
900 | */ |
901 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { | |
902 | ret = 0; | |
c585821b MD |
903 | } else { |
904 | ERR("Error pushing metadata to consumer"); | |
000baf6a | 905 | } |
331744e3 JD |
906 | ret_val = ret; |
907 | goto error_push; | |
c585821b MD |
908 | } else { |
909 | /* | |
910 | * Metadata may have been concurrently pushed, since | |
911 | * we're not holding the registry lock while pushing to | |
912 | * consumer. This is handled by the fact that we send | |
913 | * the metadata content, size, and the offset at which | |
914 | * that metadata belongs. This may arrive out of order | |
915 | * on the consumer side, and the consumer is able to | |
916 | * deal with overlapping fragments. The consumer | |
917 | * supports overlapping fragments, which must be | |
918 | * contiguous starting from offset 0. We keep the | |
919 | * largest metadata_len_sent value of the concurrent | |
920 | * send. | |
921 | */ | |
922 | registry->metadata_len_sent = | |
923 | max_t(size_t, registry->metadata_len_sent, | |
924 | new_metadata_len_sent); | |
331744e3 | 925 | } |
331744e3 JD |
926 | free(metadata_str); |
927 | return len; | |
928 | ||
929 | end: | |
930 | error: | |
ce34fcd0 MD |
931 | if (ret_val) { |
932 | /* | |
dc2bbdae MD |
933 | * On error, flag the registry that the metadata is |
934 | * closed. We were unable to push anything and this | |
935 | * means that either the consumer is not responding or | |
936 | * the metadata cache has been destroyed on the | |
937 | * consumer. | |
ce34fcd0 MD |
938 | */ |
939 | registry->metadata_closed = 1; | |
940 | } | |
331744e3 JD |
941 | error_push: |
942 | free(metadata_str); | |
943 | return ret_val; | |
944 | } | |
945 | ||
d88aee68 | 946 | /* |
ce34fcd0 | 947 | * For a given application and session, push metadata to consumer. |
331744e3 JD |
948 | * Either sock or consumer is required : if sock is NULL, the default |
949 | * socket to send the metadata is retrieved from consumer, if sock | |
950 | * is not NULL we use it to send the metadata. | |
ce34fcd0 | 951 | * RCU read-side lock must be held while calling this function, |
dc2bbdae MD |
952 | * therefore ensuring existance of registry. It also ensures existance |
953 | * of socket throughout this function. | |
d88aee68 DG |
954 | * |
955 | * Return 0 on success else a negative error. | |
2c57e06d MD |
956 | * Returning a -EPIPE return value means we could not send the metadata, |
957 | * but it can be caused by recoverable errors (e.g. the application has | |
958 | * terminated concurrently). | |
d88aee68 | 959 | */ |
7972aab2 DG |
960 | static int push_metadata(struct ust_registry_session *registry, |
961 | struct consumer_output *consumer) | |
d88aee68 | 962 | { |
331744e3 JD |
963 | int ret_val; |
964 | ssize_t ret; | |
d88aee68 DG |
965 | struct consumer_socket *socket; |
966 | ||
7972aab2 DG |
967 | assert(registry); |
968 | assert(consumer); | |
969 | ||
ce34fcd0 | 970 | pthread_mutex_lock(®istry->lock); |
ce34fcd0 | 971 | if (registry->metadata_closed) { |
dc2bbdae MD |
972 | ret_val = -EPIPE; |
973 | goto error; | |
d88aee68 DG |
974 | } |
975 | ||
d88aee68 | 976 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
977 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
978 | consumer); | |
d88aee68 | 979 | if (!socket) { |
331744e3 | 980 | ret_val = -1; |
ce34fcd0 | 981 | goto error; |
d88aee68 DG |
982 | } |
983 | ||
331744e3 | 984 | ret = ust_app_push_metadata(registry, socket, 0); |
d88aee68 | 985 | if (ret < 0) { |
331744e3 | 986 | ret_val = ret; |
ce34fcd0 | 987 | goto error; |
d88aee68 | 988 | } |
dc2bbdae | 989 | pthread_mutex_unlock(®istry->lock); |
d88aee68 DG |
990 | return 0; |
991 | ||
ce34fcd0 | 992 | error: |
dc2bbdae | 993 | pthread_mutex_unlock(®istry->lock); |
331744e3 | 994 | return ret_val; |
d88aee68 DG |
995 | } |
996 | ||
997 | /* | |
998 | * Send to the consumer a close metadata command for the given session. Once | |
999 | * done, the metadata channel is deleted and the session metadata pointer is | |
dc2bbdae | 1000 | * nullified. The session lock MUST be held unless the application is |
d88aee68 DG |
1001 | * in the destroy path. |
1002 | * | |
a70ac2f4 MD |
1003 | * Do not hold the registry lock while communicating with the consumerd, because |
1004 | * doing so causes inter-process deadlocks between consumerd and sessiond with | |
1005 | * the metadata request notification. | |
1006 | * | |
d88aee68 DG |
1007 | * Return 0 on success else a negative value. |
1008 | */ | |
7972aab2 DG |
1009 | static int close_metadata(struct ust_registry_session *registry, |
1010 | struct consumer_output *consumer) | |
d88aee68 DG |
1011 | { |
1012 | int ret; | |
1013 | struct consumer_socket *socket; | |
a70ac2f4 MD |
1014 | uint64_t metadata_key; |
1015 | bool registry_was_already_closed; | |
d88aee68 | 1016 | |
7972aab2 DG |
1017 | assert(registry); |
1018 | assert(consumer); | |
d88aee68 | 1019 | |
7972aab2 DG |
1020 | rcu_read_lock(); |
1021 | ||
ce34fcd0 | 1022 | pthread_mutex_lock(®istry->lock); |
a70ac2f4 MD |
1023 | metadata_key = registry->metadata_key; |
1024 | registry_was_already_closed = registry->metadata_closed; | |
1025 | if (metadata_key != 0) { | |
1026 | /* | |
1027 | * Metadata closed. Even on error this means that the consumer | |
1028 | * is not responding or not found so either way a second close | |
1029 | * should NOT be emit for this registry. | |
1030 | */ | |
1031 | registry->metadata_closed = 1; | |
1032 | } | |
1033 | pthread_mutex_unlock(®istry->lock); | |
ce34fcd0 | 1034 | |
a70ac2f4 | 1035 | if (metadata_key == 0 || registry_was_already_closed) { |
d88aee68 | 1036 | ret = 0; |
1b532a60 | 1037 | goto end; |
d88aee68 DG |
1038 | } |
1039 | ||
d88aee68 | 1040 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
1041 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
1042 | consumer); | |
d88aee68 DG |
1043 | if (!socket) { |
1044 | ret = -1; | |
a70ac2f4 | 1045 | goto end; |
d88aee68 DG |
1046 | } |
1047 | ||
a70ac2f4 | 1048 | ret = consumer_close_metadata(socket, metadata_key); |
d88aee68 | 1049 | if (ret < 0) { |
a70ac2f4 | 1050 | goto end; |
d88aee68 DG |
1051 | } |
1052 | ||
1b532a60 | 1053 | end: |
7972aab2 | 1054 | rcu_read_unlock(); |
d88aee68 DG |
1055 | return ret; |
1056 | } | |
1057 | ||
36b588ed MD |
1058 | /* |
1059 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
1060 | * section and outside of call_rcu thread, so we postpone its execution |
1061 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
1062 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
1063 | */ |
1064 | static | |
1065 | void delete_ust_app_session_rcu(struct rcu_head *head) | |
1066 | { | |
1067 | struct ust_app_session *ua_sess = | |
1068 | caa_container_of(head, struct ust_app_session, rcu_head); | |
1069 | ||
0b2dc8df | 1070 | ht_cleanup_push(ua_sess->channels); |
ebdb334b | 1071 | ht_cleanup_push(ua_sess->maps); |
36b588ed MD |
1072 | free(ua_sess); |
1073 | } | |
1074 | ||
d80a6244 DG |
1075 | /* |
1076 | * Delete ust app session safely. RCU read lock must be held before calling | |
1077 | * this function. | |
82cac6d2 JG |
1078 | * |
1079 | * The session list lock must be held by the caller. | |
d80a6244 | 1080 | */ |
8b366481 | 1081 | static |
d0b96690 DG |
1082 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
1083 | struct ust_app *app) | |
d80a6244 DG |
1084 | { |
1085 | int ret; | |
bec39940 | 1086 | struct lttng_ht_iter iter; |
d80a6244 | 1087 | struct ust_app_channel *ua_chan; |
ebdb334b | 1088 | struct ust_app_map *ua_map; |
7972aab2 | 1089 | struct ust_registry_session *registry; |
d80a6244 | 1090 | |
d88aee68 DG |
1091 | assert(ua_sess); |
1092 | ||
1b532a60 DG |
1093 | pthread_mutex_lock(&ua_sess->lock); |
1094 | ||
b161602a MD |
1095 | assert(!ua_sess->deleted); |
1096 | ua_sess->deleted = true; | |
1097 | ||
7972aab2 | 1098 | registry = get_session_registry(ua_sess); |
fad1ed2f | 1099 | /* Registry can be null on error path during initialization. */ |
ce34fcd0 | 1100 | if (registry) { |
d88aee68 | 1101 | /* Push metadata for application before freeing the application. */ |
7972aab2 | 1102 | (void) push_metadata(registry, ua_sess->consumer); |
d88aee68 | 1103 | |
7972aab2 DG |
1104 | /* |
1105 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
1106 | * metadata only on destroy trace session in this case. Also, the |
1107 | * previous push metadata could have flag the metadata registry to | |
1108 | * close so don't send a close command if closed. | |
7972aab2 | 1109 | */ |
ce34fcd0 | 1110 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
1111 | /* And ask to close it for this session registry. */ |
1112 | (void) close_metadata(registry, ua_sess->consumer); | |
1113 | } | |
d80a6244 DG |
1114 | } |
1115 | ||
bec39940 DG |
1116 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
1117 | node.node) { | |
1118 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
525b0740 | 1119 | assert(!ret); |
d0b96690 | 1120 | delete_ust_app_channel(sock, ua_chan, app); |
d80a6244 | 1121 | } |
d80a6244 | 1122 | |
ebdb334b JR |
1123 | cds_lfht_for_each_entry(ua_sess->maps->ht, &iter.iter, ua_map, |
1124 | node.node) { | |
1125 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
1126 | copy_ust_app_map_values(sock, ua_map, app); | |
1127 | } | |
1128 | ret = lttng_ht_del(ua_sess->maps, &iter); | |
1129 | assert(!ret); | |
1130 | delete_ust_app_map(sock, ua_map, app); | |
1131 | } | |
1132 | ||
7972aab2 DG |
1133 | /* In case of per PID, the registry is kept in the session. */ |
1134 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
1135 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
1136 | if (reg_pid) { | |
fad1ed2f JR |
1137 | /* |
1138 | * Registry can be null on error path during | |
1139 | * initialization. | |
1140 | */ | |
7972aab2 DG |
1141 | buffer_reg_pid_remove(reg_pid); |
1142 | buffer_reg_pid_destroy(reg_pid); | |
1143 | } | |
1144 | } | |
d0b96690 | 1145 | |
aee6bafd | 1146 | if (ua_sess->handle != -1) { |
fb45065e | 1147 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 1148 | ret = ustctl_release_handle(sock, ua_sess->handle); |
fb45065e | 1149 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
1150 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1151 | ERR("UST app sock %d release session handle failed with ret %d", | |
1152 | sock, ret); | |
1153 | } | |
10b56aef MD |
1154 | /* Remove session from application UST object descriptor. */ |
1155 | iter.iter.node = &ua_sess->ust_objd_node.node; | |
1156 | ret = lttng_ht_del(app->ust_sessions_objd, &iter); | |
1157 | assert(!ret); | |
aee6bafd | 1158 | } |
10b56aef | 1159 | |
1b532a60 DG |
1160 | pthread_mutex_unlock(&ua_sess->lock); |
1161 | ||
6addfa37 MD |
1162 | consumer_output_put(ua_sess->consumer); |
1163 | ||
36b588ed | 1164 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
d80a6244 | 1165 | } |
91d76f53 DG |
1166 | |
1167 | /* | |
284d8f55 DG |
1168 | * Delete a traceable application structure from the global list. Never call |
1169 | * this function outside of a call_rcu call. | |
36b588ed MD |
1170 | * |
1171 | * RCU read side lock should _NOT_ be held when calling this function. | |
91d76f53 | 1172 | */ |
8b366481 DG |
1173 | static |
1174 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 1175 | { |
8b366481 | 1176 | int ret, sock; |
d42f20df | 1177 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
993578ff JR |
1178 | struct lttng_ht_iter iter; |
1179 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
5e2abfaf | 1180 | bool event_notifier_write_fd_is_open; |
44d3bd01 | 1181 | |
82cac6d2 JG |
1182 | /* |
1183 | * The session list lock must be held during this function to guarantee | |
1184 | * the existence of ua_sess. | |
1185 | */ | |
1186 | session_lock_list(); | |
d80a6244 | 1187 | /* Delete ust app sessions info */ |
852d0037 DG |
1188 | sock = app->sock; |
1189 | app->sock = -1; | |
d80a6244 | 1190 | |
8b366481 | 1191 | /* Wipe sessions */ |
d42f20df DG |
1192 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
1193 | teardown_node) { | |
1194 | /* Free every object in the session and the session. */ | |
36b588ed | 1195 | rcu_read_lock(); |
d0b96690 | 1196 | delete_ust_app_session(sock, ua_sess, app); |
36b588ed | 1197 | rcu_read_unlock(); |
d80a6244 | 1198 | } |
36b588ed | 1199 | |
993578ff JR |
1200 | /* Remove the event notifier rules associated with this app. */ |
1201 | rcu_read_lock(); | |
1202 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
1203 | &iter.iter, event_notifier_rule, node.node) { | |
1204 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter); | |
1205 | assert(!ret); | |
1206 | ||
1207 | delete_ust_app_event_notifier_rule( | |
1208 | app->sock, event_notifier_rule, app); | |
1209 | } | |
1210 | ||
1211 | rcu_read_unlock(); | |
1212 | ||
0b2dc8df | 1213 | ht_cleanup_push(app->sessions); |
10b56aef | 1214 | ht_cleanup_push(app->ust_sessions_objd); |
ebdb334b JR |
1215 | ht_cleanup_push(app->ust_chan_objd); |
1216 | ht_cleanup_push(app->ust_map_objd); | |
993578ff | 1217 | ht_cleanup_push(app->token_to_event_notifier_rule_ht); |
d80a6244 | 1218 | |
da873412 JR |
1219 | /* |
1220 | * This could be NULL if the event notifier setup failed (e.g the app | |
1221 | * was killed or the tracer does not support this feature). | |
1222 | */ | |
1223 | if (app->event_notifier_group.object) { | |
1224 | enum lttng_error_code ret_code; | |
ebdb334b JR |
1225 | enum event_notifier_error_accounting_status status; |
1226 | ||
da873412 JR |
1227 | const int event_notifier_read_fd = lttng_pipe_get_readfd( |
1228 | app->event_notifier_group.event_pipe); | |
1229 | ||
1230 | ret_code = notification_thread_command_remove_tracer_event_source( | |
1231 | notification_thread_handle, | |
1232 | event_notifier_read_fd); | |
1233 | if (ret_code != LTTNG_OK) { | |
1234 | ERR("Failed to remove application tracer event source from notification thread"); | |
1235 | } | |
1236 | ||
ebdb334b JR |
1237 | status = event_notifier_error_accounting_unregister_app(app); |
1238 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
1239 | ERR("Error unregistering app from event notifier error accounting"); | |
1240 | } | |
1241 | ||
da873412 JR |
1242 | ustctl_release_object(sock, app->event_notifier_group.object); |
1243 | free(app->event_notifier_group.object); | |
1244 | } | |
1245 | ||
5e2abfaf JG |
1246 | event_notifier_write_fd_is_open = lttng_pipe_is_write_open( |
1247 | app->event_notifier_group.event_pipe); | |
da873412 | 1248 | lttng_pipe_destroy(app->event_notifier_group.event_pipe); |
5e2abfaf JG |
1249 | /* |
1250 | * Release the file descriptors reserved for the event notifier pipe. | |
1251 | * The app could be destroyed before the write end of the pipe could be | |
1252 | * passed to the application (and closed). In that case, both file | |
1253 | * descriptors must be released. | |
1254 | */ | |
1255 | lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1); | |
da873412 | 1256 | |
6414a713 | 1257 | /* |
852d0037 DG |
1258 | * Wait until we have deleted the application from the sock hash table |
1259 | * before closing this socket, otherwise an application could re-use the | |
1260 | * socket ID and race with the teardown, using the same hash table entry. | |
1261 | * | |
1262 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
1263 | * all RCU readers that could run concurrently with unregister app, | |
1264 | * therefore we _need_ to only close that socket after a grace period. So | |
1265 | * it should stay in this RCU callback. | |
1266 | * | |
1267 | * This close() is a very important step of the synchronization model so | |
1268 | * every modification to this function must be carefully reviewed. | |
6414a713 | 1269 | */ |
799e2c4f MD |
1270 | ret = close(sock); |
1271 | if (ret) { | |
1272 | PERROR("close"); | |
1273 | } | |
4063050c | 1274 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 1275 | |
852d0037 | 1276 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 1277 | free(app); |
82cac6d2 | 1278 | session_unlock_list(); |
099e26bd DG |
1279 | } |
1280 | ||
1281 | /* | |
f6a9efaa | 1282 | * URCU intermediate call to delete an UST app. |
099e26bd | 1283 | */ |
8b366481 DG |
1284 | static |
1285 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 1286 | { |
bec39940 DG |
1287 | struct lttng_ht_node_ulong *node = |
1288 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa | 1289 | struct ust_app *app = |
852d0037 | 1290 | caa_container_of(node, struct ust_app, pid_n); |
f6a9efaa | 1291 | |
852d0037 | 1292 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 1293 | delete_ust_app(app); |
099e26bd DG |
1294 | } |
1295 | ||
ffe60014 DG |
1296 | /* |
1297 | * Delete the session from the application ht and delete the data structure by | |
1298 | * freeing every object inside and releasing them. | |
82cac6d2 JG |
1299 | * |
1300 | * The session list lock must be held by the caller. | |
ffe60014 | 1301 | */ |
d0b96690 | 1302 | static void destroy_app_session(struct ust_app *app, |
ffe60014 DG |
1303 | struct ust_app_session *ua_sess) |
1304 | { | |
1305 | int ret; | |
1306 | struct lttng_ht_iter iter; | |
1307 | ||
1308 | assert(app); | |
1309 | assert(ua_sess); | |
1310 | ||
1311 | iter.iter.node = &ua_sess->node.node; | |
1312 | ret = lttng_ht_del(app->sessions, &iter); | |
1313 | if (ret) { | |
1314 | /* Already scheduled for teardown. */ | |
1315 | goto end; | |
1316 | } | |
1317 | ||
1318 | /* Once deleted, free the data structure. */ | |
d0b96690 | 1319 | delete_ust_app_session(app->sock, ua_sess, app); |
ffe60014 DG |
1320 | |
1321 | end: | |
1322 | return; | |
1323 | } | |
1324 | ||
8b366481 DG |
1325 | /* |
1326 | * Alloc new UST app session. | |
1327 | */ | |
1328 | static | |
40bbd087 | 1329 | struct ust_app_session *alloc_ust_app_session(void) |
8b366481 DG |
1330 | { |
1331 | struct ust_app_session *ua_sess; | |
1332 | ||
1333 | /* Init most of the default value by allocating and zeroing */ | |
1334 | ua_sess = zmalloc(sizeof(struct ust_app_session)); | |
1335 | if (ua_sess == NULL) { | |
1336 | PERROR("malloc"); | |
ffe60014 | 1337 | goto error_free; |
8b366481 DG |
1338 | } |
1339 | ||
1340 | ua_sess->handle = -1; | |
bec39940 | 1341 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
ad7a9107 | 1342 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
ebdb334b | 1343 | ua_sess->maps = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
84ad93e8 | 1344 | pthread_mutex_init(&ua_sess->lock, NULL); |
ad7a9107 | 1345 | |
8b366481 DG |
1346 | return ua_sess; |
1347 | ||
ffe60014 | 1348 | error_free: |
8b366481 DG |
1349 | return NULL; |
1350 | } | |
1351 | ||
1352 | /* | |
1353 | * Alloc new UST app channel. | |
1354 | */ | |
1355 | static | |
b53d4e59 | 1356 | struct ust_app_channel *alloc_ust_app_channel(const char *name, |
d0b96690 | 1357 | struct ust_app_session *ua_sess, |
ffe60014 | 1358 | struct lttng_ust_channel_attr *attr) |
8b366481 DG |
1359 | { |
1360 | struct ust_app_channel *ua_chan; | |
1361 | ||
1362 | /* Init most of the default value by allocating and zeroing */ | |
1363 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); | |
1364 | if (ua_chan == NULL) { | |
1365 | PERROR("malloc"); | |
1366 | goto error; | |
1367 | } | |
1368 | ||
1369 | /* Setup channel name */ | |
1370 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
1371 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
1372 | ||
1373 | ua_chan->enabled = 1; | |
1374 | ua_chan->handle = -1; | |
45893984 | 1375 | ua_chan->session = ua_sess; |
ffe60014 | 1376 | ua_chan->key = get_next_channel_key(); |
bec39940 DG |
1377 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
1378 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
1379 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
1380 | |
1381 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
31746f93 | 1382 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
8b366481 DG |
1383 | |
1384 | /* Copy attributes */ | |
1385 | if (attr) { | |
ffe60014 | 1386 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
2fe6e7f5 DG |
1387 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
1388 | ua_chan->attr.num_subbuf = attr->num_subbuf; | |
1389 | ua_chan->attr.overwrite = attr->overwrite; | |
1390 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; | |
1391 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; | |
1392 | ua_chan->attr.output = attr->output; | |
491d1539 | 1393 | ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout; |
8b366481 | 1394 | } |
ffe60014 DG |
1395 | /* By default, the channel is a per cpu channel. */ |
1396 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8b366481 DG |
1397 | |
1398 | DBG3("UST app channel %s allocated", ua_chan->name); | |
1399 | ||
1400 | return ua_chan; | |
1401 | ||
1402 | error: | |
1403 | return NULL; | |
1404 | } | |
1405 | ||
ebdb334b JR |
1406 | /* |
1407 | * Alloc new UST app map. | |
1408 | */ | |
1409 | static | |
1410 | struct ust_app_map *alloc_ust_app_map(const char *name, | |
1411 | struct ust_app_session *ua_sess) | |
1412 | { | |
1413 | struct ust_app_map *ua_map; | |
1414 | ||
1415 | /* Init most of the default value by allocating and zeroing */ | |
1416 | ua_map = zmalloc(sizeof(struct ust_app_map)); | |
1417 | if (ua_map == NULL) { | |
1418 | PERROR("malloc"); | |
1419 | goto error; | |
1420 | } | |
1421 | ||
1422 | /* Setup map name */ | |
1423 | strncpy(ua_map->name, name, sizeof(ua_map->name)); | |
1424 | ua_map->name[sizeof(ua_map->name) - 1] = '\0'; | |
1425 | ||
1426 | ua_map->enabled = 1; | |
1427 | ua_map->handle = -1; | |
1428 | ua_map->session = ua_sess; | |
1429 | ua_map->key = get_next_map_key(); | |
1430 | ua_map->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
1431 | lttng_ht_node_init_str(&ua_map->node, ua_map->name); | |
1432 | ||
1433 | CDS_INIT_LIST_HEAD(&ua_map->counters.head); | |
1434 | ||
1435 | DBG3("UST app map %s allocated", ua_map->name); | |
1436 | ||
1437 | return ua_map; | |
1438 | ||
1439 | error: | |
1440 | return NULL; | |
1441 | } | |
1442 | ||
37f1c236 DG |
1443 | /* |
1444 | * Allocate and initialize a UST app stream. | |
1445 | * | |
1446 | * Return newly allocated stream pointer or NULL on error. | |
1447 | */ | |
ffe60014 | 1448 | struct ust_app_stream *ust_app_alloc_stream(void) |
37f1c236 DG |
1449 | { |
1450 | struct ust_app_stream *stream = NULL; | |
1451 | ||
1452 | stream = zmalloc(sizeof(*stream)); | |
1453 | if (stream == NULL) { | |
1454 | PERROR("zmalloc ust app stream"); | |
1455 | goto error; | |
1456 | } | |
1457 | ||
1458 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
1459 | stream->handle = -1; | |
1460 | ||
1461 | error: | |
1462 | return stream; | |
1463 | } | |
1464 | ||
ebdb334b JR |
1465 | /* |
1466 | * Allocate and initialize a UST app map_counter. | |
1467 | * | |
1468 | * Return newly allocated map_counter pointer or NULL on error. | |
1469 | */ | |
1470 | struct ust_app_map_counter *ust_app_alloc_map_counter(void) | |
1471 | { | |
1472 | struct ust_app_map_counter *map_counter = NULL; | |
1473 | ||
1474 | map_counter = zmalloc(sizeof(*map_counter)); | |
1475 | if (map_counter == NULL) { | |
1476 | PERROR("zmalloc ust app map_counter"); | |
1477 | goto error; | |
1478 | } | |
1479 | ||
1480 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
1481 | map_counter->handle = -1; | |
1482 | ||
1483 | error: | |
1484 | return map_counter; | |
1485 | } | |
1486 | ||
8b366481 DG |
1487 | /* |
1488 | * Alloc new UST app event. | |
1489 | */ | |
1490 | static | |
1491 | struct ust_app_event *alloc_ust_app_event(char *name, | |
1492 | struct lttng_ust_event *attr) | |
1493 | { | |
1494 | struct ust_app_event *ua_event; | |
1495 | ||
1496 | /* Init most of the default value by allocating and zeroing */ | |
1497 | ua_event = zmalloc(sizeof(struct ust_app_event)); | |
1498 | if (ua_event == NULL) { | |
20533947 | 1499 | PERROR("Failed to allocate ust_app_event structure"); |
8b366481 DG |
1500 | goto error; |
1501 | } | |
1502 | ||
1503 | ua_event->enabled = 1; | |
1504 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
1505 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 | 1506 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
8b366481 DG |
1507 | |
1508 | /* Copy attributes */ | |
1509 | if (attr) { | |
1510 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
1511 | } | |
1512 | ||
1513 | DBG3("UST app event %s allocated", ua_event->name); | |
1514 | ||
1515 | return ua_event; | |
1516 | ||
1517 | error: | |
1518 | return NULL; | |
1519 | } | |
1520 | ||
ebdb334b | 1521 | |
993578ff JR |
1522 | /* |
1523 | * Allocate a new UST app event notifier rule. | |
1524 | */ | |
1525 | static struct ust_app_event_notifier_rule *alloc_ust_app_event_notifier_rule( | |
267d66aa | 1526 | struct lttng_trigger *trigger) |
993578ff JR |
1527 | { |
1528 | enum lttng_event_rule_generate_exclusions_status | |
1529 | generate_exclusion_status; | |
1530 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
267d66aa JR |
1531 | struct lttng_condition *condition = NULL; |
1532 | const struct lttng_event_rule *event_rule = NULL; | |
993578ff JR |
1533 | |
1534 | ua_event_notifier_rule = zmalloc(sizeof(struct ust_app_event_notifier_rule)); | |
1535 | if (ua_event_notifier_rule == NULL) { | |
1536 | PERROR("Failed to allocate ust_app_event_notifier_rule structure"); | |
1537 | goto error; | |
1538 | } | |
1539 | ||
1540 | ua_event_notifier_rule->enabled = 1; | |
267d66aa JR |
1541 | ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger); |
1542 | lttng_ht_node_init_u64(&ua_event_notifier_rule->node, | |
1543 | ua_event_notifier_rule->token); | |
993578ff | 1544 | |
267d66aa JR |
1545 | condition = lttng_trigger_get_condition(trigger); |
1546 | assert(condition); | |
ebdb334b | 1547 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); |
267d66aa | 1548 | |
ebdb334b | 1549 | assert(LTTNG_CONDITION_STATUS_OK == lttng_condition_on_event_get_rule(condition, &event_rule)); |
267d66aa | 1550 | assert(event_rule); |
993578ff | 1551 | |
267d66aa JR |
1552 | /* Acquire the event notifier's reference to the trigger. */ |
1553 | lttng_trigger_get(trigger); | |
1554 | ||
1555 | ua_event_notifier_rule->trigger = trigger; | |
993578ff JR |
1556 | ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule); |
1557 | generate_exclusion_status = lttng_event_rule_generate_exclusions( | |
1558 | event_rule, &ua_event_notifier_rule->exclusion); | |
1559 | switch (generate_exclusion_status) { | |
1560 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK: | |
1561 | case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE: | |
1562 | break; | |
1563 | default: | |
1564 | /* Error occured. */ | |
267d66aa JR |
1565 | ERR("Failed to generate exclusions from trigger while allocating an event notifier rule"); |
1566 | goto error_put_trigger; | |
993578ff JR |
1567 | } |
1568 | ||
1569 | DBG3("UST app event notifier rule allocated: token = %" PRIu64, | |
1570 | ua_event_notifier_rule->token); | |
1571 | ||
1572 | return ua_event_notifier_rule; | |
1573 | ||
267d66aa JR |
1574 | error_put_trigger: |
1575 | lttng_trigger_put(trigger); | |
993578ff JR |
1576 | error: |
1577 | free(ua_event_notifier_rule); | |
1578 | return NULL; | |
1579 | } | |
1580 | ||
8b366481 DG |
1581 | /* |
1582 | * Alloc new UST app context. | |
1583 | */ | |
1584 | static | |
bdf64013 | 1585 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx) |
8b366481 DG |
1586 | { |
1587 | struct ust_app_ctx *ua_ctx; | |
1588 | ||
1589 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); | |
1590 | if (ua_ctx == NULL) { | |
1591 | goto error; | |
1592 | } | |
1593 | ||
31746f93 DG |
1594 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
1595 | ||
8b366481 DG |
1596 | if (uctx) { |
1597 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
bdf64013 | 1598 | if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) { |
f3db82be | 1599 | char *provider_name = NULL, *ctx_name = NULL; |
bdf64013 JG |
1600 | |
1601 | provider_name = strdup(uctx->u.app_ctx.provider_name); | |
1602 | ctx_name = strdup(uctx->u.app_ctx.ctx_name); | |
1603 | if (!provider_name || !ctx_name) { | |
1604 | free(provider_name); | |
1605 | free(ctx_name); | |
1606 | goto error; | |
1607 | } | |
1608 | ||
1609 | ua_ctx->ctx.u.app_ctx.provider_name = provider_name; | |
1610 | ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name; | |
1611 | } | |
8b366481 DG |
1612 | } |
1613 | ||
1614 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
8b366481 | 1615 | return ua_ctx; |
bdf64013 JG |
1616 | error: |
1617 | free(ua_ctx); | |
1618 | return NULL; | |
8b366481 DG |
1619 | } |
1620 | ||
51755dc8 JG |
1621 | /* |
1622 | * Create a liblttng-ust filter bytecode from given bytecode. | |
1623 | * | |
1624 | * Return allocated filter or NULL on error. | |
1625 | */ | |
f2eafd2d JR |
1626 | static struct lttng_ust_filter_bytecode * |
1627 | create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) | |
51755dc8 JG |
1628 | { |
1629 | struct lttng_ust_filter_bytecode *filter = NULL; | |
1630 | ||
f2eafd2d | 1631 | /* Copy filter bytecode. */ |
51755dc8 JG |
1632 | filter = zmalloc(sizeof(*filter) + orig_f->len); |
1633 | if (!filter) { | |
f2eafd2d | 1634 | PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); |
51755dc8 JG |
1635 | goto error; |
1636 | } | |
1637 | ||
2b00d462 | 1638 | assert(sizeof(struct lttng_bytecode) == |
51755dc8 JG |
1639 | sizeof(struct lttng_ust_filter_bytecode)); |
1640 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1641 | error: | |
1642 | return filter; | |
1643 | } | |
1644 | ||
f2eafd2d JR |
1645 | /* |
1646 | * Create a liblttng-ust capture bytecode from given bytecode. | |
1647 | * | |
1648 | * Return allocated filter or NULL on error. | |
1649 | */ | |
1650 | static struct lttng_ust_capture_bytecode * | |
1651 | create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f) | |
1652 | { | |
1653 | struct lttng_ust_capture_bytecode *capture = NULL; | |
1654 | ||
1655 | /* Copy capture bytecode. */ | |
1656 | capture = zmalloc(sizeof(*capture) + orig_f->len); | |
1657 | if (!capture) { | |
1658 | PERROR("Failed to allocate lttng_ust_capture_bytecode: bytecode len = %" PRIu32 " bytes", orig_f->len); | |
1659 | goto error; | |
1660 | } | |
1661 | ||
1662 | assert(sizeof(struct lttng_bytecode) == | |
1663 | sizeof(struct lttng_ust_capture_bytecode)); | |
1664 | memcpy(capture, orig_f, sizeof(*capture) + orig_f->len); | |
1665 | error: | |
1666 | return capture; | |
1667 | } | |
1668 | ||
099e26bd | 1669 | /* |
421cb601 DG |
1670 | * Find an ust_app using the sock and return it. RCU read side lock must be |
1671 | * held before calling this helper function. | |
099e26bd | 1672 | */ |
f20baf8e | 1673 | struct ust_app *ust_app_find_by_sock(int sock) |
099e26bd | 1674 | { |
bec39940 | 1675 | struct lttng_ht_node_ulong *node; |
bec39940 | 1676 | struct lttng_ht_iter iter; |
f6a9efaa | 1677 | |
852d0037 | 1678 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1679 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
1680 | if (node == NULL) { |
1681 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
1682 | goto error; |
1683 | } | |
852d0037 DG |
1684 | |
1685 | return caa_container_of(node, struct ust_app, sock_n); | |
f6a9efaa DG |
1686 | |
1687 | error: | |
1688 | return NULL; | |
099e26bd DG |
1689 | } |
1690 | ||
d0b96690 DG |
1691 | /* |
1692 | * Find an ust_app using the notify sock and return it. RCU read side lock must | |
1693 | * be held before calling this helper function. | |
1694 | */ | |
1695 | static struct ust_app *find_app_by_notify_sock(int sock) | |
1696 | { | |
1697 | struct lttng_ht_node_ulong *node; | |
1698 | struct lttng_ht_iter iter; | |
1699 | ||
1700 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), | |
1701 | &iter); | |
1702 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1703 | if (node == NULL) { | |
1704 | DBG2("UST app find by notify sock %d not found", sock); | |
1705 | goto error; | |
1706 | } | |
1707 | ||
1708 | return caa_container_of(node, struct ust_app, notify_sock_n); | |
1709 | ||
1710 | error: | |
1711 | return NULL; | |
1712 | } | |
1713 | ||
025faf73 DG |
1714 | /* |
1715 | * Lookup for an ust app event based on event name, filter bytecode and the | |
1716 | * event loglevel. | |
1717 | * | |
1718 | * Return an ust_app_event object or NULL on error. | |
1719 | */ | |
18eace3b | 1720 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
2b00d462 | 1721 | const char *name, const struct lttng_bytecode *filter, |
2106efa0 | 1722 | int loglevel_value, |
ebdb334b JR |
1723 | const struct lttng_event_exclusion *exclusion, |
1724 | uint64_t tracer_token) | |
18eace3b DG |
1725 | { |
1726 | struct lttng_ht_iter iter; | |
1727 | struct lttng_ht_node_str *node; | |
1728 | struct ust_app_event *event = NULL; | |
1729 | struct ust_app_ht_key key; | |
18eace3b DG |
1730 | |
1731 | assert(name); | |
1732 | assert(ht); | |
1733 | ||
1734 | /* Setup key for event lookup. */ | |
1735 | key.name = name; | |
1736 | key.filter = filter; | |
2106efa0 | 1737 | key.loglevel_type = loglevel_value; |
39c5a3a7 | 1738 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
51755dc8 | 1739 | key.exclusion = exclusion; |
ebdb334b | 1740 | key.tracer_token = tracer_token; |
18eace3b | 1741 | |
025faf73 DG |
1742 | /* Lookup using the event name as hash and a custom match fct. */ |
1743 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), | |
1744 | ht_match_ust_app_event, &key, &iter.iter); | |
18eace3b DG |
1745 | node = lttng_ht_iter_get_node_str(&iter); |
1746 | if (node == NULL) { | |
1747 | goto end; | |
1748 | } | |
1749 | ||
1750 | event = caa_container_of(node, struct ust_app_event, node); | |
1751 | ||
1752 | end: | |
18eace3b DG |
1753 | return event; |
1754 | } | |
1755 | ||
993578ff JR |
1756 | /* |
1757 | * Look-up an event notifier rule based on its token id. | |
1758 | * | |
1759 | * Must be called with the RCU read lock held. | |
1760 | * Return an ust_app_event_notifier_rule object or NULL on error. | |
1761 | */ | |
1762 | static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule( | |
1763 | struct lttng_ht *ht, uint64_t token) | |
1764 | { | |
1765 | struct lttng_ht_iter iter; | |
1766 | struct lttng_ht_node_u64 *node; | |
1767 | struct ust_app_event_notifier_rule *event_notifier_rule = NULL; | |
1768 | ||
1769 | assert(ht); | |
1770 | ||
1771 | lttng_ht_lookup(ht, &token, &iter); | |
1772 | node = lttng_ht_iter_get_node_u64(&iter); | |
1773 | if (node == NULL) { | |
1774 | DBG2("UST app event notifier rule token not found: token = %" PRIu64, | |
1775 | token); | |
1776 | goto end; | |
1777 | } | |
1778 | ||
1779 | event_notifier_rule = caa_container_of( | |
1780 | node, struct ust_app_event_notifier_rule, node); | |
1781 | end: | |
1782 | return event_notifier_rule; | |
1783 | } | |
1784 | ||
55cc08a6 DG |
1785 | /* |
1786 | * Create the channel context on the tracer. | |
d0b96690 DG |
1787 | * |
1788 | * Called with UST app session lock held. | |
55cc08a6 DG |
1789 | */ |
1790 | static | |
1791 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
1792 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
1793 | { | |
1794 | int ret; | |
1795 | ||
840cb59c | 1796 | health_code_update(); |
86acf0da | 1797 | |
fb45065e | 1798 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1799 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 | 1800 | ua_chan->obj, &ua_ctx->obj); |
fb45065e | 1801 | pthread_mutex_unlock(&app->sock_lock); |
55cc08a6 | 1802 | if (ret < 0) { |
ffe60014 DG |
1803 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1804 | ERR("UST app create channel context failed for app (pid: %d) " | |
1805 | "with ret %d", app->pid, ret); | |
1806 | } else { | |
3757b385 DG |
1807 | /* |
1808 | * This is normal behavior, an application can die during the | |
1809 | * creation process. Don't report an error so the execution can | |
1810 | * continue normally. | |
1811 | */ | |
1812 | ret = 0; | |
88e3c2f5 | 1813 | DBG3("UST app add context failed. Application is dead."); |
ffe60014 | 1814 | } |
55cc08a6 DG |
1815 | goto error; |
1816 | } | |
1817 | ||
1818 | ua_ctx->handle = ua_ctx->obj->handle; | |
1819 | ||
d0b96690 DG |
1820 | DBG2("UST app context handle %d created successfully for channel %s", |
1821 | ua_ctx->handle, ua_chan->name); | |
55cc08a6 DG |
1822 | |
1823 | error: | |
840cb59c | 1824 | health_code_update(); |
55cc08a6 DG |
1825 | return ret; |
1826 | } | |
1827 | ||
53a80697 MD |
1828 | /* |
1829 | * Set the filter on the tracer. | |
1830 | */ | |
a154c7b8 | 1831 | static int set_ust_object_filter(struct ust_app *app, |
2b00d462 | 1832 | const struct lttng_bytecode *bytecode, |
a154c7b8 | 1833 | struct lttng_ust_object_data *ust_object) |
53a80697 MD |
1834 | { |
1835 | int ret; | |
51755dc8 | 1836 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
53a80697 | 1837 | |
840cb59c | 1838 | health_code_update(); |
86acf0da | 1839 | |
f2eafd2d | 1840 | ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode); |
51755dc8 JG |
1841 | if (!ust_bytecode) { |
1842 | ret = -LTTNG_ERR_NOMEM; | |
1843 | goto error; | |
1844 | } | |
fb45065e | 1845 | pthread_mutex_lock(&app->sock_lock); |
51755dc8 | 1846 | ret = ustctl_set_filter(app->sock, ust_bytecode, |
a154c7b8 | 1847 | ust_object); |
fb45065e | 1848 | pthread_mutex_unlock(&app->sock_lock); |
53a80697 | 1849 | if (ret < 0) { |
ffe60014 | 1850 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
f2eafd2d JR |
1851 | ERR("UST app set object filter failed: object = %p of app pid = %d, ret = %d", |
1852 | ust_object, app->pid, ret); | |
ffe60014 | 1853 | } else { |
3757b385 DG |
1854 | /* |
1855 | * This is normal behavior, an application can die during the | |
1856 | * creation process. Don't report an error so the execution can | |
1857 | * continue normally. | |
1858 | */ | |
1859 | ret = 0; | |
a154c7b8 | 1860 | DBG3("Failed to set UST app object filter. Application is dead."); |
ffe60014 | 1861 | } |
53a80697 MD |
1862 | goto error; |
1863 | } | |
1864 | ||
f2eafd2d JR |
1865 | DBG2("UST filter successfully set: object = %p", ust_object); |
1866 | ||
1867 | error: | |
1868 | health_code_update(); | |
1869 | free(ust_bytecode); | |
1870 | return ret; | |
1871 | } | |
1872 | ||
1873 | /* | |
1874 | * Set a capture bytecode for the passed object. | |
11f6ce94 JR |
1875 | * The sequence number enforces the ordering at runtime and on reception of |
1876 | * the captured payloads. | |
f2eafd2d JR |
1877 | */ |
1878 | static int set_ust_capture(struct ust_app *app, | |
1879 | const struct lttng_bytecode *bytecode, | |
11f6ce94 | 1880 | unsigned int capture_seqnum, |
f2eafd2d JR |
1881 | struct lttng_ust_object_data *ust_object) |
1882 | { | |
1883 | int ret; | |
1884 | struct lttng_ust_capture_bytecode *ust_bytecode = NULL; | |
1885 | ||
1886 | health_code_update(); | |
1887 | ||
1888 | ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode); | |
1889 | if (!ust_bytecode) { | |
1890 | ret = -LTTNG_ERR_NOMEM; | |
1891 | goto error; | |
1892 | } | |
1893 | ||
11f6ce94 JR |
1894 | /* |
1895 | * Set the sequence number to ensure the capture of fields is ordered. | |
1896 | */ | |
1897 | ust_bytecode->seqnum = capture_seqnum; | |
1898 | ||
f2eafd2d JR |
1899 | pthread_mutex_lock(&app->sock_lock); |
1900 | ret = ustctl_set_capture(app->sock, ust_bytecode, | |
1901 | ust_object); | |
1902 | pthread_mutex_unlock(&app->sock_lock); | |
1903 | if (ret < 0) { | |
1904 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
1905 | ERR("UST app set object capture failed: object = %p of app pid = %d, ret = %d", | |
1906 | ust_object, app->pid, ret); | |
1907 | } else { | |
1908 | /* | |
1909 | * This is normal behavior, an application can die during the | |
1910 | * creation process. Don't report an error so the execution can | |
1911 | * continue normally. | |
1912 | */ | |
1913 | ret = 0; | |
1914 | DBG3("Failed to set UST app object capture. Application is dead."); | |
1915 | } | |
1916 | ||
1917 | goto error; | |
1918 | } | |
1919 | ||
1920 | DBG2("UST capture successfully set: object = %p", ust_object); | |
53a80697 MD |
1921 | |
1922 | error: | |
840cb59c | 1923 | health_code_update(); |
51755dc8 | 1924 | free(ust_bytecode); |
53a80697 MD |
1925 | return ret; |
1926 | } | |
1927 | ||
51755dc8 JG |
1928 | static |
1929 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( | |
c0901ffa | 1930 | const struct lttng_event_exclusion *exclusion) |
51755dc8 JG |
1931 | { |
1932 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; | |
1933 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + | |
1934 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; | |
1935 | ||
1936 | ust_exclusion = zmalloc(exclusion_alloc_size); | |
1937 | if (!ust_exclusion) { | |
1938 | PERROR("malloc"); | |
1939 | goto end; | |
1940 | } | |
1941 | ||
1942 | assert(sizeof(struct lttng_event_exclusion) == | |
1943 | sizeof(struct lttng_ust_event_exclusion)); | |
1944 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); | |
1945 | end: | |
1946 | return ust_exclusion; | |
1947 | } | |
1948 | ||
7cc9a73c JI |
1949 | /* |
1950 | * Set event exclusions on the tracer. | |
1951 | */ | |
c0901ffa JR |
1952 | static int set_ust_object_exclusions(struct ust_app *app, |
1953 | const struct lttng_event_exclusion *exclusions, | |
1954 | struct lttng_ust_object_data *ust_object) | |
7cc9a73c JI |
1955 | { |
1956 | int ret; | |
c0901ffa | 1957 | struct lttng_ust_event_exclusion *ust_exclusions = NULL; |
7cc9a73c | 1958 | |
c0901ffa | 1959 | assert(exclusions && exclusions->count > 0); |
7cc9a73c | 1960 | |
c0901ffa | 1961 | health_code_update(); |
7cc9a73c | 1962 | |
c0901ffa JR |
1963 | ust_exclusions = create_ust_exclusion_from_exclusion( |
1964 | exclusions); | |
1965 | if (!ust_exclusions) { | |
51755dc8 JG |
1966 | ret = -LTTNG_ERR_NOMEM; |
1967 | goto error; | |
1968 | } | |
fb45065e | 1969 | pthread_mutex_lock(&app->sock_lock); |
c0901ffa | 1970 | ret = ustctl_set_exclusion(app->sock, ust_exclusions, ust_object); |
fb45065e | 1971 | pthread_mutex_unlock(&app->sock_lock); |
7cc9a73c JI |
1972 | if (ret < 0) { |
1973 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
c0901ffa JR |
1974 | ERR("Failed to set UST app exclusions for object %p of app (pid: %d) " |
1975 | "with ret %d", ust_object, app->pid, ret); | |
7cc9a73c JI |
1976 | } else { |
1977 | /* | |
1978 | * This is normal behavior, an application can die during the | |
1979 | * creation process. Don't report an error so the execution can | |
1980 | * continue normally. | |
1981 | */ | |
1982 | ret = 0; | |
c0901ffa | 1983 | DBG3("Failed to set UST app object exclusions. Application is dead."); |
7cc9a73c JI |
1984 | } |
1985 | goto error; | |
1986 | } | |
1987 | ||
c0901ffa | 1988 | DBG2("UST exclusions set successfully for object %p", ust_object); |
7cc9a73c JI |
1989 | |
1990 | error: | |
1991 | health_code_update(); | |
c0901ffa | 1992 | free(ust_exclusions); |
7cc9a73c JI |
1993 | return ret; |
1994 | } | |
1995 | ||
9730260e DG |
1996 | /* |
1997 | * Disable the specified event on to UST tracer for the UST session. | |
1998 | */ | |
e2456d0a JR |
1999 | static int disable_ust_object(struct ust_app *app, |
2000 | struct lttng_ust_object_data *object) | |
9730260e DG |
2001 | { |
2002 | int ret; | |
2003 | ||
840cb59c | 2004 | health_code_update(); |
86acf0da | 2005 | |
fb45065e | 2006 | pthread_mutex_lock(&app->sock_lock); |
e2456d0a | 2007 | ret = ustctl_disable(app->sock, object); |
fb45065e | 2008 | pthread_mutex_unlock(&app->sock_lock); |
9730260e | 2009 | if (ret < 0) { |
ffe60014 | 2010 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
e2456d0a JR |
2011 | ERR("Failed to disable UST app object %p app (pid: %d) with ret %d", |
2012 | object, app->pid, ret); | |
ffe60014 | 2013 | } else { |
3757b385 DG |
2014 | /* |
2015 | * This is normal behavior, an application can die during the | |
2016 | * creation process. Don't report an error so the execution can | |
2017 | * continue normally. | |
2018 | */ | |
2019 | ret = 0; | |
e2456d0a | 2020 | DBG3("Failed to disable UST app object. Application is dead."); |
ffe60014 | 2021 | } |
9730260e DG |
2022 | goto error; |
2023 | } | |
2024 | ||
e2456d0a JR |
2025 | DBG2("UST app object %p disabled successfully for app (pid: %d)", |
2026 | object, app->pid); | |
9730260e DG |
2027 | |
2028 | error: | |
840cb59c | 2029 | health_code_update(); |
9730260e DG |
2030 | return ret; |
2031 | } | |
2032 | ||
78f0bacd DG |
2033 | /* |
2034 | * Disable the specified channel on to UST tracer for the UST session. | |
2035 | */ | |
2036 | static int disable_ust_channel(struct ust_app *app, | |
2037 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
2038 | { | |
2039 | int ret; | |
2040 | ||
840cb59c | 2041 | health_code_update(); |
86acf0da | 2042 | |
fb45065e | 2043 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 2044 | ret = ustctl_disable(app->sock, ua_chan->obj); |
fb45065e | 2045 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 2046 | if (ret < 0) { |
ffe60014 DG |
2047 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
2048 | ERR("UST app channel %s disable failed for app (pid: %d) " | |
2049 | "and session handle %d with ret %d", | |
2050 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
2051 | } else { | |
3757b385 DG |
2052 | /* |
2053 | * This is normal behavior, an application can die during the | |
2054 | * creation process. Don't report an error so the execution can | |
2055 | * continue normally. | |
2056 | */ | |
2057 | ret = 0; | |
ffe60014 DG |
2058 | DBG3("UST app disable channel failed. Application is dead."); |
2059 | } | |
78f0bacd DG |
2060 | goto error; |
2061 | } | |
2062 | ||
78f0bacd | 2063 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
852d0037 | 2064 | ua_chan->name, app->pid); |
78f0bacd DG |
2065 | |
2066 | error: | |
840cb59c | 2067 | health_code_update(); |
78f0bacd DG |
2068 | return ret; |
2069 | } | |
2070 | ||
ebdb334b JR |
2071 | /* |
2072 | * Disable the specified map on to UST tracer for the UST session. | |
2073 | */ | |
2074 | static int disable_ust_map(struct ust_app *app, | |
2075 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) | |
2076 | { | |
2077 | int ret; | |
2078 | ||
2079 | health_code_update(); | |
2080 | ||
2081 | pthread_mutex_lock(&app->sock_lock); | |
2082 | ret = ustctl_disable(app->sock, ua_map->obj); | |
2083 | pthread_mutex_unlock(&app->sock_lock); | |
2084 | if (ret < 0) { | |
2085 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2086 | ERR("UST app map %s disable failed for app (pid: %d) " | |
2087 | "and session handle %d with ret %d", | |
2088 | ua_map->name, app->pid, ua_sess->handle, ret); | |
2089 | } else { | |
2090 | /* | |
2091 | * This is normal behavior, an application can die during the | |
2092 | * creation process. Don't report an error so the execution can | |
2093 | * continue normally. | |
2094 | */ | |
2095 | ret = 0; | |
2096 | DBG3("UST app disable map failed. Application is dead."); | |
2097 | } | |
2098 | goto error; | |
2099 | } | |
2100 | ||
2101 | DBG2("UST app map %s disabled successfully for app (pid: %d)", | |
2102 | ua_map->name, app->pid); | |
2103 | ||
2104 | error: | |
2105 | health_code_update(); | |
2106 | return ret; | |
2107 | } | |
2108 | ||
78f0bacd DG |
2109 | /* |
2110 | * Enable the specified channel on to UST tracer for the UST session. | |
2111 | */ | |
2112 | static int enable_ust_channel(struct ust_app *app, | |
2113 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
2114 | { | |
2115 | int ret; | |
2116 | ||
840cb59c | 2117 | health_code_update(); |
86acf0da | 2118 | |
fb45065e | 2119 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 2120 | ret = ustctl_enable(app->sock, ua_chan->obj); |
fb45065e | 2121 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 2122 | if (ret < 0) { |
ffe60014 DG |
2123 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
2124 | ERR("UST app channel %s enable failed for app (pid: %d) " | |
2125 | "and session handle %d with ret %d", | |
2126 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
2127 | } else { | |
3757b385 DG |
2128 | /* |
2129 | * This is normal behavior, an application can die during the | |
2130 | * creation process. Don't report an error so the execution can | |
2131 | * continue normally. | |
2132 | */ | |
2133 | ret = 0; | |
ffe60014 DG |
2134 | DBG3("UST app enable channel failed. Application is dead."); |
2135 | } | |
78f0bacd DG |
2136 | goto error; |
2137 | } | |
2138 | ||
2139 | ua_chan->enabled = 1; | |
2140 | ||
2141 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", | |
852d0037 | 2142 | ua_chan->name, app->pid); |
78f0bacd DG |
2143 | |
2144 | error: | |
840cb59c | 2145 | health_code_update(); |
78f0bacd DG |
2146 | return ret; |
2147 | } | |
2148 | ||
ebdb334b JR |
2149 | /* |
2150 | * Enable the specified map on to UST tracer for the UST session. | |
2151 | */ | |
2152 | static int enable_ust_map(struct ust_app *app, | |
2153 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) | |
2154 | { | |
2155 | int ret; | |
2156 | ||
2157 | health_code_update(); | |
2158 | ||
2159 | pthread_mutex_lock(&app->sock_lock); | |
2160 | ret = ustctl_enable(app->sock, ua_map->obj); | |
2161 | pthread_mutex_unlock(&app->sock_lock); | |
2162 | if (ret < 0) { | |
2163 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2164 | ERR("UST app map %s enable failed for app (pid: %d) " | |
2165 | "and session handle %d with ret %d", | |
2166 | ua_map->name, app->pid, ua_sess->handle, ret); | |
2167 | } else { | |
2168 | /* | |
2169 | * This is normal behavior, an application can die during the | |
2170 | * creation process. Don't report an error so the execution can | |
2171 | * continue normally. | |
2172 | */ | |
2173 | ret = 0; | |
2174 | DBG3("UST app enable map failed. Application is dead."); | |
2175 | } | |
2176 | goto error; | |
2177 | } | |
2178 | ||
2179 | ua_map->enabled = 1; | |
2180 | ||
2181 | DBG2("UST app map %s enabled successfully for app (pid: %d)", | |
2182 | ua_map->name, app->pid); | |
2183 | ||
2184 | error: | |
2185 | health_code_update(); | |
2186 | return ret; | |
2187 | } | |
2188 | ||
edb67388 DG |
2189 | /* |
2190 | * Enable the specified event on to UST tracer for the UST session. | |
2191 | */ | |
3428a1b7 JR |
2192 | static int enable_ust_object( |
2193 | struct ust_app *app, struct lttng_ust_object_data *ust_object) | |
edb67388 DG |
2194 | { |
2195 | int ret; | |
2196 | ||
840cb59c | 2197 | health_code_update(); |
86acf0da | 2198 | |
fb45065e | 2199 | pthread_mutex_lock(&app->sock_lock); |
3428a1b7 | 2200 | ret = ustctl_enable(app->sock, ust_object); |
fb45065e | 2201 | pthread_mutex_unlock(&app->sock_lock); |
edb67388 | 2202 | if (ret < 0) { |
ffe60014 | 2203 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
3428a1b7 JR |
2204 | ERR("UST app enable failed for object %p app (pid: %d) with ret %d", |
2205 | ust_object, app->pid, ret); | |
ffe60014 | 2206 | } else { |
3757b385 DG |
2207 | /* |
2208 | * This is normal behavior, an application can die during the | |
2209 | * creation process. Don't report an error so the execution can | |
2210 | * continue normally. | |
2211 | */ | |
2212 | ret = 0; | |
3428a1b7 | 2213 | DBG3("Failed to enable UST app object. Application is dead."); |
ffe60014 | 2214 | } |
edb67388 DG |
2215 | goto error; |
2216 | } | |
2217 | ||
3428a1b7 JR |
2218 | DBG2("UST app object %p enabled successfully for app (pid: %d)", |
2219 | ust_object, app->pid); | |
edb67388 DG |
2220 | |
2221 | error: | |
840cb59c | 2222 | health_code_update(); |
edb67388 DG |
2223 | return ret; |
2224 | } | |
2225 | ||
099e26bd | 2226 | /* |
7972aab2 | 2227 | * Send channel and stream buffer to application. |
4f3ab6ee | 2228 | * |
ffe60014 | 2229 | * Return 0 on success. On error, a negative value is returned. |
4f3ab6ee | 2230 | */ |
7972aab2 DG |
2231 | static int send_channel_pid_to_ust(struct ust_app *app, |
2232 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
4f3ab6ee DG |
2233 | { |
2234 | int ret; | |
ffe60014 | 2235 | struct ust_app_stream *stream, *stmp; |
4f3ab6ee DG |
2236 | |
2237 | assert(app); | |
ffe60014 | 2238 | assert(ua_sess); |
4f3ab6ee | 2239 | assert(ua_chan); |
4f3ab6ee | 2240 | |
840cb59c | 2241 | health_code_update(); |
4f3ab6ee | 2242 | |
7972aab2 DG |
2243 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
2244 | app->sock); | |
86acf0da | 2245 | |
ffe60014 DG |
2246 | /* Send channel to the application. */ |
2247 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
2248 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2249 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2250 | goto error; | |
2251 | } else if (ret < 0) { | |
b551a063 DG |
2252 | goto error; |
2253 | } | |
2254 | ||
d88aee68 DG |
2255 | health_code_update(); |
2256 | ||
ffe60014 DG |
2257 | /* Send all streams to application. */ |
2258 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
2259 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); | |
a7169585 MD |
2260 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2261 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2262 | goto error; | |
2263 | } else if (ret < 0) { | |
ffe60014 DG |
2264 | goto error; |
2265 | } | |
2266 | /* We don't need the stream anymore once sent to the tracer. */ | |
2267 | cds_list_del(&stream->list); | |
fb45065e | 2268 | delete_ust_app_stream(-1, stream, app); |
ffe60014 | 2269 | } |
ffe60014 DG |
2270 | /* Flag the channel that it is sent to the application. */ |
2271 | ua_chan->is_sent = 1; | |
ffe60014 | 2272 | |
b551a063 | 2273 | error: |
840cb59c | 2274 | health_code_update(); |
b551a063 DG |
2275 | return ret; |
2276 | } | |
2277 | ||
ebdb334b JR |
2278 | /* |
2279 | * Send map and stream buffer to application. | |
2280 | * | |
2281 | * Return 0 on success. On error, a negative value is returned. | |
2282 | */ | |
2283 | static int send_map_pid_to_ust(struct ust_app *app, | |
2284 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) | |
2285 | { | |
2286 | int ret; | |
2287 | struct ust_app_map_counter *counter, *ctmp; | |
2288 | ||
2289 | assert(app); | |
2290 | assert(ua_sess); | |
2291 | assert(ua_map); | |
2292 | ||
2293 | health_code_update(); | |
2294 | ||
2295 | DBG("UST app sending map %s to UST app sock %d", ua_map->name, | |
2296 | app->sock); | |
2297 | ||
2298 | /* Send map to the application. */ | |
2299 | pthread_mutex_lock(&app->sock_lock); | |
2300 | ret = ustctl_send_counter_data_to_ust(app->sock, | |
2301 | ua_sess->handle, ua_map->obj); | |
2302 | pthread_mutex_unlock(&app->sock_lock); | |
2303 | assert(ret == 0); | |
2304 | ||
2305 | ua_map->handle = ua_map->obj->handle; | |
2306 | ||
2307 | health_code_update(); | |
2308 | ||
2309 | /* Send all streams to application. */ | |
2310 | cds_list_for_each_entry_safe(counter, ctmp, &ua_map->counters.head, list) { | |
2311 | pthread_mutex_lock(&app->sock_lock); | |
2312 | // Do send the per cpu counter here | |
2313 | ret = ustctl_send_counter_cpu_data_to_ust(app->sock, | |
2314 | ua_map->obj, counter->obj); | |
2315 | pthread_mutex_unlock(&app->sock_lock); | |
2316 | assert(ret == 0); | |
2317 | ||
2318 | /* We don't need the stream anymore once sent to the tracer. */ | |
2319 | cds_list_del(&counter->list); | |
2320 | delete_ust_app_map_counter(-1, counter, app); | |
2321 | } | |
2322 | /* Flag the map that it is sent to the application. */ | |
2323 | ua_map->is_sent = 1; | |
2324 | ||
2325 | health_code_update(); | |
2326 | return ret; | |
2327 | } | |
2328 | ||
91d76f53 | 2329 | /* |
5b4a0ec0 | 2330 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
2331 | * |
2332 | * Should be called with session mutex held. | |
91d76f53 | 2333 | */ |
edb67388 | 2334 | static |
ebdb334b | 2335 | int create_ust_channel_event(struct ust_app *app, struct ust_app_session *ua_sess, |
edb67388 | 2336 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) |
91d76f53 | 2337 | { |
5b4a0ec0 | 2338 | int ret = 0; |
284d8f55 | 2339 | |
840cb59c | 2340 | health_code_update(); |
86acf0da | 2341 | |
5b4a0ec0 | 2342 | /* Create UST event on tracer */ |
fb45065e | 2343 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 2344 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 | 2345 | &ua_event->obj); |
fb45065e | 2346 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 | 2347 | if (ret < 0) { |
ffe60014 | 2348 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
88e3c2f5 | 2349 | abort(); |
ffe60014 DG |
2350 | ERR("Error ustctl create event %s for app pid: %d with ret %d", |
2351 | ua_event->attr.name, app->pid, ret); | |
2352 | } else { | |
3757b385 DG |
2353 | /* |
2354 | * This is normal behavior, an application can die during the | |
2355 | * creation process. Don't report an error so the execution can | |
2356 | * continue normally. | |
2357 | */ | |
2358 | ret = 0; | |
ffe60014 DG |
2359 | DBG3("UST app create event failed. Application is dead."); |
2360 | } | |
5b4a0ec0 | 2361 | goto error; |
91d76f53 | 2362 | } |
f6a9efaa | 2363 | |
5b4a0ec0 | 2364 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 2365 | |
3428a1b7 JR |
2366 | DBG2("UST app event %s created successfully for pid:%d object: %p", |
2367 | ua_event->attr.name, app->pid, ua_event->obj); | |
f6a9efaa | 2368 | |
840cb59c | 2369 | health_code_update(); |
86acf0da | 2370 | |
025faf73 DG |
2371 | /* Set filter if one is present. */ |
2372 | if (ua_event->filter) { | |
a154c7b8 | 2373 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); |
025faf73 DG |
2374 | if (ret < 0) { |
2375 | goto error; | |
2376 | } | |
2377 | } | |
2378 | ||
7cc9a73c JI |
2379 | /* Set exclusions for the event */ |
2380 | if (ua_event->exclusion) { | |
c0901ffa | 2381 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); |
7cc9a73c JI |
2382 | if (ret < 0) { |
2383 | goto error; | |
2384 | } | |
2385 | } | |
2386 | ||
8535a6d9 | 2387 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
2388 | if (ua_event->enabled) { |
2389 | /* | |
2390 | * We now need to explicitly enable the event, since it | |
2391 | * is now disabled at creation. | |
2392 | */ | |
3428a1b7 | 2393 | ret = enable_ust_object(app, ua_event->obj); |
40113787 MD |
2394 | if (ret < 0) { |
2395 | /* | |
2396 | * If we hit an EPERM, something is wrong with our enable call. If | |
2397 | * we get an EEXIST, there is a problem on the tracer side since we | |
2398 | * just created it. | |
2399 | */ | |
2400 | switch (ret) { | |
2401 | case -LTTNG_UST_ERR_PERM: | |
2402 | /* Code flow problem */ | |
2403 | assert(0); | |
2404 | case -LTTNG_UST_ERR_EXIST: | |
2405 | /* It's OK for our use case. */ | |
2406 | ret = 0; | |
2407 | break; | |
2408 | default: | |
2409 | break; | |
2410 | } | |
2411 | goto error; | |
2412 | } | |
8535a6d9 DG |
2413 | } |
2414 | ||
5b4a0ec0 | 2415 | error: |
840cb59c | 2416 | health_code_update(); |
5b4a0ec0 | 2417 | return ret; |
91d76f53 | 2418 | } |
48842b30 | 2419 | |
ebdb334b JR |
2420 | static |
2421 | void add_key_token(struct lttng_ust_key_token *ust_key_token, | |
2422 | const struct lttng_map_key_token *key_token) | |
993578ff | 2423 | { |
ebdb334b JR |
2424 | switch (key_token->type) { |
2425 | case LTTNG_MAP_KEY_TOKEN_TYPE_STRING: | |
2426 | { | |
2427 | const struct lttng_map_key_token_string *str_token; | |
2428 | str_token = (typeof(str_token)) key_token; | |
2429 | ||
2430 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_STRING; | |
2431 | strncpy(ust_key_token->arg.string, str_token->string, | |
2432 | LTTNG_UST_KEY_TOKEN_STRING_LEN_MAX); | |
2433 | ||
2434 | break; | |
2435 | } | |
2436 | case LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE: | |
2437 | { | |
2438 | const struct lttng_map_key_token_variable *var_token; | |
2439 | var_token = (typeof(var_token)) key_token; | |
2440 | switch (var_token->type) { | |
2441 | case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME: | |
2442 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_EVENT_NAME; | |
2443 | break; | |
2444 | case LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_PROVIDER_NAME: | |
2445 | ust_key_token->type = LTTNG_UST_KEY_TOKEN_PROVIDER_NAME; | |
2446 | break; | |
2447 | default: | |
2448 | abort(); | |
2449 | } | |
2450 | ||
2451 | break; | |
2452 | } | |
2453 | default: | |
2454 | abort(); | |
2455 | } | |
2456 | } | |
2457 | ||
2458 | /* | |
2459 | * Create the specified event onto the UST tracer for a UST session. | |
2460 | * | |
2461 | * Should be called with session mutex held. | |
2462 | */ | |
2463 | static | |
2464 | int create_ust_map_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
2465 | struct ust_app_map *ua_map, const struct lttng_map_key *key, | |
2466 | struct ust_app_event *ua_event) | |
2467 | { | |
2468 | int ret = 0; | |
2469 | unsigned int i, key_token_count; | |
2470 | enum lttng_map_key_status status; | |
2471 | struct lttng_ust_counter_event counter_event = {0}; | |
2472 | ||
2473 | health_code_update(); | |
2474 | ||
2475 | memcpy(&counter_event.event, &ua_event->attr, sizeof(struct lttng_ust_event)); | |
2476 | ||
2477 | status = lttng_map_key_get_token_count(key, &key_token_count); | |
2478 | if (status != LTTNG_MAP_KEY_STATUS_OK) { | |
2479 | ret = LTTNG_ERR_UNK; | |
2480 | goto error; | |
2481 | } | |
2482 | ||
2483 | assert(key_token_count > 0); | |
2484 | ||
2485 | counter_event.key.nr_dimensions = 1; | |
2486 | counter_event.key.key_dimensions[0].nr_key_tokens = key_token_count; | |
2487 | ||
2488 | if (key_token_count > LTTNG_UST_NR_KEY_TOKEN) { | |
2489 | ERR("Too many key tokens for UST tracer: token count = %u token count max =%u", | |
2490 | key_token_count, LTTNG_UST_NR_KEY_TOKEN); | |
2491 | ret = LTTNG_ERR_INVALID; | |
2492 | goto error; | |
2493 | } | |
2494 | ||
2495 | for (i = 0; i < key_token_count; i++) { | |
2496 | const struct lttng_map_key_token *token = | |
2497 | lttng_map_key_get_token_at_index(key, i); | |
2498 | ||
2499 | add_key_token(&counter_event.key.key_dimensions[0].key_tokens[i], | |
2500 | token); | |
2501 | } | |
2502 | ||
2503 | /* Create UST event on tracer */ | |
2504 | pthread_mutex_lock(&app->sock_lock); | |
2505 | ret = ustctl_counter_create_event(app->sock, &counter_event, ua_map->obj, | |
2506 | &ua_event->obj); | |
2507 | pthread_mutex_unlock(&app->sock_lock); | |
2508 | if (ret < 0) { | |
2509 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2510 | abort(); | |
2511 | ERR("Error ustctl counter create event %s for app pid: %d with ret %d", | |
2512 | ua_event->attr.name, app->pid, ret); | |
2513 | } else { | |
2514 | /* | |
2515 | * This is normal behavior, an application can die during the | |
2516 | * creation process. Don't report an error so the execution can | |
2517 | * continue normally. | |
2518 | */ | |
2519 | ret = 0; | |
2520 | DBG3("UST app counter create event failed. Application is dead."); | |
2521 | } | |
2522 | goto error; | |
2523 | } | |
2524 | ||
2525 | ua_event->handle = ua_event->obj->handle; | |
2526 | ||
2527 | DBG2("UST app map event %s created successfully for pid:%d object: %p", | |
2528 | ua_event->attr.name, app->pid, ua_event->obj); | |
2529 | ||
2530 | health_code_update(); | |
2531 | ||
2532 | /* Set filter if one is present. */ | |
2533 | if (ua_event->filter) { | |
2534 | ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj); | |
2535 | if (ret < 0) { | |
2536 | goto error; | |
2537 | } | |
2538 | } | |
2539 | ||
2540 | /* Set exclusions for the event */ | |
2541 | if (ua_event->exclusion) { | |
2542 | ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj); | |
2543 | if (ret < 0) { | |
2544 | goto error; | |
2545 | } | |
2546 | } | |
2547 | ||
2548 | /* If event not enabled, disable it on the tracer */ | |
2549 | if (ua_event->enabled) { | |
2550 | /* | |
2551 | * We now need to explicitly enable the event, since it | |
2552 | * is now disabled at creation. | |
2553 | */ | |
2554 | ret = enable_ust_object(app, ua_event->obj); | |
2555 | if (ret < 0) { | |
2556 | /* | |
2557 | * If we hit an EPERM, something is wrong with our enable call. If | |
2558 | * we get an EEXIST, there is a problem on the tracer side since we | |
2559 | * just created it. | |
2560 | */ | |
2561 | switch (ret) { | |
2562 | case -LTTNG_UST_ERR_PERM: | |
2563 | /* Code flow problem */ | |
2564 | assert(0); | |
2565 | case -LTTNG_UST_ERR_EXIST: | |
2566 | /* It's OK for our use case. */ | |
2567 | ret = 0; | |
2568 | break; | |
2569 | default: | |
2570 | break; | |
2571 | } | |
2572 | goto error; | |
2573 | } | |
2574 | } | |
2575 | ||
2576 | error: | |
2577 | health_code_update(); | |
2578 | return ret; | |
2579 | } | |
2580 | ||
2581 | static int init_ust_event_from_event_rule( | |
2582 | const struct lttng_event_rule *rule, | |
2583 | struct lttng_ust_event *event) | |
2584 | { | |
2585 | enum lttng_event_rule_status status; | |
2586 | enum lttng_ust_loglevel_type ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; | |
993578ff JR |
2587 | int loglevel = -1, ret = 0; |
2588 | const char *pattern; | |
2589 | ||
2590 | /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */ | |
2591 | assert(lttng_event_rule_get_type(rule) == | |
2592 | LTTNG_EVENT_RULE_TYPE_TRACEPOINT); | |
2593 | ||
44760c20 JR |
2594 | if (lttng_event_rule_targets_agent_domain(rule)) { |
2595 | /* | |
2596 | * Special event for agents | |
2597 | * The actual meat of the event is in the filter that will be | |
2598 | * attached later on. | |
2599 | * Set the default values for the agent event. | |
2600 | */ | |
2601 | pattern = event_get_default_agent_ust_name( | |
2602 | lttng_event_rule_get_domain_type(rule)); | |
2603 | loglevel = 0; | |
2604 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; | |
2605 | } else { | |
ebdb334b | 2606 | const struct lttng_log_level_rule *log_level_rule; |
993578ff | 2607 | |
ebdb334b | 2608 | status = lttng_event_rule_tracepoint_get_pattern(rule, &pattern); |
44760c20 JR |
2609 | if (status != LTTNG_EVENT_RULE_STATUS_OK) { |
2610 | /* At this point, this is a fatal error. */ | |
2611 | abort(); | |
2612 | } | |
993578ff | 2613 | |
ebdb334b JR |
2614 | status = lttng_event_rule_tracepoint_get_log_level_rule( |
2615 | rule, &log_level_rule); | |
2616 | if (status == LTTNG_EVENT_RULE_STATUS_UNSET) { | |
44760c20 | 2617 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_ALL; |
ebdb334b JR |
2618 | } else if (status == LTTNG_EVENT_RULE_STATUS_OK) { |
2619 | enum lttng_log_level_rule_status llr_status; | |
2620 | ||
2621 | switch (lttng_log_level_rule_get_type(log_level_rule)) { | |
2622 | case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY: | |
2623 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_SINGLE; | |
2624 | llr_status = lttng_log_level_rule_exactly_get_level(log_level_rule, &loglevel); | |
2625 | break; | |
2626 | case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS: | |
2627 | ust_loglevel_type = LTTNG_UST_LOGLEVEL_RANGE; | |
2628 | llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(log_level_rule, &loglevel); | |
2629 | break; | |
2630 | default: | |
2631 | abort(); | |
2632 | } | |
993578ff | 2633 | |
ebdb334b JR |
2634 | assert(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK); |
2635 | } else { | |
2636 | /* At this point this is a fatal error */ | |
2637 | assert(0); | |
44760c20 | 2638 | } |
993578ff JR |
2639 | } |
2640 | ||
ebdb334b JR |
2641 | event->instrumentation = LTTNG_UST_TRACEPOINT; |
2642 | ret = lttng_strncpy(event->name, pattern, | |
44760c20 | 2643 | LTTNG_UST_SYM_NAME_LEN - 1); |
993578ff JR |
2644 | if (ret) { |
2645 | ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", | |
2646 | pattern); | |
2647 | goto end; | |
2648 | } | |
2649 | ||
ebdb334b JR |
2650 | event->loglevel_type = ust_loglevel_type; |
2651 | event->loglevel = loglevel; | |
993578ff JR |
2652 | end: |
2653 | return ret; | |
2654 | } | |
2655 | ||
2656 | /* | |
2657 | * Create the specified event notifier against the user space tracer of a | |
2658 | * given application. | |
2659 | */ | |
2660 | static int create_ust_event_notifier(struct ust_app *app, | |
2661 | struct ust_app_event_notifier_rule *ua_event_notifier_rule) | |
2662 | { | |
2663 | int ret = 0; | |
ebdb334b | 2664 | struct lttng_ust_event_notifier event_notifier = {0}; |
267d66aa | 2665 | const struct lttng_condition *condition = NULL; |
267d66aa | 2666 | const struct lttng_event_rule *event_rule = NULL; |
f83be61d JR |
2667 | unsigned int capture_bytecode_count = 0, i; |
2668 | enum lttng_condition_status cond_status; | |
993578ff JR |
2669 | |
2670 | health_code_update(); | |
2671 | assert(app->event_notifier_group.object); | |
2672 | ||
267d66aa JR |
2673 | condition = lttng_trigger_get_const_condition( |
2674 | ua_event_notifier_rule->trigger); | |
2675 | assert(condition); | |
ebdb334b | 2676 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); |
993578ff | 2677 | |
ebdb334b | 2678 | lttng_condition_on_event_get_rule(condition, &event_rule); |
267d66aa JR |
2679 | assert(event_rule); |
2680 | assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT); | |
2681 | ||
ebdb334b | 2682 | init_ust_event_from_event_rule(event_rule, &event_notifier.event); |
993578ff | 2683 | event_notifier.event.token = ua_event_notifier_rule->token; |
ebdb334b | 2684 | event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index; |
993578ff JR |
2685 | |
2686 | /* Create UST event notifier against the tracer. */ | |
2687 | pthread_mutex_lock(&app->sock_lock); | |
2688 | ret = ustctl_create_event_notifier(app->sock, &event_notifier, | |
2689 | app->event_notifier_group.object, | |
2690 | &ua_event_notifier_rule->obj); | |
2691 | pthread_mutex_unlock(&app->sock_lock); | |
2692 | if (ret < 0) { | |
2693 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2694 | ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d", | |
2695 | event_notifier.event.name, app->name, | |
2696 | app->ppid, ret); | |
2697 | } else { | |
2698 | /* | |
2699 | * This is normal behavior, an application can die | |
2700 | * during the creation process. Don't report an error so | |
2701 | * the execution can continue normally. | |
2702 | */ | |
2703 | ret = 0; | |
2704 | DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)", | |
2705 | app->name, app->ppid); | |
2706 | } | |
2707 | ||
2708 | goto error; | |
2709 | } | |
2710 | ||
2711 | ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle; | |
2712 | ||
2713 | DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p", | |
2714 | event_notifier.event.name, app->name, app->ppid, | |
2715 | ua_event_notifier_rule->obj); | |
2716 | ||
2717 | health_code_update(); | |
2718 | ||
2719 | /* Set filter if one is present. */ | |
2720 | if (ua_event_notifier_rule->filter) { | |
2721 | ret = set_ust_object_filter(app, ua_event_notifier_rule->filter, | |
2722 | ua_event_notifier_rule->obj); | |
2723 | if (ret < 0) { | |
2724 | goto error; | |
2725 | } | |
2726 | } | |
2727 | ||
2728 | /* Set exclusions for the event. */ | |
2729 | if (ua_event_notifier_rule->exclusion) { | |
2730 | ret = set_ust_object_exclusions(app, | |
2731 | ua_event_notifier_rule->exclusion, | |
2732 | ua_event_notifier_rule->obj); | |
2733 | if (ret < 0) { | |
2734 | goto error; | |
2735 | } | |
2736 | } | |
f83be61d JR |
2737 | |
2738 | /* Set the capture bytecodes. */ | |
ebdb334b | 2739 | cond_status = lttng_condition_on_event_get_capture_descriptor_count( |
f83be61d JR |
2740 | condition, &capture_bytecode_count); |
2741 | assert(cond_status == LTTNG_CONDITION_STATUS_OK); | |
2742 | ||
2743 | for (i = 0; i < capture_bytecode_count; i++) { | |
2744 | const struct lttng_bytecode *capture_bytecode = | |
ebdb334b | 2745 | lttng_condition_on_event_get_capture_bytecode_at_index( |
f83be61d JR |
2746 | condition, i); |
2747 | ||
11f6ce94 | 2748 | ret = set_ust_capture(app, capture_bytecode, i, |
f83be61d JR |
2749 | ua_event_notifier_rule->obj); |
2750 | if (ret < 0) { | |
2751 | goto error; | |
2752 | } | |
2753 | } | |
993578ff JR |
2754 | |
2755 | /* | |
2756 | * We now need to explicitly enable the event, since it | |
2757 | * is disabled at creation. | |
2758 | */ | |
2759 | ret = enable_ust_object(app, ua_event_notifier_rule->obj); | |
2760 | if (ret < 0) { | |
2761 | /* | |
2762 | * If we hit an EPERM, something is wrong with our enable call. | |
2763 | * If we get an EEXIST, there is a problem on the tracer side | |
2764 | * since we just created it. | |
2765 | */ | |
2766 | switch (ret) { | |
2767 | case -LTTNG_UST_ERR_PERM: | |
2768 | /* Code flow problem. */ | |
2769 | abort(); | |
2770 | case -LTTNG_UST_ERR_EXIST: | |
2771 | /* It's OK for our use case. */ | |
2772 | ret = 0; | |
2773 | break; | |
2774 | default: | |
2775 | break; | |
2776 | } | |
2777 | ||
2778 | goto error; | |
2779 | } | |
2780 | ||
2781 | ua_event_notifier_rule->enabled = true; | |
2782 | ||
2783 | error: | |
2784 | health_code_update(); | |
2785 | return ret; | |
2786 | } | |
2787 | ||
5b4a0ec0 DG |
2788 | /* |
2789 | * Copy data between an UST app event and a LTT event. | |
2790 | */ | |
421cb601 | 2791 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
2792 | struct ltt_ust_event *uevent) |
2793 | { | |
b4ffad32 JI |
2794 | size_t exclusion_alloc_size; |
2795 | ||
48842b30 DG |
2796 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
2797 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
2798 | ||
fc34caaa DG |
2799 | ua_event->enabled = uevent->enabled; |
2800 | ||
5b4a0ec0 DG |
2801 | /* Copy event attributes */ |
2802 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
2803 | ||
53a80697 MD |
2804 | /* Copy filter bytecode */ |
2805 | if (uevent->filter) { | |
2b00d462 | 2806 | ua_event->filter = lttng_bytecode_copy(uevent->filter); |
025faf73 | 2807 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 2808 | } |
b4ffad32 JI |
2809 | |
2810 | /* Copy exclusion data */ | |
2811 | if (uevent->exclusion) { | |
51755dc8 | 2812 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
b4ffad32 JI |
2813 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
2814 | ua_event->exclusion = zmalloc(exclusion_alloc_size); | |
5f8df26c JI |
2815 | if (ua_event->exclusion == NULL) { |
2816 | PERROR("malloc"); | |
2817 | } else { | |
2818 | memcpy(ua_event->exclusion, uevent->exclusion, | |
2819 | exclusion_alloc_size); | |
b4ffad32 JI |
2820 | } |
2821 | } | |
48842b30 DG |
2822 | } |
2823 | ||
5b4a0ec0 DG |
2824 | /* |
2825 | * Copy data between an UST app channel and a LTT channel. | |
2826 | */ | |
421cb601 | 2827 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
2828 | struct ltt_ust_channel *uchan) |
2829 | { | |
fc34caaa | 2830 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
2831 | |
2832 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
2833 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 2834 | |
1624d5b7 JD |
2835 | ua_chan->tracefile_size = uchan->tracefile_size; |
2836 | ua_chan->tracefile_count = uchan->tracefile_count; | |
2837 | ||
ffe60014 DG |
2838 | /* Copy event attributes since the layout is different. */ |
2839 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
2840 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
2841 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
2842 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
2843 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
e9404c27 | 2844 | ua_chan->monitor_timer_interval = uchan->monitor_timer_interval; |
ffe60014 | 2845 | ua_chan->attr.output = uchan->attr.output; |
491d1539 MD |
2846 | ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout; |
2847 | ||
ffe60014 DG |
2848 | /* |
2849 | * Note that the attribute channel type is not set since the channel on the | |
2850 | * tracing registry side does not have this information. | |
2851 | */ | |
48842b30 | 2852 | |
fc34caaa | 2853 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 2854 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 2855 | |
fc34caaa | 2856 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
2857 | } |
2858 | ||
5b4a0ec0 DG |
2859 | /* |
2860 | * Copy data between a UST app session and a regular LTT session. | |
2861 | */ | |
421cb601 | 2862 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 2863 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2864 | { |
477d7741 MD |
2865 | struct tm *timeinfo; |
2866 | char datetime[16]; | |
2867 | int ret; | |
d7ba1388 | 2868 | char tmp_shm_path[PATH_MAX]; |
477d7741 | 2869 | |
940c4592 | 2870 | timeinfo = localtime(&app->registration_time); |
477d7741 | 2871 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
48842b30 | 2872 | |
421cb601 | 2873 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 2874 | |
7972aab2 DG |
2875 | ua_sess->tracing_id = usess->id; |
2876 | ua_sess->id = get_next_session_id(); | |
ff588497 JR |
2877 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid); |
2878 | LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid); | |
2879 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid); | |
2880 | LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid); | |
7972aab2 DG |
2881 | ua_sess->buffer_type = usess->buffer_type; |
2882 | ua_sess->bits_per_long = app->bits_per_long; | |
6addfa37 | 2883 | |
7972aab2 | 2884 | /* There is only one consumer object per session possible. */ |
6addfa37 | 2885 | consumer_output_get(usess->consumer); |
7972aab2 | 2886 | ua_sess->consumer = usess->consumer; |
6addfa37 | 2887 | |
2bba9e53 | 2888 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 2889 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
2890 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
2891 | &usess->metadata_attr); | |
7972aab2 DG |
2892 | |
2893 | switch (ua_sess->buffer_type) { | |
2894 | case LTTNG_BUFFER_PER_PID: | |
2895 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 2896 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
2897 | datetime); |
2898 | break; | |
2899 | case LTTNG_BUFFER_PER_UID: | |
2900 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
470cc211 | 2901 | DEFAULT_UST_TRACE_UID_PATH, |
ff588497 | 2902 | lttng_credentials_get_uid(&ua_sess->real_credentials), |
470cc211 | 2903 | app->bits_per_long); |
7972aab2 DG |
2904 | break; |
2905 | default: | |
2906 | assert(0); | |
2907 | goto error; | |
2908 | } | |
477d7741 MD |
2909 | if (ret < 0) { |
2910 | PERROR("asprintf UST shadow copy session"); | |
477d7741 | 2911 | assert(0); |
7972aab2 | 2912 | goto error; |
477d7741 MD |
2913 | } |
2914 | ||
3d071855 MD |
2915 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
2916 | sizeof(ua_sess->root_shm_path)); | |
2917 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
2918 | strncpy(ua_sess->shm_path, usess->shm_path, |
2919 | sizeof(ua_sess->shm_path)); | |
2920 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2921 | if (ua_sess->shm_path[0]) { | |
2922 | switch (ua_sess->buffer_type) { | |
2923 | case LTTNG_BUFFER_PER_PID: | |
2924 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2925 | "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", |
d7ba1388 MD |
2926 | app->name, app->pid, datetime); |
2927 | break; | |
2928 | case LTTNG_BUFFER_PER_UID: | |
2929 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
5da88b0f | 2930 | "/" DEFAULT_UST_TRACE_UID_PATH, |
d7ba1388 MD |
2931 | app->uid, app->bits_per_long); |
2932 | break; | |
2933 | default: | |
2934 | assert(0); | |
2935 | goto error; | |
2936 | } | |
2937 | if (ret < 0) { | |
2938 | PERROR("sprintf UST shadow copy session"); | |
2939 | assert(0); | |
2940 | goto error; | |
2941 | } | |
2942 | strncat(ua_sess->shm_path, tmp_shm_path, | |
2943 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
2944 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
2945 | } | |
6addfa37 | 2946 | return; |
7972aab2 DG |
2947 | |
2948 | error: | |
6addfa37 | 2949 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
2950 | } |
2951 | ||
78f0bacd DG |
2952 | /* |
2953 | * Lookup sesison wrapper. | |
2954 | */ | |
84cd17c6 | 2955 | static |
fb9a95c4 | 2956 | void __lookup_session_by_app(const struct ltt_ust_session *usess, |
bec39940 | 2957 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
2958 | { |
2959 | /* Get right UST app session from app */ | |
d9bf3ca4 | 2960 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
2961 | } |
2962 | ||
421cb601 DG |
2963 | /* |
2964 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 2965 | * id. |
421cb601 | 2966 | */ |
48842b30 | 2967 | static struct ust_app_session *lookup_session_by_app( |
fb9a95c4 | 2968 | const struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 2969 | { |
bec39940 | 2970 | struct lttng_ht_iter iter; |
d9bf3ca4 | 2971 | struct lttng_ht_node_u64 *node; |
48842b30 | 2972 | |
84cd17c6 | 2973 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 2974 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
2975 | if (node == NULL) { |
2976 | goto error; | |
2977 | } | |
2978 | ||
2979 | return caa_container_of(node, struct ust_app_session, node); | |
2980 | ||
2981 | error: | |
2982 | return NULL; | |
2983 | } | |
2984 | ||
7972aab2 DG |
2985 | /* |
2986 | * Setup buffer registry per PID for the given session and application. If none | |
2987 | * is found, a new one is created, added to the global registry and | |
2988 | * initialized. If regp is valid, it's set with the newly created object. | |
2989 | * | |
2990 | * Return 0 on success or else a negative value. | |
2991 | */ | |
2992 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
2993 | struct ust_app *app, struct buffer_reg_pid **regp) | |
2994 | { | |
2995 | int ret = 0; | |
2996 | struct buffer_reg_pid *reg_pid; | |
2997 | ||
2998 | assert(ua_sess); | |
2999 | assert(app); | |
3000 | ||
3001 | rcu_read_lock(); | |
3002 | ||
3003 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
3004 | if (!reg_pid) { | |
3005 | /* | |
3006 | * This is the create channel path meaning that if there is NO | |
3007 | * registry available, we have to create one for this session. | |
3008 | */ | |
d7ba1388 | 3009 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 3010 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
3011 | if (ret < 0) { |
3012 | goto error; | |
3013 | } | |
7972aab2 DG |
3014 | } else { |
3015 | goto end; | |
3016 | } | |
3017 | ||
3018 | /* Initialize registry. */ | |
3019 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, | |
3020 | app->bits_per_long, app->uint8_t_alignment, | |
3021 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf | 3022 | app->uint64_t_alignment, app->long_alignment, |
470cc211 JG |
3023 | app->byte_order, app->version.major, app->version.minor, |
3024 | reg_pid->root_shm_path, reg_pid->shm_path, | |
ff588497 JR |
3025 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
3026 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
3027 | ua_sess->tracing_id, | |
8de88061 | 3028 | app->uid); |
7972aab2 | 3029 | if (ret < 0) { |
286c991a MD |
3030 | /* |
3031 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
3032 | * destroy the buffer registry, because it is always expected | |
3033 | * that if the buffer registry can be found, its ust registry is | |
3034 | * non-NULL. | |
3035 | */ | |
3036 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
3037 | goto error; |
3038 | } | |
3039 | ||
286c991a MD |
3040 | buffer_reg_pid_add(reg_pid); |
3041 | ||
7972aab2 DG |
3042 | DBG3("UST app buffer registry per PID created successfully"); |
3043 | ||
3044 | end: | |
3045 | if (regp) { | |
3046 | *regp = reg_pid; | |
3047 | } | |
3048 | error: | |
3049 | rcu_read_unlock(); | |
3050 | return ret; | |
3051 | } | |
3052 | ||
3053 | /* | |
3054 | * Setup buffer registry per UID for the given session and application. If none | |
3055 | * is found, a new one is created, added to the global registry and | |
3056 | * initialized. If regp is valid, it's set with the newly created object. | |
3057 | * | |
3058 | * Return 0 on success or else a negative value. | |
3059 | */ | |
3060 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 3061 | struct ust_app_session *ua_sess, |
7972aab2 DG |
3062 | struct ust_app *app, struct buffer_reg_uid **regp) |
3063 | { | |
3064 | int ret = 0; | |
3065 | struct buffer_reg_uid *reg_uid; | |
3066 | ||
3067 | assert(usess); | |
3068 | assert(app); | |
3069 | ||
3070 | rcu_read_lock(); | |
3071 | ||
3072 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
3073 | if (!reg_uid) { | |
3074 | /* | |
3075 | * This is the create channel path meaning that if there is NO | |
3076 | * registry available, we have to create one for this session. | |
3077 | */ | |
3078 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, | |
3d071855 MD |
3079 | LTTNG_DOMAIN_UST, ®_uid, |
3080 | ua_sess->root_shm_path, ua_sess->shm_path); | |
7972aab2 DG |
3081 | if (ret < 0) { |
3082 | goto error; | |
3083 | } | |
7972aab2 DG |
3084 | } else { |
3085 | goto end; | |
3086 | } | |
3087 | ||
3088 | /* Initialize registry. */ | |
af6142cf | 3089 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
7972aab2 DG |
3090 | app->bits_per_long, app->uint8_t_alignment, |
3091 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
3092 | app->uint64_t_alignment, app->long_alignment, |
3093 | app->byte_order, app->version.major, | |
3d071855 | 3094 | app->version.minor, reg_uid->root_shm_path, |
8de88061 JR |
3095 | reg_uid->shm_path, usess->uid, usess->gid, |
3096 | ua_sess->tracing_id, app->uid); | |
7972aab2 | 3097 | if (ret < 0) { |
286c991a MD |
3098 | /* |
3099 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
3100 | * destroy the buffer registry, because it is always expected | |
3101 | * that if the buffer registry can be found, its ust registry is | |
3102 | * non-NULL. | |
3103 | */ | |
3104 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
3105 | goto error; |
3106 | } | |
3107 | /* Add node to teardown list of the session. */ | |
3108 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
3109 | ||
286c991a | 3110 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 3111 | |
286c991a | 3112 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
3113 | end: |
3114 | if (regp) { | |
3115 | *regp = reg_uid; | |
3116 | } | |
3117 | error: | |
3118 | rcu_read_unlock(); | |
3119 | return ret; | |
3120 | } | |
3121 | ||
421cb601 | 3122 | /* |
3d8ca23b | 3123 | * Create a session on the tracer side for the given app. |
421cb601 | 3124 | * |
3d8ca23b DG |
3125 | * On success, ua_sess_ptr is populated with the session pointer or else left |
3126 | * untouched. If the session was created, is_created is set to 1. On error, | |
3127 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
3128 | * be NULL. | |
3129 | * | |
3130 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
3131 | * -ENOTCONN which is the default code if the ustctl_create_session fails. | |
421cb601 | 3132 | */ |
03f91eaa | 3133 | static int find_or_create_ust_app_session(struct ltt_ust_session *usess, |
3d8ca23b DG |
3134 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, |
3135 | int *is_created) | |
421cb601 | 3136 | { |
3d8ca23b | 3137 | int ret, created = 0; |
421cb601 DG |
3138 | struct ust_app_session *ua_sess; |
3139 | ||
3d8ca23b DG |
3140 | assert(usess); |
3141 | assert(app); | |
3142 | assert(ua_sess_ptr); | |
3143 | ||
840cb59c | 3144 | health_code_update(); |
86acf0da | 3145 | |
421cb601 DG |
3146 | ua_sess = lookup_session_by_app(usess, app); |
3147 | if (ua_sess == NULL) { | |
d9bf3ca4 | 3148 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 3149 | app->pid, usess->id); |
40bbd087 | 3150 | ua_sess = alloc_ust_app_session(); |
421cb601 DG |
3151 | if (ua_sess == NULL) { |
3152 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
3153 | ret = -ENOMEM; |
3154 | goto error; | |
421cb601 | 3155 | } |
477d7741 | 3156 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 3157 | created = 1; |
421cb601 DG |
3158 | } |
3159 | ||
7972aab2 DG |
3160 | switch (usess->buffer_type) { |
3161 | case LTTNG_BUFFER_PER_PID: | |
3162 | /* Init local registry. */ | |
3163 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 3164 | if (ret < 0) { |
e64207cf | 3165 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
3166 | goto error; |
3167 | } | |
3168 | break; | |
3169 | case LTTNG_BUFFER_PER_UID: | |
3170 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 3171 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 3172 | if (ret < 0) { |
e64207cf | 3173 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
3174 | goto error; |
3175 | } | |
3176 | break; | |
3177 | default: | |
3178 | assert(0); | |
3179 | ret = -EINVAL; | |
3180 | goto error; | |
3181 | } | |
3182 | ||
3183 | health_code_update(); | |
3184 | ||
3185 | if (ua_sess->handle == -1) { | |
fb45065e | 3186 | pthread_mutex_lock(&app->sock_lock); |
7972aab2 | 3187 | ret = ustctl_create_session(app->sock); |
fb45065e | 3188 | pthread_mutex_unlock(&app->sock_lock); |
7972aab2 DG |
3189 | if (ret < 0) { |
3190 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
3191 | ERR("Creating session for app pid %d with ret %d", | |
ffe60014 DG |
3192 | app->pid, ret); |
3193 | } else { | |
3194 | DBG("UST app creating session failed. Application is dead"); | |
3757b385 DG |
3195 | /* |
3196 | * This is normal behavior, an application can die during the | |
3197 | * creation process. Don't report an error so the execution can | |
3198 | * continue normally. This will get flagged ENOTCONN and the | |
3199 | * caller will handle it. | |
3200 | */ | |
3201 | ret = 0; | |
ffe60014 | 3202 | } |
d0b96690 | 3203 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
3204 | if (ret != -ENOMEM) { |
3205 | /* | |
3206 | * Tracer is probably gone or got an internal error so let's | |
3207 | * behave like it will soon unregister or not usable. | |
3208 | */ | |
3209 | ret = -ENOTCONN; | |
3210 | } | |
3211 | goto error; | |
421cb601 DG |
3212 | } |
3213 | ||
7972aab2 DG |
3214 | ua_sess->handle = ret; |
3215 | ||
3216 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
3217 | lttng_ht_node_init_u64(&ua_sess->node, |
3218 | ua_sess->tracing_id); | |
3219 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
10b56aef MD |
3220 | lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle); |
3221 | lttng_ht_add_unique_ulong(app->ust_sessions_objd, | |
3222 | &ua_sess->ust_objd_node); | |
7972aab2 DG |
3223 | |
3224 | DBG2("UST app session created successfully with handle %d", ret); | |
3225 | } | |
3226 | ||
3227 | *ua_sess_ptr = ua_sess; | |
3228 | if (is_created) { | |
3229 | *is_created = created; | |
3230 | } | |
3231 | ||
3232 | /* Everything went well. */ | |
3233 | ret = 0; | |
3234 | ||
3235 | error: | |
3236 | health_code_update(); | |
3237 | return ret; | |
3238 | } | |
3239 | ||
6a6b2068 JG |
3240 | /* |
3241 | * Match function for a hash table lookup of ust_app_ctx. | |
3242 | * | |
3243 | * It matches an ust app context based on the context type and, in the case | |
3244 | * of perf counters, their name. | |
3245 | */ | |
3246 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
3247 | { | |
3248 | struct ust_app_ctx *ctx; | |
bdf64013 | 3249 | const struct lttng_ust_context_attr *key; |
6a6b2068 JG |
3250 | |
3251 | assert(node); | |
3252 | assert(_key); | |
3253 | ||
3254 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
3255 | key = _key; | |
3256 | ||
3257 | /* Context type */ | |
3258 | if (ctx->ctx.ctx != key->ctx) { | |
3259 | goto no_match; | |
3260 | } | |
3261 | ||
bdf64013 JG |
3262 | switch(key->ctx) { |
3263 | case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER: | |
6a6b2068 | 3264 | if (strncmp(key->u.perf_counter.name, |
bdf64013 JG |
3265 | ctx->ctx.u.perf_counter.name, |
3266 | sizeof(key->u.perf_counter.name))) { | |
3267 | goto no_match; | |
3268 | } | |
3269 | break; | |
3270 | case LTTNG_UST_CONTEXT_APP_CONTEXT: | |
3271 | if (strcmp(key->u.app_ctx.provider_name, | |
3272 | ctx->ctx.u.app_ctx.provider_name) || | |
3273 | strcmp(key->u.app_ctx.ctx_name, | |
3274 | ctx->ctx.u.app_ctx.ctx_name)) { | |
6a6b2068 JG |
3275 | goto no_match; |
3276 | } | |
bdf64013 JG |
3277 | break; |
3278 | default: | |
3279 | break; | |
6a6b2068 JG |
3280 | } |
3281 | ||
3282 | /* Match. */ | |
3283 | return 1; | |
3284 | ||
3285 | no_match: | |
3286 | return 0; | |
3287 | } | |
3288 | ||
3289 | /* | |
3290 | * Lookup for an ust app context from an lttng_ust_context. | |
3291 | * | |
be184a0f | 3292 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
3293 | * Return an ust_app_ctx object or NULL on error. |
3294 | */ | |
3295 | static | |
3296 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
bdf64013 | 3297 | struct lttng_ust_context_attr *uctx) |
6a6b2068 JG |
3298 | { |
3299 | struct lttng_ht_iter iter; | |
3300 | struct lttng_ht_node_ulong *node; | |
3301 | struct ust_app_ctx *app_ctx = NULL; | |
3302 | ||
3303 | assert(uctx); | |
3304 | assert(ht); | |
3305 | ||
3306 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
3307 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
3308 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
3309 | node = lttng_ht_iter_get_node_ulong(&iter); | |
3310 | if (!node) { | |
3311 | goto end; | |
3312 | } | |
3313 | ||
3314 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); | |
3315 | ||
3316 | end: | |
3317 | return app_ctx; | |
3318 | } | |
3319 | ||
7972aab2 DG |
3320 | /* |
3321 | * Create a context for the channel on the tracer. | |
3322 | * | |
3323 | * Called with UST app session lock held and a RCU read side lock. | |
3324 | */ | |
3325 | static | |
c9edf082 | 3326 | int create_ust_app_channel_context(struct ust_app_channel *ua_chan, |
f3db82be | 3327 | struct lttng_ust_context_attr *uctx, |
7972aab2 DG |
3328 | struct ust_app *app) |
3329 | { | |
3330 | int ret = 0; | |
7972aab2 DG |
3331 | struct ust_app_ctx *ua_ctx; |
3332 | ||
3333 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
3334 | ||
6a6b2068 JG |
3335 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
3336 | if (ua_ctx) { | |
7972aab2 DG |
3337 | ret = -EEXIST; |
3338 | goto error; | |
3339 | } | |
3340 | ||
3341 | ua_ctx = alloc_ust_app_ctx(uctx); | |
3342 | if (ua_ctx == NULL) { | |
3343 | /* malloc failed */ | |
7682f304 | 3344 | ret = -ENOMEM; |
7972aab2 DG |
3345 | goto error; |
3346 | } | |
3347 | ||
3348 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 3349 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 3350 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
3351 | |
3352 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
3353 | if (ret < 0) { | |
3354 | goto error; | |
3355 | } | |
3356 | ||
3357 | error: | |
3358 | return ret; | |
3359 | } | |
3360 | ||
3361 | /* | |
3362 | * Enable on the tracer side a ust app event for the session and channel. | |
3363 | * | |
3364 | * Called with UST app session lock held. | |
3365 | */ | |
3366 | static | |
3367 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
3368 | struct ust_app_event *ua_event, struct ust_app *app) | |
3369 | { | |
3370 | int ret; | |
3371 | ||
3428a1b7 | 3372 | ret = enable_ust_object(app, ua_event->obj); |
7972aab2 DG |
3373 | if (ret < 0) { |
3374 | goto error; | |
3375 | } | |
3376 | ||
3377 | ua_event->enabled = 1; | |
3378 | ||
3379 | error: | |
3380 | return ret; | |
3381 | } | |
3382 | ||
3383 | /* | |
3384 | * Disable on the tracer side a ust app event for the session and channel. | |
3385 | */ | |
3386 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
3387 | struct ust_app_event *ua_event, struct ust_app *app) | |
3388 | { | |
3389 | int ret; | |
3390 | ||
e2456d0a | 3391 | ret = disable_ust_object(app, ua_event->obj); |
7972aab2 DG |
3392 | if (ret < 0) { |
3393 | goto error; | |
3394 | } | |
3395 | ||
3396 | ua_event->enabled = 0; | |
3397 | ||
3398 | error: | |
3399 | return ret; | |
3400 | } | |
3401 | ||
3402 | /* | |
3403 | * Lookup ust app channel for session and disable it on the tracer side. | |
3404 | */ | |
3405 | static | |
3406 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
3407 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
3408 | { | |
3409 | int ret; | |
3410 | ||
3411 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
3412 | if (ret < 0) { | |
3413 | goto error; | |
3414 | } | |
3415 | ||
3416 | ua_chan->enabled = 0; | |
3417 | ||
3418 | error: | |
3419 | return ret; | |
3420 | } | |
3421 | ||
ebdb334b JR |
3422 | /* |
3423 | * Lookup ust app map for session and disable it on the tracer side. | |
3424 | */ | |
3425 | static | |
3426 | int disable_ust_app_map(struct ust_app_session *ua_sess, | |
3427 | struct ust_app_map *ua_map, struct ust_app *app) | |
3428 | { | |
3429 | int ret; | |
3430 | ||
3431 | ret = disable_ust_map(app, ua_sess, ua_map); | |
3432 | if (ret < 0) { | |
3433 | goto error; | |
3434 | } | |
3435 | ||
3436 | ua_map->enabled = 0; | |
3437 | ||
3438 | error: | |
3439 | return ret; | |
3440 | } | |
3441 | ||
7972aab2 DG |
3442 | /* |
3443 | * Lookup ust app channel for session and enable it on the tracer side. This | |
3444 | * MUST be called with a RCU read side lock acquired. | |
3445 | */ | |
3446 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
3447 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
3448 | { | |
3449 | int ret = 0; | |
3450 | struct lttng_ht_iter iter; | |
3451 | struct lttng_ht_node_str *ua_chan_node; | |
3452 | struct ust_app_channel *ua_chan; | |
3453 | ||
3454 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); | |
3455 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
3456 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 3457 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
3458 | uchan->name, ua_sess->tracing_id); |
3459 | goto error; | |
3460 | } | |
3461 | ||
3462 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
3463 | ||
3464 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
3465 | if (ret < 0) { | |
3466 | goto error; | |
3467 | } | |
3468 | ||
3469 | error: | |
3470 | return ret; | |
3471 | } | |
3472 | ||
ebdb334b JR |
3473 | /* |
3474 | * Lookup ust app map for session and enable it on the tracer side. This | |
3475 | * MUST be called with a RCU read side lock acquired. | |
3476 | */ | |
3477 | static int enable_ust_app_map(struct ust_app_session *ua_sess, | |
3478 | struct ltt_ust_map *umap, struct ust_app *app) | |
3479 | { | |
3480 | int ret = 0; | |
3481 | struct lttng_ht_iter iter; | |
3482 | struct lttng_ht_node_str *ua_map_node; | |
3483 | struct ust_app_map *ua_map; | |
3484 | ||
3485 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &iter); | |
3486 | ua_map_node = lttng_ht_iter_get_node_str(&iter); | |
3487 | if (ua_map_node == NULL) { | |
3488 | DBG2("Unable to find map %s in ust session id %" PRIu64, | |
3489 | umap->name, ua_sess->tracing_id); | |
3490 | goto error; | |
3491 | } | |
3492 | ||
3493 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
3494 | ||
3495 | ret = enable_ust_map(app, ua_sess, ua_map); | |
3496 | if (ret < 0) { | |
3497 | goto error; | |
3498 | } | |
3499 | ||
3500 | error: | |
3501 | return ret; | |
3502 | } | |
3503 | ||
7972aab2 DG |
3504 | /* |
3505 | * Ask the consumer to create a channel and get it if successful. | |
3506 | * | |
fad1ed2f JR |
3507 | * Called with UST app session lock held. |
3508 | * | |
7972aab2 DG |
3509 | * Return 0 on success or else a negative value. |
3510 | */ | |
3511 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
3512 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
e098433c JG |
3513 | int bitness, struct ust_registry_session *registry, |
3514 | uint64_t trace_archive_id) | |
7972aab2 DG |
3515 | { |
3516 | int ret; | |
3517 | unsigned int nb_fd = 0; | |
3518 | struct consumer_socket *socket; | |
3519 | ||
3520 | assert(usess); | |
3521 | assert(ua_sess); | |
3522 | assert(ua_chan); | |
3523 | assert(registry); | |
3524 | ||
3525 | rcu_read_lock(); | |
3526 | health_code_update(); | |
3527 | ||
3528 | /* Get the right consumer socket for the application. */ | |
3529 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
3530 | if (!socket) { | |
3531 | ret = -EINVAL; | |
3532 | goto error; | |
3533 | } | |
3534 | ||
3535 | health_code_update(); | |
3536 | ||
3537 | /* Need one fd for the channel. */ | |
3538 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3539 | if (ret < 0) { | |
3540 | ERR("Exhausted number of available FD upon create channel"); | |
3541 | goto error; | |
3542 | } | |
3543 | ||
3544 | /* | |
3545 | * Ask consumer to create channel. The consumer will return the number of | |
3546 | * stream we have to expect. | |
3547 | */ | |
3548 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
d2956687 | 3549 | registry, usess->current_trace_chunk); |
7972aab2 DG |
3550 | if (ret < 0) { |
3551 | goto error_ask; | |
3552 | } | |
3553 | ||
3554 | /* | |
3555 | * Compute the number of fd needed before receiving them. It must be 2 per | |
3556 | * stream (2 being the default value here). | |
3557 | */ | |
3558 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
3559 | ||
3560 | /* Reserve the amount of file descriptor we need. */ | |
3561 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
3562 | if (ret < 0) { | |
3563 | ERR("Exhausted number of available FD upon create channel"); | |
3564 | goto error_fd_get_stream; | |
3565 | } | |
3566 | ||
3567 | health_code_update(); | |
3568 | ||
3569 | /* | |
3570 | * Now get the channel from the consumer. This call wil populate the stream | |
3571 | * list of that channel and set the ust objects. | |
3572 | */ | |
d9078d0c DG |
3573 | if (usess->consumer->enabled) { |
3574 | ret = ust_consumer_get_channel(socket, ua_chan); | |
3575 | if (ret < 0) { | |
3576 | goto error_destroy; | |
3577 | } | |
7972aab2 DG |
3578 | } |
3579 | ||
3580 | rcu_read_unlock(); | |
3581 | return 0; | |
3582 | ||
3583 | error_destroy: | |
3584 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
3585 | error_fd_get_stream: | |
3586 | /* | |
3587 | * Initiate a destroy channel on the consumer since we had an error | |
3588 | * handling it on our side. The return value is of no importance since we | |
3589 | * already have a ret value set by the previous error that we need to | |
3590 | * return. | |
3591 | */ | |
3592 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
3593 | error_ask: | |
3594 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3595 | error: | |
3596 | health_code_update(); | |
3597 | rcu_read_unlock(); | |
3598 | return ret; | |
3599 | } | |
3600 | ||
ebdb334b JR |
3601 | static int create_map_object(struct ltt_ust_session *usess, |
3602 | struct ust_app_session *ua_sess, struct ust_app_map *ua_map) | |
3603 | { | |
3604 | int i, ret, nr_counter_cpu; | |
3605 | struct ustctl_counter_dimension dimension[1] = {0}; | |
3606 | struct ustctl_daemon_counter *daemon_counter; | |
3607 | struct lttng_ust_object_data **counter_cpus; | |
3608 | enum ustctl_counter_bitness bitness; | |
3609 | int *counter_cpu_fds; | |
3610 | ||
3611 | assert(usess); | |
3612 | assert(ua_sess); | |
3613 | assert(ua_map); | |
3614 | assert(ua_map->bucket_count > 0); | |
3615 | ||
3616 | DBG("Creating UST map \"%s\"", ua_map->name); | |
3617 | ||
3618 | if (ua_map->bitness == LTTNG_MAP_BITNESS_32BITS) { | |
3619 | bitness = USTCTL_COUNTER_BITNESS_32; | |
3620 | } else { | |
3621 | bitness = USTCTL_COUNTER_BITNESS_64; | |
3622 | } | |
3623 | ||
3624 | nr_counter_cpu = ustctl_get_nr_cpu_per_counter(); | |
3625 | counter_cpu_fds = zmalloc(nr_counter_cpu * sizeof(*counter_cpu_fds)); | |
3626 | if (!counter_cpu_fds) { | |
3627 | ret = -1; | |
3628 | goto end; | |
3629 | } | |
3630 | ||
3631 | counter_cpus = zmalloc(nr_counter_cpu * sizeof(**counter_cpus)); | |
3632 | if (!counter_cpus) { | |
3633 | ret = -1; | |
3634 | goto free_cpu_fds; | |
3635 | } | |
3636 | ||
3637 | /* Need one fd for each cpu counter of the map. */ | |
3638 | ret = lttng_fd_get(LTTNG_FD_APPS, nr_counter_cpu); | |
3639 | if (ret < 0) { | |
3640 | ERR("Exhausted number of available FD upon create map"); | |
3641 | goto free_cpu_counters; | |
3642 | } | |
3643 | ||
3644 | for (i = 0; i < nr_counter_cpu; i++) { | |
3645 | counter_cpu_fds[i] = shm_create_anonymous("ust-map-counter"); | |
3646 | if (counter_cpu_fds[i] < 0) { | |
3647 | ERR("Error creating anonymous shared memory object"); | |
3648 | ret = -1; | |
3649 | goto error; | |
3650 | } | |
3651 | } | |
3652 | ||
3653 | dimension[0].size = ua_map->bucket_count; | |
3654 | dimension[0].has_underflow = false; | |
3655 | dimension[0].has_overflow = false; | |
3656 | ||
3657 | daemon_counter = ustctl_create_counter(1, dimension, 0, -1, | |
3658 | nr_counter_cpu, counter_cpu_fds, | |
3659 | bitness, | |
3660 | USTCTL_COUNTER_ARITHMETIC_MODULAR, | |
3661 | USTCTL_COUNTER_ALLOC_PER_CPU, | |
3662 | ua_map->coalesce_hits); | |
3663 | assert(daemon_counter); | |
3664 | ||
3665 | DBG("Created daemon counter succesfully"); | |
3666 | ||
3667 | ua_map->map_handle = daemon_counter; | |
3668 | ||
3669 | ret = ustctl_create_counter_data(daemon_counter, &ua_map->obj); | |
3670 | assert(ret == 0); | |
3671 | DBG("Created counter data succesfully"); | |
3672 | ||
3673 | for (i = 0; i < nr_counter_cpu; i++) { | |
3674 | struct ust_app_map_counter *counter; | |
3675 | ||
3676 | /* Create UST counter */ | |
3677 | counter = ust_app_alloc_map_counter(); | |
3678 | if (counter == NULL) { | |
3679 | ret = -ENOMEM; | |
3680 | goto release_counters; | |
3681 | } | |
3682 | ||
3683 | ret = ustctl_create_counter_cpu_data(daemon_counter, i, | |
3684 | &counter->obj); | |
3685 | if (ret < 0) { | |
3686 | ERR("Creating map counter cpu data"); | |
3687 | free(counter); | |
3688 | goto error; | |
3689 | } | |
3690 | ||
3691 | cds_list_add_tail(&counter->list, &ua_map->counters.head); | |
3692 | ua_map->counters.count++; | |
3693 | ||
3694 | DBG2("UST app map counter %d created successfully", | |
3695 | ua_map->counters.count); | |
3696 | } | |
3697 | ||
3698 | ret = 0; | |
3699 | goto end; | |
3700 | ||
3701 | error: | |
3702 | release_counters: | |
3703 | //TODO | |
3704 | free_cpu_counters: | |
3705 | free(counter_cpus); | |
3706 | ||
3707 | free_cpu_fds: | |
3708 | free(counter_cpu_fds); | |
3709 | end: | |
3710 | return ret; | |
3711 | } | |
3712 | ||
7972aab2 DG |
3713 | /* |
3714 | * Duplicate the ust data object of the ust app stream and save it in the | |
3715 | * buffer registry stream. | |
3716 | * | |
3717 | * Return 0 on success or else a negative value. | |
3718 | */ | |
3719 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
3720 | struct ust_app_stream *stream) | |
3721 | { | |
3722 | int ret; | |
3723 | ||
3724 | assert(reg_stream); | |
3725 | assert(stream); | |
3726 | ||
ebdb334b | 3727 | /* Duplicating a stream requires 2 new fds. Reserve them. */ |
7972aab2 DG |
3728 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); |
3729 | if (ret < 0) { | |
3730 | ERR("Exhausted number of available FD upon duplicate stream"); | |
3731 | goto error; | |
3732 | } | |
3733 | ||
3734 | /* Duplicate object for stream once the original is in the registry. */ | |
3735 | ret = ustctl_duplicate_ust_object_data(&stream->obj, | |
3736 | reg_stream->obj.ust); | |
3737 | if (ret < 0) { | |
3738 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
3739 | reg_stream->obj.ust, stream->obj, ret); | |
3740 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
3741 | goto error; | |
3742 | } | |
3743 | stream->handle = stream->obj->handle; | |
3744 | ||
3745 | error: | |
3746 | return ret; | |
3747 | } | |
3748 | ||
ebdb334b JR |
3749 | /* |
3750 | * Duplicate the ust data object of the ust app map_counter and save it in the | |
3751 | * buffer registry map_counter. | |
3752 | * | |
3753 | * Return 0 on success or else a negative value. | |
3754 | */ | |
3755 | static int duplicate_map_counter_object(struct buffer_reg_map_counter *reg_map_counter, | |
3756 | struct ust_app_map_counter *map_counter) | |
3757 | { | |
3758 | int ret; | |
3759 | ||
3760 | assert(reg_map_counter); | |
3761 | assert(map_counter); | |
3762 | ||
3763 | /* Duplicating a map_counter requires 2 new fds. Reserve them. */ | |
3764 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
3765 | if (ret < 0) { | |
3766 | ERR("Exhausted number of available FD upon duplicate map_counter"); | |
3767 | goto error; | |
3768 | } | |
3769 | ||
3770 | /* Duplicate object for map_counter once the original is in the registry. */ | |
3771 | ret = ustctl_duplicate_ust_object_data(&map_counter->obj, | |
3772 | reg_map_counter->obj.ust); | |
3773 | if (ret < 0) { | |
3774 | ERR("Duplicate map_counter obj from %p to %p failed with ret %d", | |
3775 | reg_map_counter->obj.ust, map_counter->obj, ret); | |
3776 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
3777 | goto error; | |
3778 | } | |
3779 | map_counter->handle = map_counter->obj->handle; | |
3780 | ||
3781 | error: | |
3782 | return ret; | |
3783 | } | |
3784 | ||
7972aab2 DG |
3785 | /* |
3786 | * Duplicate the ust data object of the ust app. channel and save it in the | |
3787 | * buffer registry channel. | |
3788 | * | |
3789 | * Return 0 on success or else a negative value. | |
3790 | */ | |
ebdb334b | 3791 | static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
3792 | struct ust_app_channel *ua_chan) |
3793 | { | |
3794 | int ret; | |
3795 | ||
ebdb334b | 3796 | assert(buf_reg_chan); |
7972aab2 DG |
3797 | assert(ua_chan); |
3798 | ||
ebdb334b | 3799 | /* Duplicating a channel requires 1 new fd. Reserve it. */ |
7972aab2 DG |
3800 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); |
3801 | if (ret < 0) { | |
3802 | ERR("Exhausted number of available FD upon duplicate channel"); | |
3803 | goto error_fd_get; | |
3804 | } | |
3805 | ||
3806 | /* Duplicate object for stream once the original is in the registry. */ | |
ebdb334b | 3807 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust); |
7972aab2 DG |
3808 | if (ret < 0) { |
3809 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
ebdb334b | 3810 | buf_reg_chan->obj.ust, ua_chan->obj, ret); |
7972aab2 DG |
3811 | goto error; |
3812 | } | |
3813 | ua_chan->handle = ua_chan->obj->handle; | |
3814 | ||
3815 | return 0; | |
3816 | ||
3817 | error: | |
3818 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3819 | error_fd_get: | |
3820 | return ret; | |
3821 | } | |
3822 | ||
3823 | /* | |
ebdb334b JR |
3824 | * Duplicate the ust data object of the ust app. map and save it in the |
3825 | * buffer registry map. | |
7972aab2 DG |
3826 | * |
3827 | * Return 0 on success or else a negative value. | |
3828 | */ | |
ebdb334b JR |
3829 | static int duplicate_map_object(struct buffer_reg_map *buf_reg_map, |
3830 | struct ust_app_map *ua_map) | |
7972aab2 | 3831 | { |
ebdb334b | 3832 | int ret; |
7972aab2 | 3833 | |
ebdb334b JR |
3834 | assert(buf_reg_map); |
3835 | assert(ua_map); | |
7972aab2 | 3836 | |
ebdb334b JR |
3837 | /* Duplicating a map requires 1 new fd. Reserve it. */ |
3838 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3839 | if (ret < 0) { | |
3840 | ERR("Exhausted number of available FD upon duplicate map"); | |
3841 | goto error_fd_get; | |
3842 | } | |
3843 | ||
3844 | /* Duplicate object for stream once the original is in the registry. */ | |
3845 | ret = ustctl_duplicate_ust_object_data(&ua_map->obj, buf_reg_map->obj.ust); | |
3846 | if (ret < 0) { | |
3847 | ERR("Duplicate map obj from %p to %p failed with ret: %d", | |
3848 | buf_reg_map->obj.ust, ua_map->obj, ret); | |
3849 | goto error; | |
3850 | } | |
3851 | ua_map->handle = ua_map->obj->handle; | |
3852 | ||
3853 | return 0; | |
3854 | ||
3855 | error: | |
3856 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
3857 | error_fd_get: | |
3858 | return ret; | |
3859 | } | |
3860 | ||
3861 | /* | |
3862 | * For a given channel buffer registry, setup all streams of the given ust | |
3863 | * application channel. | |
3864 | * | |
3865 | * Return 0 on success or else a negative value. | |
3866 | */ | |
3867 | static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan, | |
3868 | struct ust_app_channel *ua_chan, | |
3869 | struct ust_app *app) | |
3870 | { | |
3871 | int ret = 0; | |
3872 | struct ust_app_stream *stream, *stmp; | |
3873 | ||
3874 | assert(buf_reg_chan); | |
3875 | assert(ua_chan); | |
3876 | ||
3877 | DBG2("UST app setup buffer registry stream"); | |
7972aab2 DG |
3878 | |
3879 | /* Send all streams to application. */ | |
3880 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
3881 | struct buffer_reg_stream *reg_stream; | |
3882 | ||
3883 | ret = buffer_reg_stream_create(®_stream); | |
3884 | if (ret < 0) { | |
3885 | goto error; | |
3886 | } | |
3887 | ||
3888 | /* | |
3889 | * Keep original pointer and nullify it in the stream so the delete | |
3890 | * stream call does not release the object. | |
3891 | */ | |
3892 | reg_stream->obj.ust = stream->obj; | |
3893 | stream->obj = NULL; | |
ebdb334b | 3894 | buffer_reg_stream_add(reg_stream, buf_reg_chan); |
421cb601 | 3895 | |
7972aab2 DG |
3896 | /* We don't need the streams anymore. */ |
3897 | cds_list_del(&stream->list); | |
fb45065e | 3898 | delete_ust_app_stream(-1, stream, app); |
7972aab2 | 3899 | } |
421cb601 | 3900 | |
7972aab2 DG |
3901 | error: |
3902 | return ret; | |
3903 | } | |
3904 | ||
ebdb334b JR |
3905 | /* |
3906 | * For a given map buffer registry, setup all counters of the given ust | |
3907 | * application map. | |
3908 | * | |
3909 | * Return 0 on success or else a negative value. | |
3910 | */ | |
3911 | static int setup_buffer_reg_map_counters(struct buffer_reg_map *buf_reg_map, | |
3912 | struct ust_app_map *ua_map, | |
3913 | struct ust_app *app) | |
3914 | { | |
3915 | int ret = 0; | |
3916 | struct ust_app_map_counter *counter, *stmp; | |
3917 | ||
3918 | assert(buf_reg_map); | |
3919 | assert(ua_map); | |
3920 | ||
3921 | DBG2("UST app setup buffer registry counter"); | |
3922 | ||
3923 | /* Send all counters to application. */ | |
3924 | cds_list_for_each_entry_safe(counter, stmp, &ua_map->counters.head, list) { | |
3925 | struct buffer_reg_map_counter *reg_counter; | |
3926 | ||
3927 | ret = buffer_reg_map_counter_create(®_counter); | |
3928 | if (ret < 0) { | |
3929 | goto error; | |
3930 | } | |
3931 | ||
3932 | /* | |
3933 | * Keep original pointer and nullify it in the counter so the delete | |
3934 | * counter call does not release the object. | |
3935 | */ | |
3936 | reg_counter->obj.ust = counter->obj; | |
3937 | counter->obj = NULL; | |
3938 | buffer_reg_map_counter_add(reg_counter, buf_reg_map); | |
3939 | ||
3940 | /* We don't need the counters anymore. */ | |
3941 | cds_list_del(&counter->list); | |
3942 | delete_ust_app_map_counter(-1, counter, app); | |
3943 | } | |
3944 | ||
3945 | error: | |
3946 | return ret; | |
3947 | } | |
3948 | ||
7972aab2 DG |
3949 | /* |
3950 | * Create a buffer registry channel for the given session registry and | |
3951 | * application channel object. If regp pointer is valid, it's set with the | |
3952 | * created object. Important, the created object is NOT added to the session | |
3953 | * registry hash table. | |
3954 | * | |
3955 | * Return 0 on success else a negative value. | |
3956 | */ | |
3957 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
3958 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
3959 | { | |
3960 | int ret; | |
ebdb334b | 3961 | struct buffer_reg_channel *buf_reg_chan = NULL; |
7972aab2 DG |
3962 | |
3963 | assert(reg_sess); | |
3964 | assert(ua_chan); | |
3965 | ||
3966 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
3967 | ||
3968 | /* Create buffer registry channel. */ | |
ebdb334b | 3969 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan); |
7972aab2 DG |
3970 | if (ret < 0) { |
3971 | goto error_create; | |
421cb601 | 3972 | } |
ebdb334b JR |
3973 | assert(buf_reg_chan); |
3974 | buf_reg_chan->consumer_key = ua_chan->key; | |
3975 | buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size; | |
3976 | buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf; | |
421cb601 | 3977 | |
7972aab2 DG |
3978 | /* Create and add a channel registry to session. */ |
3979 | ret = ust_registry_channel_add(reg_sess->reg.ust, | |
3980 | ua_chan->tracing_channel_id); | |
3981 | if (ret < 0) { | |
3982 | goto error; | |
d88aee68 | 3983 | } |
ebdb334b | 3984 | buffer_reg_channel_add(reg_sess, buf_reg_chan); |
d88aee68 | 3985 | |
7972aab2 | 3986 | if (regp) { |
ebdb334b | 3987 | *regp = buf_reg_chan; |
3d8ca23b | 3988 | } |
d88aee68 | 3989 | |
7972aab2 | 3990 | return 0; |
3d8ca23b DG |
3991 | |
3992 | error: | |
7972aab2 | 3993 | /* Safe because the registry channel object was not added to any HT. */ |
ebdb334b JR |
3994 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); |
3995 | error_create: | |
3996 | return ret; | |
3997 | } | |
3998 | ||
3999 | /* | |
4000 | * Create a buffer registry map for the given session registry and | |
4001 | * application map object. If regp pointer is valid, it's set with the | |
4002 | * created object. Important, the created object is NOT added to the session | |
4003 | * registry hash table. | |
4004 | * | |
4005 | * Return 0 on success else a negative value. | |
4006 | */ | |
4007 | static int create_buffer_reg_map(struct buffer_reg_session *reg_sess, | |
4008 | struct ust_app_map *ua_map, struct buffer_reg_map **regp) | |
4009 | { | |
4010 | int ret; | |
4011 | struct buffer_reg_map *buf_reg_map = NULL; | |
4012 | ||
4013 | assert(reg_sess); | |
4014 | assert(ua_map); | |
4015 | ||
4016 | DBG2("UST app creating buffer registry map for %s", ua_map->name); | |
4017 | ||
4018 | /* Create buffer registry map. */ | |
4019 | ret = buffer_reg_map_create(ua_map->tracing_map_id, &buf_reg_map); | |
4020 | if (ret < 0) { | |
4021 | goto error_create; | |
4022 | } | |
4023 | assert(buf_reg_map); | |
4024 | ||
4025 | /* Create and add a map registry to session. */ | |
4026 | ret = ust_registry_map_add(reg_sess->reg.ust, ua_map->tracing_map_id); | |
4027 | if (ret < 0) { | |
4028 | goto error; | |
4029 | } | |
4030 | buffer_reg_map_add(reg_sess, buf_reg_map); | |
4031 | ||
4032 | if (regp) { | |
4033 | *regp = buf_reg_map; | |
4034 | } | |
4035 | ||
4036 | return 0; | |
4037 | ||
4038 | error: | |
4039 | /* Safe because the registry map object was not added to any HT. */ | |
4040 | buffer_reg_map_destroy(buf_reg_map, LTTNG_DOMAIN_UST); | |
7972aab2 | 4041 | error_create: |
3d8ca23b | 4042 | return ret; |
421cb601 DG |
4043 | } |
4044 | ||
55cc08a6 | 4045 | /* |
7972aab2 DG |
4046 | * Setup buffer registry channel for the given session registry and application |
4047 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 4048 | * |
7972aab2 | 4049 | * Return 0 on success else a negative value. |
55cc08a6 | 4050 | */ |
7972aab2 | 4051 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
ebdb334b | 4052 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan, |
fb45065e | 4053 | struct ust_app *app) |
55cc08a6 | 4054 | { |
7972aab2 | 4055 | int ret; |
55cc08a6 | 4056 | |
7972aab2 | 4057 | assert(reg_sess); |
ebdb334b | 4058 | assert(buf_reg_chan); |
7972aab2 DG |
4059 | assert(ua_chan); |
4060 | assert(ua_chan->obj); | |
55cc08a6 | 4061 | |
7972aab2 | 4062 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 4063 | |
7972aab2 | 4064 | /* Setup all streams for the registry. */ |
ebdb334b | 4065 | ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app); |
7972aab2 | 4066 | if (ret < 0) { |
55cc08a6 DG |
4067 | goto error; |
4068 | } | |
4069 | ||
ebdb334b | 4070 | buf_reg_chan->obj.ust = ua_chan->obj; |
7972aab2 | 4071 | ua_chan->obj = NULL; |
55cc08a6 | 4072 | |
7972aab2 | 4073 | return 0; |
55cc08a6 DG |
4074 | |
4075 | error: | |
ebdb334b JR |
4076 | buffer_reg_channel_remove(reg_sess, buf_reg_chan); |
4077 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
4078 | return ret; | |
4079 | } | |
4080 | ||
4081 | /* | |
4082 | * Setup buffer registry map for the given session registry and application | |
4083 | * map object. If regp pointer is valid, it's set with the created object. | |
4084 | * | |
4085 | * Return 0 on success else a negative value. | |
4086 | */ | |
4087 | static int setup_buffer_reg_map(struct buffer_reg_session *reg_sess, | |
4088 | struct ust_app_map *ua_map, struct buffer_reg_map *buf_reg_map, | |
4089 | struct ust_app *app) | |
4090 | { | |
4091 | int ret; | |
4092 | ||
4093 | assert(reg_sess); | |
4094 | assert(buf_reg_map); | |
4095 | assert(ua_map); | |
4096 | assert(ua_map->obj); | |
4097 | ||
4098 | DBG2("UST app setup buffer registry map for %s", ua_map->name); | |
4099 | ||
4100 | /* Setup all counters for the registry. */ | |
4101 | ret = setup_buffer_reg_map_counters(buf_reg_map, ua_map, app); | |
4102 | if (ret < 0) { | |
4103 | goto error; | |
4104 | } | |
4105 | ||
4106 | buf_reg_map->obj.ust = ua_map->obj; | |
4107 | ua_map->obj = NULL; | |
4108 | buf_reg_map->daemon_counter = ua_map->map_handle; | |
4109 | ua_map->map_handle = NULL; | |
4110 | ||
4111 | return 0; | |
4112 | ||
4113 | error: | |
4114 | buffer_reg_map_remove(reg_sess, buf_reg_map); | |
4115 | buffer_reg_map_destroy(buf_reg_map, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
4116 | return ret; |
4117 | } | |
4118 | ||
edb67388 | 4119 | /* |
7972aab2 | 4120 | * Send buffer registry channel to the application. |
d0b96690 | 4121 | * |
7972aab2 | 4122 | * Return 0 on success else a negative value. |
edb67388 | 4123 | */ |
ebdb334b | 4124 | static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan, |
7972aab2 DG |
4125 | struct ust_app *app, struct ust_app_session *ua_sess, |
4126 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
4127 | { |
4128 | int ret; | |
7972aab2 | 4129 | struct buffer_reg_stream *reg_stream; |
edb67388 | 4130 | |
ebdb334b | 4131 | assert(buf_reg_chan); |
7972aab2 DG |
4132 | assert(app); |
4133 | assert(ua_sess); | |
4134 | assert(ua_chan); | |
4135 | ||
4136 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
4137 | ||
ebdb334b | 4138 | ret = duplicate_channel_object(buf_reg_chan, ua_chan); |
edb67388 DG |
4139 | if (ret < 0) { |
4140 | goto error; | |
4141 | } | |
4142 | ||
7972aab2 DG |
4143 | /* Send channel to the application. */ |
4144 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
4145 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
4146 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
4147 | goto error; | |
4148 | } else if (ret < 0) { | |
7972aab2 DG |
4149 | goto error; |
4150 | } | |
4151 | ||
4152 | health_code_update(); | |
4153 | ||
4154 | /* Send all streams to application. */ | |
ebdb334b JR |
4155 | pthread_mutex_lock(&buf_reg_chan->stream_list_lock); |
4156 | cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) { | |
7972aab2 DG |
4157 | struct ust_app_stream stream; |
4158 | ||
4159 | ret = duplicate_stream_object(reg_stream, &stream); | |
4160 | if (ret < 0) { | |
4161 | goto error_stream_unlock; | |
4162 | } | |
4163 | ||
4164 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
4165 | if (ret < 0) { | |
fb45065e | 4166 | (void) release_ust_app_stream(-1, &stream, app); |
a7169585 MD |
4167 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
4168 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
a7169585 | 4169 | } |
7972aab2 DG |
4170 | goto error_stream_unlock; |
4171 | } | |
edb67388 | 4172 | |
7972aab2 DG |
4173 | /* |
4174 | * The return value is not important here. This function will output an | |
4175 | * error if needed. | |
4176 | */ | |
fb45065e | 4177 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
4178 | } |
4179 | ua_chan->is_sent = 1; | |
4180 | ||
4181 | error_stream_unlock: | |
ebdb334b JR |
4182 | pthread_mutex_unlock(&buf_reg_chan->stream_list_lock); |
4183 | error: | |
4184 | return ret; | |
4185 | } | |
4186 | ||
4187 | /* | |
4188 | * Send buffer registry map to the application. | |
4189 | * | |
4190 | * Return 0 on success else a negative value. | |
4191 | */ | |
4192 | static int send_map_uid_to_ust(struct buffer_reg_map *buf_reg_map, | |
4193 | struct ust_app *app, struct ust_app_session *ua_sess, | |
4194 | struct ust_app_map *ua_map) | |
4195 | { | |
4196 | int ret; | |
4197 | struct buffer_reg_map_counter *reg_map_counter; | |
4198 | ||
4199 | assert(buf_reg_map); | |
4200 | assert(app); | |
4201 | assert(ua_sess); | |
4202 | assert(ua_map); | |
4203 | ||
4204 | DBG("UST app sending buffer registry map to ust sock %d", app->sock); | |
4205 | ||
4206 | ret = duplicate_map_object(buf_reg_map, ua_map); | |
4207 | if (ret < 0) { | |
4208 | goto error; | |
4209 | } | |
4210 | ||
4211 | /* Send map to the application. */ | |
4212 | ||
4213 | pthread_mutex_lock(&app->sock_lock); | |
4214 | ret = ustctl_send_counter_data_to_ust(app->sock, | |
4215 | ua_sess->handle, ua_map->obj); | |
4216 | pthread_mutex_unlock(&app->sock_lock); | |
4217 | assert(ret == 0); | |
4218 | if (ret < 0) { | |
4219 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
4220 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
4221 | } | |
4222 | goto error_map_counter_unlock; | |
4223 | } | |
4224 | ||
4225 | ua_map->handle = ua_map->obj->handle; | |
4226 | ||
4227 | health_code_update(); | |
4228 | ||
4229 | /* Send all map_counters to application. */ | |
4230 | pthread_mutex_lock(&buf_reg_map->counter_list_lock); | |
4231 | cds_list_for_each_entry(reg_map_counter, &buf_reg_map->counters, lnode) { | |
4232 | struct ust_app_map_counter map_counter; | |
4233 | ||
4234 | ret = duplicate_map_counter_object(reg_map_counter, &map_counter); | |
4235 | if (ret < 0) { | |
4236 | goto error_map_counter_unlock; | |
4237 | } | |
4238 | ||
4239 | pthread_mutex_lock(&app->sock_lock); | |
4240 | // Do send the per cpu counter here | |
4241 | ret = ustctl_send_counter_cpu_data_to_ust(app->sock, | |
4242 | ua_map->obj, map_counter.obj); | |
4243 | pthread_mutex_unlock(&app->sock_lock); | |
4244 | assert(ret == 0); | |
4245 | if (ret < 0) { | |
4246 | (void) release_ust_app_map_counter(-1, &map_counter, app); | |
4247 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { | |
4248 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
4249 | } | |
4250 | goto error_map_counter_unlock; | |
4251 | } | |
4252 | ||
4253 | /* | |
4254 | * The return value is not important here. This function will | |
4255 | * output an error if needed. | |
4256 | */ | |
4257 | (void) release_ust_app_map_counter(-1, &map_counter, app); | |
4258 | } | |
4259 | ua_map->is_sent = 1; | |
4260 | ||
4261 | error_map_counter_unlock: | |
4262 | pthread_mutex_unlock(&buf_reg_map->counter_list_lock); | |
edb67388 DG |
4263 | error: |
4264 | return ret; | |
4265 | } | |
4266 | ||
9730260e | 4267 | /* |
7972aab2 DG |
4268 | * Create and send to the application the created buffers with per UID buffers. |
4269 | * | |
9acdc1d6 | 4270 | * This MUST be called with a RCU read side lock acquired. |
71e0a100 | 4271 | * The session list lock and the session's lock must be acquired. |
9acdc1d6 | 4272 | * |
7972aab2 | 4273 | * Return 0 on success else a negative value. |
9730260e | 4274 | */ |
7972aab2 DG |
4275 | static int create_channel_per_uid(struct ust_app *app, |
4276 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4277 | struct ust_app_channel *ua_chan) | |
9730260e DG |
4278 | { |
4279 | int ret; | |
7972aab2 | 4280 | struct buffer_reg_uid *reg_uid; |
ebdb334b | 4281 | struct buffer_reg_channel *buf_reg_chan; |
e32d7f27 | 4282 | struct ltt_session *session = NULL; |
e098433c | 4283 | enum lttng_error_code notification_ret; |
ebdb334b | 4284 | struct ust_registry_channel *ust_reg_chan; |
9730260e | 4285 | |
7972aab2 DG |
4286 | assert(app); |
4287 | assert(usess); | |
4288 | assert(ua_sess); | |
4289 | assert(ua_chan); | |
4290 | ||
4291 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
4292 | ||
4293 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
4294 | /* | |
4295 | * The session creation handles the creation of this global registry | |
4296 | * object. If none can be find, there is a code flow problem or a | |
4297 | * teardown race. | |
4298 | */ | |
4299 | assert(reg_uid); | |
4300 | ||
ebdb334b | 4301 | buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, |
7972aab2 | 4302 | reg_uid); |
ebdb334b | 4303 | if (buf_reg_chan) { |
2721f7ea JG |
4304 | goto send_channel; |
4305 | } | |
7972aab2 | 4306 | |
2721f7ea | 4307 | /* Create the buffer registry channel object. */ |
ebdb334b | 4308 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan); |
2721f7ea JG |
4309 | if (ret < 0) { |
4310 | ERR("Error creating the UST channel \"%s\" registry instance", | |
f14256d6 | 4311 | ua_chan->name); |
2721f7ea JG |
4312 | goto error; |
4313 | } | |
f14256d6 | 4314 | |
e098433c JG |
4315 | session = session_find_by_id(ua_sess->tracing_id); |
4316 | assert(session); | |
4317 | assert(pthread_mutex_trylock(&session->lock)); | |
4318 | assert(session_trylock_list()); | |
4319 | ||
2721f7ea JG |
4320 | /* |
4321 | * Create the buffers on the consumer side. This call populates the | |
4322 | * ust app channel object with all streams and data object. | |
4323 | */ | |
4324 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
e098433c | 4325 | app->bits_per_long, reg_uid->registry->reg.ust, |
d2956687 | 4326 | session->most_recent_chunk_id.value); |
2721f7ea JG |
4327 | if (ret < 0) { |
4328 | ERR("Error creating UST channel \"%s\" on the consumer daemon", | |
4329 | ua_chan->name); | |
7972aab2 DG |
4330 | |
4331 | /* | |
2721f7ea JG |
4332 | * Let's remove the previously created buffer registry channel so |
4333 | * it's not visible anymore in the session registry. | |
7972aab2 | 4334 | */ |
2721f7ea JG |
4335 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, |
4336 | ua_chan->tracing_channel_id, false); | |
ebdb334b JR |
4337 | buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan); |
4338 | buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST); | |
2721f7ea | 4339 | goto error; |
7972aab2 DG |
4340 | } |
4341 | ||
2721f7ea JG |
4342 | /* |
4343 | * Setup the streams and add it to the session registry. | |
4344 | */ | |
4345 | ret = setup_buffer_reg_channel(reg_uid->registry, | |
ebdb334b | 4346 | ua_chan, buf_reg_chan, app); |
2721f7ea JG |
4347 | if (ret < 0) { |
4348 | ERR("Error setting up UST channel \"%s\"", ua_chan->name); | |
4349 | goto error; | |
4350 | } | |
4351 | ||
e098433c JG |
4352 | /* Notify the notification subsystem of the channel's creation. */ |
4353 | pthread_mutex_lock(®_uid->registry->reg.ust->lock); | |
ebdb334b | 4354 | ust_reg_chan = ust_registry_channel_find(reg_uid->registry->reg.ust, |
e098433c | 4355 | ua_chan->tracing_channel_id); |
ebdb334b JR |
4356 | assert(ust_reg_chan); |
4357 | ust_reg_chan->consumer_key = ua_chan->key; | |
4358 | ust_reg_chan = NULL; | |
e098433c | 4359 | pthread_mutex_unlock(®_uid->registry->reg.ust->lock); |
e9404c27 | 4360 | |
e098433c JG |
4361 | notification_ret = notification_thread_command_add_channel( |
4362 | notification_thread_handle, session->name, | |
ff588497 JR |
4363 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
4364 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
4365 | ua_chan->name, | |
470cc211 | 4366 | ua_chan->key, LTTNG_DOMAIN_UST, |
e098433c JG |
4367 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
4368 | if (notification_ret != LTTNG_OK) { | |
4369 | ret = - (int) notification_ret; | |
4370 | ERR("Failed to add channel to notification thread"); | |
4371 | goto error; | |
e9404c27 JG |
4372 | } |
4373 | ||
2721f7ea | 4374 | send_channel: |
66ff8e3f | 4375 | /* Send buffers to the application. */ |
ebdb334b | 4376 | ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan); |
66ff8e3f JG |
4377 | if (ret < 0) { |
4378 | if (ret != -ENOTCONN) { | |
4379 | ERR("Error sending channel to application"); | |
4380 | } | |
4381 | goto error; | |
4382 | } | |
4383 | ||
9730260e | 4384 | error: |
e32d7f27 JG |
4385 | if (session) { |
4386 | session_put(session); | |
4387 | } | |
9730260e DG |
4388 | return ret; |
4389 | } | |
4390 | ||
ebdb334b JR |
4391 | /* |
4392 | * Create and send to the application the created buffers with per UID buffers. | |
4393 | * | |
4394 | * This MUST be called with a RCU read side lock acquired. | |
4395 | * The session list lock and the session's lock must be acquired. | |
4396 | * | |
4397 | * Return 0 on success else a negative value. | |
4398 | */ | |
4399 | static int create_map_per_uid(struct ust_app *app, | |
4400 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4401 | struct ust_app_map *ua_map) | |
4402 | { | |
4403 | int ret; | |
4404 | struct buffer_reg_uid *buffer_reg_uid; | |
4405 | struct buffer_reg_map *buffer_reg_map; | |
4406 | struct ltt_session *session = NULL; | |
4407 | struct ust_registry_map *ust_reg_map; | |
4408 | ||
4409 | assert(app); | |
4410 | assert(usess); | |
4411 | assert(ua_sess); | |
4412 | assert(ua_map); | |
4413 | ||
4414 | DBG("UST app creating map %s with per UID buffers", ua_map->name); | |
4415 | ||
4416 | buffer_reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
4417 | /* | |
4418 | * The session creation handles the creation of this global registry | |
4419 | * object. If none can be find, there is a code flow problem or a | |
4420 | * teardown race. | |
4421 | */ | |
4422 | assert(buffer_reg_uid); | |
4423 | ||
4424 | buffer_reg_map = buffer_reg_map_find(ua_map->tracing_map_id, | |
4425 | buffer_reg_uid); | |
4426 | if (buffer_reg_map) { | |
4427 | goto send_map; | |
4428 | } | |
4429 | ||
4430 | /* Create the buffer registry map object. */ | |
4431 | ret = create_buffer_reg_map(buffer_reg_uid->registry, ua_map, | |
4432 | &buffer_reg_map); | |
4433 | if (ret < 0) { | |
4434 | ERR("Error creating the UST map \"%s\" registry instance", | |
4435 | ua_map->name); | |
4436 | goto error; | |
4437 | } | |
4438 | ||
4439 | session = session_find_by_id(ua_sess->tracing_id); | |
4440 | assert(session); | |
4441 | assert(pthread_mutex_trylock(&session->lock)); | |
4442 | assert(session_trylock_list()); | |
4443 | ||
4444 | /* | |
4445 | */ | |
4446 | ret = create_map_object(usess, ua_sess, ua_map); | |
4447 | assert(ret == 0); | |
4448 | if (ret < 0) { | |
4449 | ERR("Error creating UST map object: map_name = \"%s\"", ua_map->name); | |
4450 | goto error; | |
4451 | } | |
4452 | ||
4453 | /* | |
4454 | * Setup the streams and add it to the session registry. | |
4455 | */ | |
4456 | ret = setup_buffer_reg_map(buffer_reg_uid->registry, ua_map, | |
4457 | buffer_reg_map, app); | |
4458 | if (ret < 0) { | |
4459 | ERR("Error setting up UST map \"%s\"", ua_map->name); | |
4460 | goto error; | |
4461 | } | |
4462 | ||
4463 | /* Notify the notification subsystem of the map's creation. */ | |
4464 | pthread_mutex_lock(&buffer_reg_uid->registry->reg.ust->lock); | |
4465 | ust_reg_map = ust_registry_map_find(buffer_reg_uid->registry->reg.ust, | |
4466 | ua_map->tracing_map_id); | |
4467 | assert(ust_reg_map); | |
4468 | ust_reg_map = NULL; | |
4469 | pthread_mutex_unlock(&buffer_reg_uid->registry->reg.ust->lock); | |
4470 | ||
4471 | send_map: | |
4472 | /* Send buffers to the application. */ | |
4473 | ret = send_map_uid_to_ust(buffer_reg_map, app, ua_sess, ua_map); | |
4474 | if (ret < 0) { | |
4475 | if (ret != -ENOTCONN) { | |
4476 | ERR("Error sending map to application"); | |
4477 | } | |
4478 | goto error; | |
4479 | } | |
4480 | ||
4481 | error: | |
4482 | if (session) { | |
4483 | session_put(session); | |
4484 | } | |
4485 | return ret; | |
4486 | } | |
4487 | ||
4488 | //static int destroy_map_per_uid(struct ust_app *app, | |
4489 | // struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4490 | // struct ust_app_map *ua_map) | |
4491 | //{ | |
4492 | // int ret; | |
4493 | // struct buffer_reg_uid *buffer_reg_uid; | |
4494 | // struct buffer_reg_map *buffer_reg_map; | |
4495 | // | |
4496 | // assert(app); | |
4497 | // assert(usess); | |
4498 | // assert(ua_sess); | |
4499 | // assert(ua_map); | |
4500 | // | |
4501 | // DBG("UST app destroy map %s with per UID buffers", ua_map->name); | |
4502 | // | |
4503 | // buffer_reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
4504 | // /* | |
4505 | // * The session creation handles the creation of this global registry | |
4506 | // * object. If none can be find, there is a code flow problem or a | |
4507 | // * teardown race. | |
4508 | // */ | |
4509 | // assert(buffer_reg_uid); | |
4510 | // | |
4511 | // buffer_reg_map = buffer_reg_map_find(ua_map->tracing_map_id, | |
4512 | // buffer_reg_uid); | |
4513 | // if (!buffer_reg_map) { | |
4514 | // ERR("Can't find map in buffer registry: map-name = '%s', uid = %d", | |
4515 | // ua_map->name, app->uid); | |
4516 | // ret = -1; | |
4517 | // goto end; | |
4518 | // } | |
4519 | // | |
4520 | // buffer_reg_map_destroy(buffer_reg_map, LTTNG_DOMAIN_UST); | |
4521 | // | |
4522 | // ret = 0; | |
4523 | //end: | |
4524 | // return ret; | |
4525 | //} | |
4526 | ||
78f0bacd | 4527 | /* |
7972aab2 DG |
4528 | * Create and send to the application the created buffers with per PID buffers. |
4529 | * | |
fad1ed2f | 4530 | * Called with UST app session lock held. |
71e0a100 | 4531 | * The session list lock and the session's lock must be acquired. |
fad1ed2f | 4532 | * |
7972aab2 | 4533 | * Return 0 on success else a negative value. |
78f0bacd | 4534 | */ |
7972aab2 DG |
4535 | static int create_channel_per_pid(struct ust_app *app, |
4536 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4537 | struct ust_app_channel *ua_chan) | |
78f0bacd | 4538 | { |
8535a6d9 | 4539 | int ret; |
7972aab2 | 4540 | struct ust_registry_session *registry; |
e9404c27 | 4541 | enum lttng_error_code cmd_ret; |
e32d7f27 | 4542 | struct ltt_session *session = NULL; |
e9404c27 | 4543 | uint64_t chan_reg_key; |
ebdb334b | 4544 | struct ust_registry_channel *ust_reg_chan; |
78f0bacd | 4545 | |
7972aab2 DG |
4546 | assert(app); |
4547 | assert(usess); | |
4548 | assert(ua_sess); | |
4549 | assert(ua_chan); | |
4550 | ||
4551 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
4552 | ||
4553 | rcu_read_lock(); | |
4554 | ||
4555 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 4556 | /* The UST app session lock is held, registry shall not be null. */ |
7972aab2 DG |
4557 | assert(registry); |
4558 | ||
4559 | /* Create and add a new channel registry to session. */ | |
4560 | ret = ust_registry_channel_add(registry, ua_chan->key); | |
78f0bacd | 4561 | if (ret < 0) { |
f14256d6 MD |
4562 | ERR("Error creating the UST channel \"%s\" registry instance", |
4563 | ua_chan->name); | |
78f0bacd DG |
4564 | goto error; |
4565 | } | |
4566 | ||
e098433c JG |
4567 | session = session_find_by_id(ua_sess->tracing_id); |
4568 | assert(session); | |
4569 | ||
4570 | assert(pthread_mutex_trylock(&session->lock)); | |
4571 | assert(session_trylock_list()); | |
4572 | ||
7972aab2 DG |
4573 | /* Create and get channel on the consumer side. */ |
4574 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
e098433c | 4575 | app->bits_per_long, registry, |
d2956687 | 4576 | session->most_recent_chunk_id.value); |
7972aab2 | 4577 | if (ret < 0) { |
f14256d6 MD |
4578 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
4579 | ua_chan->name); | |
5b951542 | 4580 | goto error_remove_from_registry; |
7972aab2 DG |
4581 | } |
4582 | ||
4583 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
4584 | if (ret < 0) { | |
a7169585 MD |
4585 | if (ret != -ENOTCONN) { |
4586 | ERR("Error sending channel to application"); | |
4587 | } | |
5b951542 | 4588 | goto error_remove_from_registry; |
7972aab2 | 4589 | } |
8535a6d9 | 4590 | |
e9404c27 JG |
4591 | chan_reg_key = ua_chan->key; |
4592 | pthread_mutex_lock(®istry->lock); | |
ebdb334b JR |
4593 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
4594 | assert(ust_reg_chan); | |
4595 | ust_reg_chan->consumer_key = ua_chan->key; | |
e9404c27 JG |
4596 | pthread_mutex_unlock(®istry->lock); |
4597 | ||
4598 | cmd_ret = notification_thread_command_add_channel( | |
4599 | notification_thread_handle, session->name, | |
ff588497 JR |
4600 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
4601 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
4602 | ua_chan->name, | |
470cc211 | 4603 | ua_chan->key, LTTNG_DOMAIN_UST, |
e9404c27 JG |
4604 | ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf); |
4605 | if (cmd_ret != LTTNG_OK) { | |
4606 | ret = - (int) cmd_ret; | |
4607 | ERR("Failed to add channel to notification thread"); | |
5b951542 | 4608 | goto error_remove_from_registry; |
e9404c27 JG |
4609 | } |
4610 | ||
5b951542 MD |
4611 | error_remove_from_registry: |
4612 | if (ret) { | |
4613 | ust_registry_channel_del_free(registry, ua_chan->key, false); | |
4614 | } | |
78f0bacd | 4615 | error: |
7972aab2 | 4616 | rcu_read_unlock(); |
e32d7f27 JG |
4617 | if (session) { |
4618 | session_put(session); | |
4619 | } | |
78f0bacd DG |
4620 | return ret; |
4621 | } | |
4622 | ||
4623 | /* | |
ebdb334b | 4624 | * Create and send to the application the created buffers with per PID buffers. |
7972aab2 | 4625 | * |
fad1ed2f | 4626 | * Called with UST app session lock held. |
ebdb334b | 4627 | * The session list lock and the session's lock must be acquired. |
fad1ed2f | 4628 | * |
ebdb334b | 4629 | * Return 0 on success else a negative value. |
78f0bacd | 4630 | */ |
ebdb334b | 4631 | static int create_map_per_pid(struct ust_app *app, |
7972aab2 | 4632 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, |
ebdb334b | 4633 | struct ust_app_map *ua_map) |
78f0bacd | 4634 | { |
7972aab2 | 4635 | int ret; |
ebdb334b JR |
4636 | struct ust_registry_session *registry; |
4637 | struct ltt_session *session = NULL; | |
4638 | uint64_t map_reg_key; | |
4639 | struct ust_registry_map *ust_reg_map; | |
78f0bacd | 4640 | |
7972aab2 DG |
4641 | assert(app); |
4642 | assert(usess); | |
4643 | assert(ua_sess); | |
ebdb334b | 4644 | assert(ua_map); |
7972aab2 | 4645 | |
ebdb334b | 4646 | DBG("UST app creating map %s with per PID buffers", ua_map->name); |
78f0bacd | 4647 | |
ebdb334b JR |
4648 | rcu_read_lock(); |
4649 | ||
4650 | registry = get_session_registry(ua_sess); | |
4651 | /* The UST app session lock is held, registry shall not be null. */ | |
4652 | assert(registry); | |
4653 | ||
4654 | /* Create and add a new map registry to session. */ | |
4655 | ret = ust_registry_map_add(registry, ua_map->key); | |
4656 | if (ret < 0) { | |
4657 | ERR("Error creating the UST map \"%s\" registry instance", | |
4658 | ua_map->name); | |
4659 | goto error; | |
4660 | } | |
4661 | ||
4662 | session = session_find_by_id(ua_sess->tracing_id); | |
4663 | assert(session); | |
4664 | ||
4665 | assert(pthread_mutex_trylock(&session->lock)); | |
4666 | assert(session_trylock_list()); | |
4667 | ||
4668 | /* Create and get map. */ | |
4669 | ret = create_map_object(usess, ua_sess, ua_map); | |
4670 | if (ret < 0) { | |
4671 | ERR("Error creating UST map object: map_name = \"%s\" ", | |
4672 | ua_map->name); | |
4673 | goto error_remove_from_registry; | |
4674 | } | |
4675 | ||
4676 | ret = send_map_pid_to_ust(app, ua_sess, ua_map); | |
4677 | if (ret < 0) { | |
4678 | if (ret != -ENOTCONN) { | |
4679 | ERR("Error sending map to application"); | |
4680 | } | |
4681 | goto error_remove_from_registry; | |
4682 | } | |
4683 | ||
4684 | map_reg_key = ua_map->key; | |
4685 | pthread_mutex_lock(®istry->lock); | |
4686 | ust_reg_map = ust_registry_map_find(registry, map_reg_key); | |
4687 | assert(ust_reg_map); | |
4688 | pthread_mutex_unlock(®istry->lock); | |
4689 | ||
4690 | error_remove_from_registry: | |
4691 | if (ret) { | |
4692 | ust_registry_map_del_free(registry, ua_map->key); | |
4693 | } | |
4694 | error: | |
4695 | rcu_read_unlock(); | |
4696 | if (session) { | |
4697 | session_put(session); | |
4698 | } | |
4699 | return ret; | |
4700 | } | |
4701 | ||
4702 | /* | |
4703 | * From an already allocated ust app channel, create the channel buffers if | |
4704 | * needed and send them to the application. This MUST be called with a RCU read | |
4705 | * side lock acquired. | |
4706 | * | |
4707 | * Called with UST app session lock held. | |
4708 | * | |
4709 | * Return 0 on success or else a negative value. Returns -ENOTCONN if | |
4710 | * the application exited concurrently. | |
4711 | */ | |
4712 | static int ust_app_channel_send(struct ust_app *app, | |
4713 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4714 | struct ust_app_channel *ua_chan) | |
4715 | { | |
4716 | int ret; | |
4717 | ||
4718 | assert(app); | |
4719 | assert(usess); | |
4720 | assert(usess->active); | |
4721 | assert(ua_sess); | |
4722 | assert(ua_chan); | |
4723 | ||
4724 | /* Handle buffer type before sending the channel to the application. */ | |
4725 | switch (usess->buffer_type) { | |
4726 | case LTTNG_BUFFER_PER_UID: | |
4727 | { | |
4728 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
4729 | if (ret < 0) { | |
4730 | goto error; | |
4731 | } | |
4732 | break; | |
4733 | } | |
4734 | case LTTNG_BUFFER_PER_PID: | |
4735 | { | |
4736 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
4737 | if (ret < 0) { | |
4738 | goto error; | |
4739 | } | |
4740 | break; | |
4741 | } | |
4742 | default: | |
4743 | assert(0); | |
4744 | ret = -EINVAL; | |
4745 | goto error; | |
4746 | } | |
4747 | ||
4748 | /* Initialize ust objd object using the received handle and add it. */ | |
4749 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
4750 | lttng_ht_add_unique_ulong(app->ust_chan_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 4751 | |
7972aab2 DG |
4752 | /* If channel is not enabled, disable it on the tracer */ |
4753 | if (!ua_chan->enabled) { | |
4754 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
4755 | if (ret < 0) { | |
4756 | goto error; | |
4757 | } | |
78f0bacd DG |
4758 | } |
4759 | ||
4760 | error: | |
4761 | return ret; | |
4762 | } | |
4763 | ||
ebdb334b JR |
4764 | /* |
4765 | * From an already allocated ust app map, create the map buffers if | |
4766 | * needed and send them to the application. This MUST be called with a RCU read | |
4767 | * side lock acquired. | |
4768 | * | |
4769 | * Called with UST app session lock held. | |
4770 | * | |
4771 | * Return 0 on success or else a negative value. Returns -ENOTCONN if | |
4772 | * the application exited concurrently. | |
4773 | */ | |
4774 | static int ust_app_map_send(struct ust_app *app, | |
4775 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
4776 | struct ust_app_map *ua_map) | |
4777 | { | |
4778 | int ret; | |
4779 | ||
4780 | assert(app); | |
4781 | assert(usess); | |
4782 | assert(usess->active); | |
4783 | assert(ua_sess); | |
4784 | assert(ua_map); | |
4785 | ||
4786 | /* Handle buffer type before sending the map to the application. */ | |
4787 | switch (usess->buffer_type) { | |
4788 | case LTTNG_BUFFER_PER_UID: | |
4789 | { | |
4790 | ret = create_map_per_uid(app, usess, ua_sess, ua_map); | |
4791 | if (ret < 0) { | |
4792 | goto error; | |
4793 | } | |
4794 | break; | |
4795 | } | |
4796 | case LTTNG_BUFFER_PER_PID: | |
4797 | { | |
4798 | ret = create_map_per_pid(app, usess, ua_sess, ua_map); | |
4799 | if (ret < 0) { | |
4800 | goto error; | |
4801 | } | |
4802 | break; | |
4803 | } | |
4804 | default: | |
4805 | assert(0); | |
4806 | ret = -EINVAL; | |
4807 | goto error; | |
4808 | } | |
4809 | ||
4810 | /* Initialize ust objd object using the received handle and add it. */ | |
4811 | lttng_ht_node_init_ulong(&ua_map->ust_objd_node, ua_map->handle); | |
4812 | lttng_ht_add_unique_ulong(app->ust_map_objd, &ua_map->ust_objd_node); | |
4813 | ||
4814 | /* If map is not enabled, disable it on the tracer */ | |
4815 | if (!ua_map->enabled) { | |
4816 | ret = disable_ust_map(app, ua_sess, ua_map); | |
4817 | if (ret < 0) { | |
4818 | goto error; | |
4819 | } | |
4820 | } | |
4821 | ||
4822 | error: | |
4823 | return ret; | |
4824 | } | |
4825 | ||
284d8f55 | 4826 | /* |
88e3c2f5 | 4827 | * Create UST app channel and return it through ua_chanp if not NULL. |
d0b96690 | 4828 | * |
36b588ed | 4829 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 4830 | * |
88e3c2f5 | 4831 | * Return 0 on success or else a negative value. |
284d8f55 | 4832 | */ |
88e3c2f5 JG |
4833 | static int ust_app_channel_allocate(struct ust_app_session *ua_sess, |
4834 | struct ltt_ust_channel *uchan, | |
7972aab2 | 4835 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
4d710ac2 | 4836 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
4837 | { |
4838 | int ret = 0; | |
bec39940 DG |
4839 | struct lttng_ht_iter iter; |
4840 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
4841 | struct ust_app_channel *ua_chan; |
4842 | ||
4843 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
4844 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
4845 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 4846 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 4847 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 4848 | goto end; |
5b4a0ec0 DG |
4849 | } |
4850 | ||
d0b96690 | 4851 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
4852 | if (ua_chan == NULL) { |
4853 | /* Only malloc can fail here */ | |
4d710ac2 | 4854 | ret = -ENOMEM; |
88e3c2f5 | 4855 | goto error; |
fc34caaa DG |
4856 | } |
4857 | shadow_copy_channel(ua_chan, uchan); | |
4858 | ||
ffe60014 DG |
4859 | /* Set channel type. */ |
4860 | ua_chan->attr.type = type; | |
4861 | ||
d0b96690 DG |
4862 | /* Only add the channel if successful on the tracer side. */ |
4863 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
fc34caaa | 4864 | end: |
4d710ac2 DG |
4865 | if (ua_chanp) { |
4866 | *ua_chanp = ua_chan; | |
4867 | } | |
4868 | ||
4869 | /* Everything went well. */ | |
4870 | return 0; | |
5b4a0ec0 DG |
4871 | |
4872 | error: | |
4d710ac2 | 4873 | return ret; |
5b4a0ec0 DG |
4874 | } |
4875 | ||
ebdb334b JR |
4876 | /* |
4877 | * Create UST app map and return it through ua_mapp if not NULL. | |
4878 | * | |
4879 | * Called with UST app session lock and RCU read-side lock held. | |
4880 | * | |
4881 | * Return 0 on success or else a negative value. | |
4882 | */ | |
4883 | static int ust_app_map_allocate(struct ust_app_session *ua_sess, | |
4884 | struct ltt_ust_map *umap, | |
4885 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, | |
4886 | struct ust_app_map **ua_mapp) | |
4887 | { | |
4888 | int ret = 0; | |
4889 | struct lttng_ht_iter iter; | |
4890 | struct lttng_ht_node_str *ua_map_node; | |
4891 | struct ust_app_map *ua_map; | |
4892 | ||
4893 | DBG("Allocating map id = %"PRIu64, umap->id); | |
4894 | ||
4895 | /* Lookup map in the ust app session */ | |
4896 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &iter); | |
4897 | ua_map_node = lttng_ht_iter_get_node_str(&iter); | |
4898 | if (ua_map_node != NULL) { | |
4899 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
4900 | goto end; | |
4901 | } | |
4902 | ||
4903 | ua_map = alloc_ust_app_map(umap->name, ua_sess); | |
4904 | if (ua_map == NULL) { | |
4905 | /* Only malloc can fail here */ | |
4906 | ret = -ENOMEM; | |
4907 | goto error; | |
4908 | } | |
4909 | //shadow_copy_map(ua_map, umap); | |
4910 | ua_map->tracing_map_id = umap->id; | |
4911 | ua_map->coalesce_hits = umap->coalesce_hits; | |
4912 | ua_map->dead_app_kv_values = &umap->dead_app_kv_values; | |
4913 | ua_map->bitness = umap->bitness; | |
4914 | ||
4915 | /* Set map type. */ | |
4916 | //ua_map->attr.type = type; | |
4917 | ua_map->bucket_count = umap->bucket_count; | |
4918 | ||
4919 | /* Only add the map if successful on the tracer side. */ | |
4920 | lttng_ht_add_unique_str(ua_sess->maps, &ua_map->node); | |
4921 | end: | |
4922 | if (ua_mapp) { | |
4923 | *ua_mapp = ua_map; | |
4924 | } | |
4925 | ||
4926 | /* Everything went well. */ | |
4927 | return 0; | |
4928 | ||
4929 | error: | |
4930 | return ret; | |
4931 | } | |
4932 | ||
5b4a0ec0 DG |
4933 | /* |
4934 | * Create UST app event and create it on the tracer side. | |
d0b96690 | 4935 | * |
993578ff | 4936 | * Must be called with the RCU read side lock held. |
d0b96690 | 4937 | * Called with ust app session mutex held. |
5b4a0ec0 | 4938 | */ |
edb67388 | 4939 | static |
ebdb334b | 4940 | int create_ust_app_channel_event(struct ust_app_session *ua_sess, |
edb67388 DG |
4941 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, |
4942 | struct ust_app *app) | |
284d8f55 | 4943 | { |
edb67388 | 4944 | int ret = 0; |
5b4a0ec0 | 4945 | struct ust_app_event *ua_event; |
284d8f55 | 4946 | |
edb67388 DG |
4947 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
4948 | if (ua_event == NULL) { | |
20533947 | 4949 | /* Only failure mode of alloc_ust_app_event(). */ |
edb67388 | 4950 | ret = -ENOMEM; |
fc34caaa | 4951 | goto end; |
5b4a0ec0 | 4952 | } |
edb67388 | 4953 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 4954 | |
edb67388 | 4955 | /* Create it on the tracer side */ |
ebdb334b | 4956 | ret = create_ust_channel_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 4957 | if (ret < 0) { |
e9f11505 JG |
4958 | /* |
4959 | * Not found previously means that it does not exist on the | |
4960 | * tracer. If the application reports that the event existed, | |
4961 | * it means there is a bug in the sessiond or lttng-ust | |
4962 | * (or corruption, etc.) | |
4963 | */ | |
4964 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
4965 | ERR("Tracer for application reported that an event being created already existed: " | |
4966 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
4967 | uevent->attr.name, | |
4968 | app->pid, app->ppid, app->uid, | |
4969 | app->gid); | |
4970 | } | |
284d8f55 DG |
4971 | goto error; |
4972 | } | |
4973 | ||
ebdb334b | 4974 | add_unique_ust_app_event(ua_chan->events, ua_event); |
284d8f55 | 4975 | |
993578ff JR |
4976 | DBG2("UST app create event completed: app = '%s' (ppid: %d)", |
4977 | app->name, app->ppid); | |
7f79d3a1 | 4978 | |
edb67388 | 4979 | end: |
fc34caaa DG |
4980 | return ret; |
4981 | ||
5b4a0ec0 | 4982 | error: |
fc34caaa | 4983 | /* Valid. Calling here is already in a read side lock */ |
fb45065e | 4984 | delete_ust_app_event(-1, ua_event, app); |
edb67388 | 4985 | return ret; |
5b4a0ec0 DG |
4986 | } |
4987 | ||
ebdb334b JR |
4988 | /* |
4989 | * Create UST app event and create it on the tracer side. | |
4990 | * | |
4991 | * Must be called with the RCU read side lock held. | |
4992 | * Called with ust app session mutex held. | |
4993 | */ | |
4994 | static | |
4995 | int create_ust_app_map_event(struct ust_app_session *ua_sess, | |
4996 | struct ust_app_map *ua_map, struct ltt_ust_event *uevent, | |
4997 | struct ust_app *app) | |
4998 | { | |
4999 | int ret = 0; | |
5000 | uint64_t map_reg_key; | |
5001 | struct ust_app_event *ua_event; | |
5002 | struct ust_registry_session *registry; | |
5003 | ||
5004 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
5005 | if (ua_event == NULL) { | |
5006 | /* Only failure mode of alloc_ust_app_event(). */ | |
5007 | ret = -ENOMEM; | |
5008 | goto end; | |
5009 | } | |
5010 | shadow_copy_event(ua_event, uevent); | |
5011 | ||
5012 | registry = get_session_registry(ua_sess); | |
5013 | if (!registry) { | |
5014 | DBG("Application session is being torn down. Abort event notify"); | |
5015 | ret = 0; | |
5016 | goto error; | |
5017 | } | |
5018 | ||
5019 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
5020 | map_reg_key = ua_map->tracing_map_id; | |
5021 | } else { | |
5022 | map_reg_key = ua_map->key; | |
5023 | } | |
5024 | ||
5025 | pthread_mutex_lock(®istry->lock); | |
5026 | ret = ust_registry_map_add_token_key_mapping(registry, map_reg_key, | |
5027 | uevent->attr.token, uevent->key); | |
5028 | assert(ret == 0); | |
5029 | pthread_mutex_unlock(®istry->lock); | |
5030 | ||
5031 | /* Create it on the tracer side */ | |
5032 | ret = create_ust_map_event(app, ua_sess, ua_map, uevent->key, ua_event); | |
5033 | if (ret < 0) { | |
5034 | /* | |
5035 | * Not found previously means that it does not exist on the | |
5036 | * tracer. If the application reports that the event existed, | |
5037 | * it means there is a bug in the sessiond or lttng-ust | |
5038 | * (or corruption, etc.) | |
5039 | */ | |
5040 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
5041 | ERR("Tracer for application reported that an event being created already existed: " | |
5042 | "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
5043 | uevent->attr.name, | |
5044 | app->pid, app->ppid, app->uid, | |
5045 | app->gid); | |
5046 | } | |
5047 | ||
5048 | /* | |
5049 | * FIXME: frdeso: remove key from tokey->key mapping. | |
5050 | */ | |
5051 | goto error; | |
5052 | } | |
5053 | ||
5054 | add_unique_ust_app_event(ua_map->events, ua_event); | |
5055 | ||
5056 | DBG2("UST app create event completed: app = '%s', tracer token = %"PRIu64" (ppid: %d)", | |
5057 | app->name, uevent->attr.token, app->ppid); | |
5058 | ||
5059 | end: | |
5060 | return ret; | |
5061 | ||
5062 | error: | |
5063 | /* Valid. Calling here is already in a read side lock */ | |
5064 | delete_ust_app_event(-1, ua_event, app); | |
5065 | return ret; | |
5066 | } | |
5067 | ||
993578ff JR |
5068 | /* |
5069 | * Create UST app event notifier rule and create it on the tracer side. | |
5070 | * | |
5071 | * Must be called with the RCU read side lock held. | |
5072 | * Called with ust app session mutex held. | |
5073 | */ | |
5074 | static | |
267d66aa JR |
5075 | int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, |
5076 | struct ust_app *app) | |
993578ff JR |
5077 | { |
5078 | int ret = 0; | |
5079 | struct ust_app_event_notifier_rule *ua_event_notifier_rule; | |
5080 | ||
267d66aa | 5081 | ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger); |
993578ff JR |
5082 | if (ua_event_notifier_rule == NULL) { |
5083 | ret = -ENOMEM; | |
5084 | goto end; | |
5085 | } | |
5086 | ||
5087 | /* Create it on the tracer side. */ | |
5088 | ret = create_ust_event_notifier(app, ua_event_notifier_rule); | |
5089 | if (ret < 0) { | |
5090 | /* | |
5091 | * Not found previously means that it does not exist on the | |
5092 | * tracer. If the application reports that the event existed, | |
5093 | * it means there is a bug in the sessiond or lttng-ust | |
5094 | * (or corruption, etc.) | |
5095 | */ | |
5096 | if (ret == -LTTNG_UST_ERR_EXIST) { | |
5097 | ERR("Tracer for application reported that an event notifier being created already exists: " | |
5098 | "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d", | |
267d66aa | 5099 | lttng_trigger_get_tracer_token(trigger), |
993578ff JR |
5100 | app->pid, app->ppid, app->uid, |
5101 | app->gid); | |
5102 | } | |
5103 | goto error; | |
5104 | } | |
5105 | ||
5106 | lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht, | |
5107 | &ua_event_notifier_rule->node); | |
5108 | ||
5109 | DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64, | |
267d66aa | 5110 | app->name, app->ppid, lttng_trigger_get_tracer_token(trigger)); |
993578ff | 5111 | |
ebdb334b | 5112 | goto end; |
993578ff JR |
5113 | |
5114 | error: | |
5115 | /* The RCU read side lock is already being held by the caller. */ | |
5116 | delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app); | |
ebdb334b | 5117 | end: |
993578ff JR |
5118 | return ret; |
5119 | } | |
5120 | ||
5b4a0ec0 DG |
5121 | /* |
5122 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 5123 | * |
7972aab2 | 5124 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
5125 | */ |
5126 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 5127 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
5128 | { |
5129 | int ret = 0; | |
ffe60014 | 5130 | struct ust_app_channel *metadata; |
d88aee68 | 5131 | struct consumer_socket *socket; |
7972aab2 | 5132 | struct ust_registry_session *registry; |
e32d7f27 | 5133 | struct ltt_session *session = NULL; |
5b4a0ec0 | 5134 | |
ffe60014 DG |
5135 | assert(ua_sess); |
5136 | assert(app); | |
d88aee68 | 5137 | assert(consumer); |
5b4a0ec0 | 5138 | |
7972aab2 | 5139 | registry = get_session_registry(ua_sess); |
fad1ed2f | 5140 | /* The UST app session is held registry shall not be null. */ |
7972aab2 DG |
5141 | assert(registry); |
5142 | ||
ce34fcd0 MD |
5143 | pthread_mutex_lock(®istry->lock); |
5144 | ||
1b532a60 DG |
5145 | /* Metadata already exists for this registry or it was closed previously */ |
5146 | if (registry->metadata_key || registry->metadata_closed) { | |
7972aab2 DG |
5147 | ret = 0; |
5148 | goto error; | |
5b4a0ec0 DG |
5149 | } |
5150 | ||
ffe60014 | 5151 | /* Allocate UST metadata */ |
d0b96690 | 5152 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
5153 | if (!metadata) { |
5154 | /* malloc() failed */ | |
5155 | ret = -ENOMEM; | |
5156 | goto error; | |
5157 | } | |
5b4a0ec0 | 5158 | |
ad7a9107 | 5159 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 5160 | |
7972aab2 DG |
5161 | /* Need one fd for the channel. */ |
5162 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
5163 | if (ret < 0) { | |
5164 | ERR("Exhausted number of available FD upon create metadata"); | |
5165 | goto error; | |
5166 | } | |
5167 | ||
4dc3dfc5 DG |
5168 | /* Get the right consumer socket for the application. */ |
5169 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); | |
5170 | if (!socket) { | |
5171 | ret = -EINVAL; | |
5172 | goto error_consumer; | |
5173 | } | |
5174 | ||
331744e3 JD |
5175 | /* |
5176 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
5177 | * to the registry *before* we ask the consumer so we avoid the race of the | |
5178 | * consumer requesting the metadata and the ask_channel call on our side | |
5179 | * did not returned yet. | |
5180 | */ | |
5181 | registry->metadata_key = metadata->key; | |
5182 | ||
e098433c JG |
5183 | session = session_find_by_id(ua_sess->tracing_id); |
5184 | assert(session); | |
5185 | ||
5186 | assert(pthread_mutex_trylock(&session->lock)); | |
5187 | assert(session_trylock_list()); | |
5188 | ||
d88aee68 DG |
5189 | /* |
5190 | * Ask the metadata channel creation to the consumer. The metadata object | |
5191 | * will be created by the consumer and kept their. However, the stream is | |
5192 | * never added or monitored until we do a first push metadata to the | |
5193 | * consumer. | |
5194 | */ | |
7972aab2 | 5195 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
d2956687 | 5196 | registry, session->current_trace_chunk); |
d88aee68 | 5197 | if (ret < 0) { |
f2a444f1 DG |
5198 | /* Nullify the metadata key so we don't try to close it later on. */ |
5199 | registry->metadata_key = 0; | |
d88aee68 DG |
5200 | goto error_consumer; |
5201 | } | |
5202 | ||
5203 | /* | |
5204 | * The setup command will make the metadata stream be sent to the relayd, | |
5205 | * if applicable, and the thread managing the metadatas. This is important | |
5206 | * because after this point, if an error occurs, the only way the stream | |
5207 | * can be deleted is to be monitored in the consumer. | |
5208 | */ | |
7972aab2 | 5209 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 5210 | if (ret < 0) { |
f2a444f1 DG |
5211 | /* Nullify the metadata key so we don't try to close it later on. */ |
5212 | registry->metadata_key = 0; | |
d88aee68 | 5213 | goto error_consumer; |
5b4a0ec0 DG |
5214 | } |
5215 | ||
7972aab2 DG |
5216 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
5217 | metadata->key, app->pid); | |
5b4a0ec0 | 5218 | |
d88aee68 | 5219 | error_consumer: |
b80f0b6c | 5220 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 5221 | delete_ust_app_channel(-1, metadata, app); |
5b4a0ec0 | 5222 | error: |
ce34fcd0 | 5223 | pthread_mutex_unlock(®istry->lock); |
e32d7f27 JG |
5224 | if (session) { |
5225 | session_put(session); | |
5226 | } | |
ffe60014 | 5227 | return ret; |
5b4a0ec0 DG |
5228 | } |
5229 | ||
5b4a0ec0 | 5230 | /* |
d88aee68 DG |
5231 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
5232 | * acquired before calling this function. | |
5b4a0ec0 DG |
5233 | */ |
5234 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
5235 | { | |
d88aee68 | 5236 | struct ust_app *app = NULL; |
bec39940 DG |
5237 | struct lttng_ht_node_ulong *node; |
5238 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 5239 | |
bec39940 DG |
5240 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
5241 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
5242 | if (node == NULL) { |
5243 | DBG2("UST app no found with pid %d", pid); | |
5244 | goto error; | |
5245 | } | |
5b4a0ec0 DG |
5246 | |
5247 | DBG2("Found UST app by pid %d", pid); | |
5248 | ||
d88aee68 | 5249 | app = caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
5250 | |
5251 | error: | |
d88aee68 | 5252 | return app; |
5b4a0ec0 DG |
5253 | } |
5254 | ||
d88aee68 DG |
5255 | /* |
5256 | * Allocate and init an UST app object using the registration information and | |
5257 | * the command socket. This is called when the command socket connects to the | |
5258 | * session daemon. | |
5259 | * | |
5260 | * The object is returned on success or else NULL. | |
5261 | */ | |
d0b96690 | 5262 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 5263 | { |
5e2abfaf | 5264 | int ret; |
d0b96690 | 5265 | struct ust_app *lta = NULL; |
da873412 | 5266 | struct lttng_pipe *event_notifier_event_source_pipe = NULL; |
d0b96690 DG |
5267 | |
5268 | assert(msg); | |
5269 | assert(sock >= 0); | |
5270 | ||
5271 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 5272 | |
173af62f DG |
5273 | if ((msg->bits_per_long == 64 && |
5274 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
5275 | || (msg->bits_per_long == 32 && | |
5276 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 5277 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
5278 | "%d-bit long, but no consumerd for this size is available.\n", |
5279 | msg->name, msg->pid, msg->bits_per_long); | |
5280 | goto error; | |
3f2c5fcc | 5281 | } |
d0b96690 | 5282 | |
5e2abfaf JG |
5283 | /* |
5284 | * Reserve the two file descriptors of the event source pipe. The write | |
5285 | * end will be closed once it is passed to the application, at which | |
5286 | * point a single 'put' will be performed. | |
5287 | */ | |
5288 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
5289 | if (ret) { | |
5290 | ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)", | |
5291 | msg->name, (int) msg->ppid); | |
5292 | goto error; | |
5293 | } | |
5294 | ||
da873412 JR |
5295 | event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC); |
5296 | if (!event_notifier_event_source_pipe) { | |
5297 | PERROR("Failed to open application event source pipe: '%s' (ppid = %d)", | |
5298 | msg->name, msg->ppid); | |
5299 | goto error; | |
5300 | } | |
5301 | ||
5b4a0ec0 DG |
5302 | lta = zmalloc(sizeof(struct ust_app)); |
5303 | if (lta == NULL) { | |
5304 | PERROR("malloc"); | |
da873412 | 5305 | goto error_free_pipe; |
5b4a0ec0 DG |
5306 | } |
5307 | ||
da873412 JR |
5308 | lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe; |
5309 | ||
5b4a0ec0 DG |
5310 | lta->ppid = msg->ppid; |
5311 | lta->uid = msg->uid; | |
5312 | lta->gid = msg->gid; | |
d0b96690 | 5313 | |
7753dea8 | 5314 | lta->bits_per_long = msg->bits_per_long; |
d0b96690 DG |
5315 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
5316 | lta->uint16_t_alignment = msg->uint16_t_alignment; | |
5317 | lta->uint32_t_alignment = msg->uint32_t_alignment; | |
5318 | lta->uint64_t_alignment = msg->uint64_t_alignment; | |
5319 | lta->long_alignment = msg->long_alignment; | |
5320 | lta->byte_order = msg->byte_order; | |
5321 | ||
5b4a0ec0 DG |
5322 | lta->v_major = msg->major; |
5323 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 5324 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
ebdb334b JR |
5325 | lta->ust_chan_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
5326 | lta->ust_map_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); | |
10b56aef | 5327 | lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
d0b96690 | 5328 | lta->notify_sock = -1; |
993578ff | 5329 | lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d88aee68 DG |
5330 | |
5331 | /* Copy name and make sure it's NULL terminated. */ | |
5332 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
5333 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
5334 | ||
5335 | /* | |
5336 | * Before this can be called, when receiving the registration information, | |
5337 | * the application compatibility is checked. So, at this point, the | |
5338 | * application can work with this session daemon. | |
5339 | */ | |
d0b96690 | 5340 | lta->compatible = 1; |
5b4a0ec0 | 5341 | |
852d0037 | 5342 | lta->pid = msg->pid; |
d0b96690 | 5343 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 5344 | lta->sock = sock; |
fb45065e | 5345 | pthread_mutex_init(<a->sock_lock, NULL); |
d0b96690 | 5346 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 5347 | |
d42f20df | 5348 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
d0b96690 | 5349 | return lta; |
da873412 JR |
5350 | |
5351 | error_free_pipe: | |
5352 | lttng_pipe_destroy(event_notifier_event_source_pipe); | |
5e2abfaf | 5353 | lttng_fd_put(LTTNG_FD_APPS, 2); |
da873412 JR |
5354 | error: |
5355 | return NULL; | |
d0b96690 DG |
5356 | } |
5357 | ||
d88aee68 DG |
5358 | /* |
5359 | * For a given application object, add it to every hash table. | |
5360 | */ | |
d0b96690 DG |
5361 | void ust_app_add(struct ust_app *app) |
5362 | { | |
5363 | assert(app); | |
5364 | assert(app->notify_sock >= 0); | |
5365 | ||
940c4592 JR |
5366 | app->registration_time = time(NULL); |
5367 | ||
5b4a0ec0 | 5368 | rcu_read_lock(); |
852d0037 DG |
5369 | |
5370 | /* | |
5371 | * On a re-registration, we want to kick out the previous registration of | |
5372 | * that pid | |
5373 | */ | |
d0b96690 | 5374 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
5375 | |
5376 | /* | |
5377 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
5378 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
5379 | * already in the table. | |
5380 | */ | |
d0b96690 | 5381 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 5382 | |
d0b96690 DG |
5383 | /* Add application to the notify socket hash table. */ |
5384 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
5385 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 5386 | |
d0b96690 | 5387 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
d88aee68 DG |
5388 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
5389 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, | |
5390 | app->v_minor); | |
5b4a0ec0 | 5391 | |
d0b96690 DG |
5392 | rcu_read_unlock(); |
5393 | } | |
5394 | ||
d88aee68 DG |
5395 | /* |
5396 | * Set the application version into the object. | |
5397 | * | |
5398 | * Return 0 on success else a negative value either an errno code or a | |
5399 | * LTTng-UST error code. | |
5400 | */ | |
d0b96690 DG |
5401 | int ust_app_version(struct ust_app *app) |
5402 | { | |
d88aee68 DG |
5403 | int ret; |
5404 | ||
d0b96690 | 5405 | assert(app); |
d88aee68 | 5406 | |
fb45065e | 5407 | pthread_mutex_lock(&app->sock_lock); |
d88aee68 | 5408 | ret = ustctl_tracer_version(app->sock, &app->version); |
fb45065e | 5409 | pthread_mutex_unlock(&app->sock_lock); |
d88aee68 DG |
5410 | if (ret < 0) { |
5411 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
5368d366 | 5412 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
d88aee68 | 5413 | } else { |
5368d366 | 5414 | DBG3("UST app %d version failed. Application is dead", app->sock); |
d88aee68 DG |
5415 | } |
5416 | } | |
5417 | ||
5418 | return ret; | |
5b4a0ec0 DG |
5419 | } |
5420 | ||
da873412 JR |
5421 | /* |
5422 | * Setup the base event notifier group. | |
5423 | * | |
5424 | * Return 0 on success else a negative value either an errno code or a | |
5425 | * LTTng-UST error code. | |
5426 | */ | |
5427 | int ust_app_setup_event_notifier_group(struct ust_app *app) | |
5428 | { | |
5429 | int ret; | |
5430 | int event_pipe_write_fd; | |
5431 | struct lttng_ust_object_data *event_notifier_group = NULL; | |
5432 | enum lttng_error_code lttng_ret; | |
ebdb334b | 5433 | enum event_notifier_error_accounting_status event_notifier_error_accounting_status; |
da873412 JR |
5434 | |
5435 | assert(app); | |
5436 | ||
5437 | /* Get the write side of the pipe. */ | |
5438 | event_pipe_write_fd = lttng_pipe_get_writefd( | |
5439 | app->event_notifier_group.event_pipe); | |
5440 | ||
5441 | pthread_mutex_lock(&app->sock_lock); | |
5442 | ret = ustctl_create_event_notifier_group(app->sock, | |
5443 | event_pipe_write_fd, &event_notifier_group); | |
5444 | pthread_mutex_unlock(&app->sock_lock); | |
5445 | if (ret < 0) { | |
5446 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
5447 | ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d", | |
5448 | ret, app->sock, event_pipe_write_fd); | |
5449 | } else { | |
5450 | DBG("Failed to create application event notifier group (application is dead): app socket fd = %d", | |
5451 | app->sock); | |
5452 | } | |
5453 | ||
5454 | goto error; | |
5455 | } | |
5456 | ||
5d4193fd JG |
5457 | ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe); |
5458 | if (ret) { | |
5459 | ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)", | |
5460 | app->name, app->ppid); | |
5461 | goto error; | |
5462 | } | |
5463 | ||
5e2abfaf JG |
5464 | /* |
5465 | * Release the file descriptor that was reserved for the write-end of | |
5466 | * the pipe. | |
5467 | */ | |
5468 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
5469 | ||
da873412 JR |
5470 | lttng_ret = notification_thread_command_add_tracer_event_source( |
5471 | notification_thread_handle, | |
5472 | lttng_pipe_get_readfd(app->event_notifier_group.event_pipe), | |
5473 | LTTNG_DOMAIN_UST); | |
5474 | if (lttng_ret != LTTNG_OK) { | |
5475 | ERR("Failed to add tracer event source to notification thread"); | |
5476 | ret = - 1; | |
5477 | goto error; | |
5478 | } | |
5479 | ||
5480 | /* Assign handle only when the complete setup is valid. */ | |
5481 | app->event_notifier_group.object = event_notifier_group; | |
ebdb334b JR |
5482 | |
5483 | event_notifier_error_accounting_status = event_notifier_error_accounting_register_app(app); | |
5484 | if (event_notifier_error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
5485 | ERR("Failed to setup event notifier error accounting for app"); | |
5486 | ret = -1; | |
5487 | goto error; | |
5488 | } | |
5489 | ||
da873412 JR |
5490 | return ret; |
5491 | ||
5492 | error: | |
5493 | ustctl_release_object(app->sock, app->event_notifier_group.object); | |
5494 | free(app->event_notifier_group.object); | |
5495 | return ret; | |
5496 | } | |
5497 | ||
5b4a0ec0 DG |
5498 | /* |
5499 | * Unregister app by removing it from the global traceable app list and freeing | |
5500 | * the data struct. | |
5501 | * | |
5502 | * The socket is already closed at this point so no close to sock. | |
5503 | */ | |
5504 | void ust_app_unregister(int sock) | |
5505 | { | |
5506 | struct ust_app *lta; | |
bec39940 | 5507 | struct lttng_ht_node_ulong *node; |
c4b88406 | 5508 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 5509 | struct lttng_ht_iter iter; |
d42f20df | 5510 | struct ust_app_session *ua_sess; |
525b0740 | 5511 | int ret; |
5b4a0ec0 DG |
5512 | |
5513 | rcu_read_lock(); | |
886459c6 | 5514 | |
5b4a0ec0 | 5515 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
5516 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
5517 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
d0b96690 | 5518 | assert(node); |
284d8f55 | 5519 | |
852d0037 | 5520 | lta = caa_container_of(node, struct ust_app, sock_n); |
852d0037 DG |
5521 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
5522 | ||
d88aee68 | 5523 | /* |
ce34fcd0 MD |
5524 | * For per-PID buffers, perform "push metadata" and flush all |
5525 | * application streams before removing app from hash tables, | |
5526 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 5527 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 5528 | */ |
d42f20df DG |
5529 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
5530 | node.node) { | |
7972aab2 DG |
5531 | struct ust_registry_session *registry; |
5532 | ||
d42f20df DG |
5533 | ret = lttng_ht_del(lta->sessions, &iter); |
5534 | if (ret) { | |
5535 | /* The session was already removed so scheduled for teardown. */ | |
5536 | continue; | |
5537 | } | |
5538 | ||
ce34fcd0 MD |
5539 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
5540 | (void) ust_app_flush_app_session(lta, ua_sess); | |
5541 | } | |
c4b88406 | 5542 | |
d42f20df DG |
5543 | /* |
5544 | * Add session to list for teardown. This is safe since at this point we | |
5545 | * are the only one using this list. | |
5546 | */ | |
d88aee68 DG |
5547 | pthread_mutex_lock(&ua_sess->lock); |
5548 | ||
b161602a MD |
5549 | if (ua_sess->deleted) { |
5550 | pthread_mutex_unlock(&ua_sess->lock); | |
5551 | continue; | |
5552 | } | |
5553 | ||
d88aee68 DG |
5554 | /* |
5555 | * Normally, this is done in the delete session process which is | |
5556 | * executed in the call rcu below. However, upon registration we can't | |
5557 | * afford to wait for the grace period before pushing data or else the | |
5558 | * data pending feature can race between the unregistration and stop | |
5559 | * command where the data pending command is sent *before* the grace | |
5560 | * period ended. | |
5561 | * | |
5562 | * The close metadata below nullifies the metadata pointer in the | |
5563 | * session so the delete session will NOT push/close a second time. | |
5564 | */ | |
7972aab2 | 5565 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 5566 | if (registry) { |
7972aab2 DG |
5567 | /* Push metadata for application before freeing the application. */ |
5568 | (void) push_metadata(registry, ua_sess->consumer); | |
5569 | ||
5570 | /* | |
5571 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
5572 | * metadata only on destroy trace session in this case. Also, the |
5573 | * previous push metadata could have flag the metadata registry to | |
5574 | * close so don't send a close command if closed. | |
7972aab2 | 5575 | */ |
ce34fcd0 | 5576 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
5577 | /* And ask to close it for this session registry. */ |
5578 | (void) close_metadata(registry, ua_sess->consumer); | |
5579 | } | |
5580 | } | |
d42f20df | 5581 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
c4b88406 | 5582 | |
d88aee68 | 5583 | pthread_mutex_unlock(&ua_sess->lock); |
d42f20df DG |
5584 | } |
5585 | ||
c4b88406 MD |
5586 | /* Remove application from PID hash table */ |
5587 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); | |
5588 | assert(!ret); | |
5589 | ||
5590 | /* | |
5591 | * Remove application from notify hash table. The thread handling the | |
5592 | * notify socket could have deleted the node so ignore on error because | |
c48239ca JG |
5593 | * either way it's valid. The close of that socket is handled by the |
5594 | * apps_notify_thread. | |
c4b88406 MD |
5595 | */ |
5596 | iter.iter.node = <a->notify_sock_n.node; | |
5597 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
5598 | ||
5599 | /* | |
5600 | * Ignore return value since the node might have been removed before by an | |
5601 | * add replace during app registration because the PID can be reassigned by | |
5602 | * the OS. | |
5603 | */ | |
5604 | iter.iter.node = <a->pid_n.node; | |
5605 | ret = lttng_ht_del(ust_app_ht, &iter); | |
5606 | if (ret) { | |
5607 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
5608 | lta->pid); | |
5609 | } | |
5610 | ||
852d0037 DG |
5611 | /* Free memory */ |
5612 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
5613 | ||
5b4a0ec0 DG |
5614 | rcu_read_unlock(); |
5615 | return; | |
284d8f55 DG |
5616 | } |
5617 | ||
5b4a0ec0 DG |
5618 | /* |
5619 | * Fill events array with all events name of all registered apps. | |
5620 | */ | |
5621 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 5622 | { |
5b4a0ec0 DG |
5623 | int ret, handle; |
5624 | size_t nbmem, count = 0; | |
bec39940 | 5625 | struct lttng_ht_iter iter; |
5b4a0ec0 | 5626 | struct ust_app *app; |
c617c0c6 | 5627 | struct lttng_event *tmp_event; |
421cb601 | 5628 | |
5b4a0ec0 | 5629 | nbmem = UST_APP_EVENT_LIST_SIZE; |
c617c0c6 MD |
5630 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
5631 | if (tmp_event == NULL) { | |
5b4a0ec0 DG |
5632 | PERROR("zmalloc ust app events"); |
5633 | ret = -ENOMEM; | |
421cb601 DG |
5634 | goto error; |
5635 | } | |
5636 | ||
5b4a0ec0 | 5637 | rcu_read_lock(); |
421cb601 | 5638 | |
852d0037 | 5639 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
90eaa0d2 | 5640 | struct lttng_ust_tracepoint_iter uiter; |
ac3bd9c0 | 5641 | |
840cb59c | 5642 | health_code_update(); |
86acf0da | 5643 | |
e0c7ec2b DG |
5644 | if (!app->compatible) { |
5645 | /* | |
5646 | * TODO: In time, we should notice the caller of this error by | |
5647 | * telling him that this is a version error. | |
5648 | */ | |
5649 | continue; | |
5650 | } | |
fb45065e | 5651 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 5652 | handle = ustctl_tracepoint_list(app->sock); |
5b4a0ec0 | 5653 | if (handle < 0) { |
ffe60014 DG |
5654 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
5655 | ERR("UST app list events getting handle failed for app pid %d", | |
5656 | app->pid); | |
5657 | } | |
fb45065e | 5658 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 DG |
5659 | continue; |
5660 | } | |
421cb601 | 5661 | |
852d0037 | 5662 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 5663 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
5664 | /* Handle ustctl error. */ |
5665 | if (ret < 0) { | |
fb45065e MD |
5666 | int release_ret; |
5667 | ||
a2ba1ab0 | 5668 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
5669 | ERR("UST app tp list get failed for app %d with ret %d", |
5670 | app->sock, ret); | |
5671 | } else { | |
5672 | DBG3("UST app tp list get failed. Application is dead"); | |
3757b385 DG |
5673 | /* |
5674 | * This is normal behavior, an application can die during the | |
5675 | * creation process. Don't report an error so the execution can | |
5676 | * continue normally. Continue normal execution. | |
5677 | */ | |
5678 | break; | |
ffe60014 | 5679 | } |
98f595d4 | 5680 | free(tmp_event); |
fb45065e | 5681 | release_ret = ustctl_release_handle(app->sock, handle); |
68313703 JG |
5682 | if (release_ret < 0 && |
5683 | release_ret != -LTTNG_UST_ERR_EXITING && | |
5684 | release_ret != -EPIPE) { | |
fb45065e MD |
5685 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
5686 | } | |
5687 | pthread_mutex_unlock(&app->sock_lock); | |
ffe60014 DG |
5688 | goto rcu_error; |
5689 | } | |
5690 | ||
840cb59c | 5691 | health_code_update(); |
815564d8 | 5692 | if (count >= nbmem) { |
d7b3776f | 5693 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
5694 | struct lttng_event *new_tmp_event; |
5695 | size_t new_nbmem; | |
5696 | ||
5697 | new_nbmem = nbmem << 1; | |
5698 | DBG2("Reallocating event list from %zu to %zu entries", | |
5699 | nbmem, new_nbmem); | |
5700 | new_tmp_event = realloc(tmp_event, | |
5701 | new_nbmem * sizeof(struct lttng_event)); | |
5702 | if (new_tmp_event == NULL) { | |
fb45065e MD |
5703 | int release_ret; |
5704 | ||
5b4a0ec0 | 5705 | PERROR("realloc ust app events"); |
c617c0c6 | 5706 | free(tmp_event); |
5b4a0ec0 | 5707 | ret = -ENOMEM; |
fb45065e | 5708 | release_ret = ustctl_release_handle(app->sock, handle); |
68313703 JG |
5709 | if (release_ret < 0 && |
5710 | release_ret != -LTTNG_UST_ERR_EXITING && | |
5711 | release_ret != -EPIPE) { | |
fb45065e MD |
5712 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
5713 | } | |
5714 | pthread_mutex_unlock(&app->sock_lock); | |
5b4a0ec0 DG |
5715 | goto rcu_error; |
5716 | } | |
53efb85a MD |
5717 | /* Zero the new memory */ |
5718 | memset(new_tmp_event + nbmem, 0, | |
5719 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
5720 | nbmem = new_nbmem; | |
5721 | tmp_event = new_tmp_event; | |
5b4a0ec0 | 5722 | } |
c617c0c6 MD |
5723 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
5724 | tmp_event[count].loglevel = uiter.loglevel; | |
5725 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; | |
5726 | tmp_event[count].pid = app->pid; | |
5727 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 5728 | count++; |
421cb601 | 5729 | } |
fb45065e MD |
5730 | ret = ustctl_release_handle(app->sock, handle); |
5731 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 | 5732 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
fb45065e MD |
5733 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
5734 | } | |
421cb601 DG |
5735 | } |
5736 | ||
5b4a0ec0 | 5737 | ret = count; |
c617c0c6 | 5738 | *events = tmp_event; |
421cb601 | 5739 | |
5b4a0ec0 | 5740 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 5741 | |
5b4a0ec0 DG |
5742 | rcu_error: |
5743 | rcu_read_unlock(); | |
421cb601 | 5744 | error: |
840cb59c | 5745 | health_code_update(); |
5b4a0ec0 | 5746 | return ret; |
421cb601 DG |
5747 | } |
5748 | ||
f37d259d MD |
5749 | /* |
5750 | * Fill events array with all events name of all registered apps. | |
5751 | */ | |
5752 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
5753 | { | |
5754 | int ret, handle; | |
5755 | size_t nbmem, count = 0; | |
5756 | struct lttng_ht_iter iter; | |
5757 | struct ust_app *app; | |
c617c0c6 | 5758 | struct lttng_event_field *tmp_event; |
f37d259d MD |
5759 | |
5760 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
c617c0c6 MD |
5761 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
5762 | if (tmp_event == NULL) { | |
f37d259d MD |
5763 | PERROR("zmalloc ust app event fields"); |
5764 | ret = -ENOMEM; | |
5765 | goto error; | |
5766 | } | |
5767 | ||
5768 | rcu_read_lock(); | |
5769 | ||
5770 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5771 | struct lttng_ust_field_iter uiter; | |
5772 | ||
840cb59c | 5773 | health_code_update(); |
86acf0da | 5774 | |
f37d259d MD |
5775 | if (!app->compatible) { |
5776 | /* | |
5777 | * TODO: In time, we should notice the caller of this error by | |
5778 | * telling him that this is a version error. | |
5779 | */ | |
5780 | continue; | |
5781 | } | |
fb45065e | 5782 | pthread_mutex_lock(&app->sock_lock); |
f37d259d MD |
5783 | handle = ustctl_tracepoint_field_list(app->sock); |
5784 | if (handle < 0) { | |
ffe60014 DG |
5785 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
5786 | ERR("UST app list field getting handle failed for app pid %d", | |
5787 | app->pid); | |
5788 | } | |
fb45065e | 5789 | pthread_mutex_unlock(&app->sock_lock); |
f37d259d MD |
5790 | continue; |
5791 | } | |
5792 | ||
5793 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, | |
fb54cdbf | 5794 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
5795 | /* Handle ustctl error. */ |
5796 | if (ret < 0) { | |
fb45065e MD |
5797 | int release_ret; |
5798 | ||
a2ba1ab0 | 5799 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
5800 | ERR("UST app tp list field failed for app %d with ret %d", |
5801 | app->sock, ret); | |
5802 | } else { | |
5803 | DBG3("UST app tp list field failed. Application is dead"); | |
3757b385 DG |
5804 | /* |
5805 | * This is normal behavior, an application can die during the | |
5806 | * creation process. Don't report an error so the execution can | |
98f595d4 | 5807 | * continue normally. Reset list and count for next app. |
3757b385 DG |
5808 | */ |
5809 | break; | |
ffe60014 | 5810 | } |
98f595d4 | 5811 | free(tmp_event); |
fb45065e MD |
5812 | release_ret = ustctl_release_handle(app->sock, handle); |
5813 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
5814 | if (release_ret < 0 && |
5815 | release_ret != -LTTNG_UST_ERR_EXITING && | |
5816 | release_ret != -EPIPE) { | |
fb45065e MD |
5817 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
5818 | } | |
ffe60014 DG |
5819 | goto rcu_error; |
5820 | } | |
5821 | ||
840cb59c | 5822 | health_code_update(); |
f37d259d | 5823 | if (count >= nbmem) { |
d7b3776f | 5824 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
5825 | struct lttng_event_field *new_tmp_event; |
5826 | size_t new_nbmem; | |
5827 | ||
5828 | new_nbmem = nbmem << 1; | |
5829 | DBG2("Reallocating event field list from %zu to %zu entries", | |
5830 | nbmem, new_nbmem); | |
5831 | new_tmp_event = realloc(tmp_event, | |
5832 | new_nbmem * sizeof(struct lttng_event_field)); | |
5833 | if (new_tmp_event == NULL) { | |
fb45065e MD |
5834 | int release_ret; |
5835 | ||
f37d259d | 5836 | PERROR("realloc ust app event fields"); |
c617c0c6 | 5837 | free(tmp_event); |
f37d259d | 5838 | ret = -ENOMEM; |
fb45065e MD |
5839 | release_ret = ustctl_release_handle(app->sock, handle); |
5840 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
5841 | if (release_ret && |
5842 | release_ret != -LTTNG_UST_ERR_EXITING && | |
5843 | release_ret != -EPIPE) { | |
fb45065e MD |
5844 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
5845 | } | |
f37d259d MD |
5846 | goto rcu_error; |
5847 | } | |
53efb85a MD |
5848 | /* Zero the new memory */ |
5849 | memset(new_tmp_event + nbmem, 0, | |
5850 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); | |
5851 | nbmem = new_nbmem; | |
5852 | tmp_event = new_tmp_event; | |
f37d259d | 5853 | } |
f37d259d | 5854 | |
c617c0c6 | 5855 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
2e84128e DG |
5856 | /* Mapping between these enums matches 1 to 1. */ |
5857 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; | |
c617c0c6 | 5858 | tmp_event[count].nowrite = uiter.nowrite; |
f37d259d | 5859 | |
c617c0c6 MD |
5860 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
5861 | tmp_event[count].event.loglevel = uiter.loglevel; | |
2e84128e | 5862 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
c617c0c6 MD |
5863 | tmp_event[count].event.pid = app->pid; |
5864 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
5865 | count++; |
5866 | } | |
fb45065e MD |
5867 | ret = ustctl_release_handle(app->sock, handle); |
5868 | pthread_mutex_unlock(&app->sock_lock); | |
68313703 JG |
5869 | if (ret < 0 && |
5870 | ret != -LTTNG_UST_ERR_EXITING && | |
5871 | ret != -EPIPE) { | |
fb45065e MD |
5872 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
5873 | } | |
f37d259d MD |
5874 | } |
5875 | ||
5876 | ret = count; | |
c617c0c6 | 5877 | *fields = tmp_event; |
f37d259d MD |
5878 | |
5879 | DBG2("UST app list event fields done (%zu events)", count); | |
5880 | ||
5881 | rcu_error: | |
5882 | rcu_read_unlock(); | |
5883 | error: | |
840cb59c | 5884 | health_code_update(); |
f37d259d MD |
5885 | return ret; |
5886 | } | |
5887 | ||
5b4a0ec0 DG |
5888 | /* |
5889 | * Free and clean all traceable apps of the global list. | |
36b588ed MD |
5890 | * |
5891 | * Should _NOT_ be called with RCU read-side lock held. | |
5b4a0ec0 DG |
5892 | */ |
5893 | void ust_app_clean_list(void) | |
421cb601 | 5894 | { |
5b4a0ec0 | 5895 | int ret; |
659ed79f | 5896 | struct ust_app *app; |
bec39940 | 5897 | struct lttng_ht_iter iter; |
421cb601 | 5898 | |
5b4a0ec0 | 5899 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 5900 | |
5b4a0ec0 | 5901 | rcu_read_lock(); |
421cb601 | 5902 | |
faadaa3a JG |
5903 | /* Cleanup notify socket hash table */ |
5904 | if (ust_app_ht_by_notify_sock) { | |
5905 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, | |
5906 | notify_sock_n.node) { | |
b69a1b40 JG |
5907 | /* |
5908 | * Assert that all notifiers are gone as all triggers | |
5909 | * are unregistered prior to this clean-up. | |
5910 | */ | |
5911 | assert(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0); | |
faadaa3a | 5912 | |
faadaa3a JG |
5913 | ust_app_notify_sock_unregister(app->notify_sock); |
5914 | } | |
5915 | } | |
5916 | ||
f1b711c4 MD |
5917 | if (ust_app_ht) { |
5918 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5919 | ret = lttng_ht_del(ust_app_ht, &iter); | |
5920 | assert(!ret); | |
5921 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
5922 | } | |
421cb601 DG |
5923 | } |
5924 | ||
852d0037 | 5925 | /* Cleanup socket hash table */ |
f1b711c4 MD |
5926 | if (ust_app_ht_by_sock) { |
5927 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, | |
5928 | sock_n.node) { | |
5929 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); | |
5930 | assert(!ret); | |
5931 | } | |
bec39940 | 5932 | } |
852d0037 | 5933 | |
36b588ed | 5934 | rcu_read_unlock(); |
d88aee68 | 5935 | |
bec39940 | 5936 | /* Destroy is done only when the ht is empty */ |
f1b711c4 MD |
5937 | if (ust_app_ht) { |
5938 | ht_cleanup_push(ust_app_ht); | |
5939 | } | |
5940 | if (ust_app_ht_by_sock) { | |
5941 | ht_cleanup_push(ust_app_ht_by_sock); | |
5942 | } | |
5943 | if (ust_app_ht_by_notify_sock) { | |
5944 | ht_cleanup_push(ust_app_ht_by_notify_sock); | |
5945 | } | |
5b4a0ec0 DG |
5946 | } |
5947 | ||
5948 | /* | |
5949 | * Init UST app hash table. | |
5950 | */ | |
57703f6e | 5951 | int ust_app_ht_alloc(void) |
5b4a0ec0 | 5952 | { |
bec39940 | 5953 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
5954 | if (!ust_app_ht) { |
5955 | return -1; | |
5956 | } | |
852d0037 | 5957 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
5958 | if (!ust_app_ht_by_sock) { |
5959 | return -1; | |
5960 | } | |
d0b96690 | 5961 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
5962 | if (!ust_app_ht_by_notify_sock) { |
5963 | return -1; | |
5964 | } | |
5965 | return 0; | |
421cb601 DG |
5966 | } |
5967 | ||
78f0bacd DG |
5968 | /* |
5969 | * For a specific UST session, disable the channel for all registered apps. | |
5970 | */ | |
35a9059d | 5971 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
5972 | struct ltt_ust_channel *uchan) |
5973 | { | |
5974 | int ret = 0; | |
bec39940 DG |
5975 | struct lttng_ht_iter iter; |
5976 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
5977 | struct ust_app *app; |
5978 | struct ust_app_session *ua_sess; | |
8535a6d9 | 5979 | struct ust_app_channel *ua_chan; |
78f0bacd | 5980 | |
88e3c2f5 | 5981 | assert(usess->active); |
d9bf3ca4 | 5982 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
a991f516 | 5983 | uchan->name, usess->id); |
78f0bacd DG |
5984 | |
5985 | rcu_read_lock(); | |
5986 | ||
5987 | /* For every registered applications */ | |
852d0037 | 5988 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 5989 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
5990 | if (!app->compatible) { |
5991 | /* | |
5992 | * TODO: In time, we should notice the caller of this error by | |
5993 | * telling him that this is a version error. | |
5994 | */ | |
5995 | continue; | |
5996 | } | |
78f0bacd DG |
5997 | ua_sess = lookup_session_by_app(usess, app); |
5998 | if (ua_sess == NULL) { | |
5999 | continue; | |
6000 | } | |
6001 | ||
8535a6d9 | 6002 | /* Get channel */ |
bec39940 DG |
6003 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
6004 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 DG |
6005 | /* If the session if found for the app, the channel must be there */ |
6006 | assert(ua_chan_node); | |
6007 | ||
6008 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
6009 | /* The channel must not be already disabled */ | |
6010 | assert(ua_chan->enabled == 1); | |
6011 | ||
6012 | /* Disable channel onto application */ | |
6013 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
6014 | if (ret < 0) { |
6015 | /* XXX: We might want to report this error at some point... */ | |
6016 | continue; | |
6017 | } | |
6018 | } | |
6019 | ||
6020 | rcu_read_unlock(); | |
78f0bacd DG |
6021 | return ret; |
6022 | } | |
6023 | ||
6024 | /* | |
ebdb334b | 6025 | * For a specific UST session, disable the channel for all registered apps. |
78f0bacd | 6026 | */ |
ebdb334b JR |
6027 | int ust_app_disable_map_glb(struct ltt_ust_session *usess, |
6028 | struct ltt_ust_map *umap) | |
78f0bacd DG |
6029 | { |
6030 | int ret = 0; | |
bec39940 | 6031 | struct lttng_ht_iter iter; |
ebdb334b | 6032 | struct lttng_ht_node_str *ua_map_node; |
78f0bacd DG |
6033 | struct ust_app *app; |
6034 | struct ust_app_session *ua_sess; | |
ebdb334b | 6035 | struct ust_app_map *ua_map; |
78f0bacd | 6036 | |
88e3c2f5 | 6037 | assert(usess->active); |
ebdb334b JR |
6038 | DBG2("UST app disabling map %s from global domain for session id %" PRIu64, |
6039 | umap->name, usess->id); | |
78f0bacd DG |
6040 | |
6041 | rcu_read_lock(); | |
6042 | ||
6043 | /* For every registered applications */ | |
852d0037 | 6044 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
ebdb334b | 6045 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
6046 | if (!app->compatible) { |
6047 | /* | |
6048 | * TODO: In time, we should notice the caller of this error by | |
6049 | * telling him that this is a version error. | |
6050 | */ | |
6051 | continue; | |
6052 | } | |
78f0bacd DG |
6053 | ua_sess = lookup_session_by_app(usess, app); |
6054 | if (ua_sess == NULL) { | |
6055 | continue; | |
6056 | } | |
6057 | ||
ebdb334b JR |
6058 | /* Get map */ |
6059 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); | |
6060 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); | |
6061 | /* If the session if found for the app, the map must be there */ | |
6062 | assert(ua_map_node); | |
6063 | ||
6064 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
6065 | /* The map must not be already disabled */ | |
6066 | assert(ua_map->enabled == 1); | |
6067 | ||
6068 | /* Disable map onto application */ | |
6069 | ret = disable_ust_app_map(ua_sess, ua_map, app); | |
6070 | if (ret < 0) { | |
6071 | /* XXX: We might want to report this error at some point... */ | |
6072 | continue; | |
6073 | } | |
6074 | } | |
6075 | ||
6076 | rcu_read_unlock(); | |
6077 | return ret; | |
6078 | } | |
6079 | ||
6080 | /* | |
6081 | * For a specific UST session, enable the channel for all registered apps. | |
6082 | */ | |
6083 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, | |
6084 | struct ltt_ust_channel *uchan) | |
6085 | { | |
6086 | int ret = 0; | |
6087 | struct lttng_ht_iter iter; | |
6088 | struct ust_app *app; | |
6089 | struct ust_app_session *ua_sess; | |
6090 | ||
6091 | assert(usess->active); | |
6092 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, | |
6093 | uchan->name, usess->id); | |
6094 | ||
6095 | rcu_read_lock(); | |
6096 | ||
6097 | /* For every registered applications */ | |
6098 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6099 | if (!app->compatible) { | |
6100 | /* | |
6101 | * TODO: In time, we should notice the caller of this error by | |
6102 | * telling him that this is a version error. | |
6103 | */ | |
6104 | continue; | |
6105 | } | |
6106 | ua_sess = lookup_session_by_app(usess, app); | |
6107 | if (ua_sess == NULL) { | |
6108 | continue; | |
6109 | } | |
6110 | ||
6111 | /* Enable channel onto application */ | |
6112 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
6113 | if (ret < 0) { | |
6114 | /* XXX: We might want to report this error at some point... */ | |
6115 | continue; | |
6116 | } | |
6117 | } | |
6118 | ||
6119 | rcu_read_unlock(); | |
6120 | return ret; | |
6121 | } | |
6122 | ||
6123 | /* | |
6124 | * For a specific UST session, enable the map for all registered apps. | |
6125 | */ | |
6126 | int ust_app_enable_map_glb(struct ltt_ust_session *usess, | |
6127 | struct ltt_ust_map *umap) | |
6128 | { | |
6129 | int ret = 0; | |
6130 | struct lttng_ht_iter iter; | |
6131 | struct ust_app *app; | |
6132 | struct ust_app_session *ua_sess; | |
6133 | ||
6134 | assert(usess->active); | |
6135 | DBG2("UST app enabling map %s to global domain for session id %" PRIu64, | |
6136 | umap->name, usess->id); | |
6137 | ||
6138 | rcu_read_lock(); | |
6139 | ||
6140 | /* For every registered applications */ | |
6141 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6142 | if (!app->compatible) { | |
6143 | /* | |
6144 | * TODO: In time, we should notice the caller of this error by | |
6145 | * telling him that this is a version error. | |
6146 | */ | |
6147 | continue; | |
6148 | } | |
6149 | ua_sess = lookup_session_by_app(usess, app); | |
6150 | if (ua_sess == NULL) { | |
6151 | continue; | |
6152 | } | |
6153 | ||
6154 | /* Enable map onto application */ | |
6155 | ret = enable_ust_app_map(ua_sess, umap, app); | |
78f0bacd DG |
6156 | if (ret < 0) { |
6157 | /* XXX: We might want to report this error at some point... */ | |
6158 | continue; | |
6159 | } | |
6160 | } | |
6161 | ||
6162 | rcu_read_unlock(); | |
78f0bacd DG |
6163 | return ret; |
6164 | } | |
6165 | ||
b0a40d28 DG |
6166 | /* |
6167 | * Disable an event in a channel and for a specific session. | |
6168 | */ | |
ebdb334b | 6169 | int ust_app_disable_channel_event_glb(struct ltt_ust_session *usess, |
35a9059d | 6170 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
b0a40d28 DG |
6171 | { |
6172 | int ret = 0; | |
bec39940 | 6173 | struct lttng_ht_iter iter, uiter; |
700c5a9d | 6174 | struct lttng_ht_node_str *ua_chan_node; |
b0a40d28 DG |
6175 | struct ust_app *app; |
6176 | struct ust_app_session *ua_sess; | |
6177 | struct ust_app_channel *ua_chan; | |
6178 | struct ust_app_event *ua_event; | |
6179 | ||
88e3c2f5 | 6180 | assert(usess->active); |
b0a40d28 | 6181 | DBG("UST app disabling event %s for all apps in channel " |
d9bf3ca4 MD |
6182 | "%s for session id %" PRIu64, |
6183 | uevent->attr.name, uchan->name, usess->id); | |
b0a40d28 DG |
6184 | |
6185 | rcu_read_lock(); | |
6186 | ||
6187 | /* For all registered applications */ | |
852d0037 | 6188 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
6189 | if (!app->compatible) { |
6190 | /* | |
6191 | * TODO: In time, we should notice the caller of this error by | |
6192 | * telling him that this is a version error. | |
6193 | */ | |
6194 | continue; | |
6195 | } | |
b0a40d28 DG |
6196 | ua_sess = lookup_session_by_app(usess, app); |
6197 | if (ua_sess == NULL) { | |
6198 | /* Next app */ | |
6199 | continue; | |
6200 | } | |
6201 | ||
6202 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
6203 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
6204 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 6205 | if (ua_chan_node == NULL) { |
d9bf3ca4 | 6206 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
852d0037 | 6207 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
6208 | continue; |
6209 | } | |
6210 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
6211 | ||
700c5a9d JR |
6212 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
6213 | uevent->filter, uevent->attr.loglevel, | |
ebdb334b | 6214 | uevent->exclusion, uevent->attr.token); |
700c5a9d | 6215 | if (ua_event == NULL) { |
b0a40d28 | 6216 | DBG2("Event %s not found in channel %s for app pid %d." |
852d0037 | 6217 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
6218 | continue; |
6219 | } | |
b0a40d28 | 6220 | |
7f79d3a1 | 6221 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
b0a40d28 DG |
6222 | if (ret < 0) { |
6223 | /* XXX: Report error someday... */ | |
6224 | continue; | |
6225 | } | |
6226 | } | |
6227 | ||
6228 | rcu_read_unlock(); | |
88e3c2f5 JG |
6229 | return ret; |
6230 | } | |
6231 | ||
ebdb334b JR |
6232 | /* |
6233 | * Disable an event in a map and for a specific session. | |
6234 | */ | |
6235 | int ust_app_disable_map_event_glb(struct ltt_ust_session *usess, | |
6236 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) | |
6237 | { | |
6238 | int ret = 0; | |
6239 | struct lttng_ht_iter iter, uiter; | |
6240 | struct lttng_ht_node_str *ua_map_node; | |
6241 | struct ust_app *app; | |
6242 | struct ust_app_session *ua_sess; | |
6243 | struct ust_app_map *ua_map; | |
6244 | struct ust_app_event *ua_event; | |
6245 | ||
6246 | assert(usess->active); | |
6247 | DBG("UST app disabling event %s for all apps in map " | |
6248 | "%s for session id %" PRIu64, | |
6249 | uevent->attr.name, umap->name, usess->id); | |
6250 | ||
6251 | rcu_read_lock(); | |
6252 | ||
6253 | /* For all registered applications */ | |
6254 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6255 | if (!app->compatible) { | |
6256 | /* | |
6257 | * TODO: In time, we should notice the caller of this error by | |
6258 | * telling him that this is a version error. | |
6259 | */ | |
6260 | continue; | |
6261 | } | |
6262 | ua_sess = lookup_session_by_app(usess, app); | |
6263 | if (ua_sess == NULL) { | |
6264 | /* Next app */ | |
6265 | continue; | |
6266 | } | |
6267 | ||
6268 | /* Lookup map in the ust app session */ | |
6269 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); | |
6270 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); | |
6271 | if (ua_map_node == NULL) { | |
6272 | DBG2("map %s not found in session id %" PRIu64 " for app pid %d." | |
6273 | "Skipping", umap->name, usess->id, app->pid); | |
6274 | continue; | |
6275 | } | |
6276 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
6277 | ||
6278 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, | |
6279 | uevent->filter, uevent->attr.loglevel, | |
6280 | uevent->exclusion, uevent->attr.token); | |
6281 | if (ua_event == NULL) { | |
6282 | DBG2("Event %s not found in map %s for app pid %d." | |
6283 | "Skipping", uevent->attr.name, umap->name, app->pid); | |
6284 | continue; | |
6285 | } | |
6286 | ||
6287 | ret = disable_ust_app_event(ua_sess, ua_event, app); | |
6288 | if (ret < 0) { | |
6289 | /* XXX: Report error someday... */ | |
6290 | continue; | |
6291 | } | |
6292 | } | |
6293 | ||
6294 | rcu_read_unlock(); | |
6295 | return ret; | |
6296 | } | |
6297 | ||
88e3c2f5 JG |
6298 | /* The ua_sess lock must be held by the caller. */ |
6299 | static | |
6300 | int ust_app_channel_create(struct ltt_ust_session *usess, | |
6301 | struct ust_app_session *ua_sess, | |
6302 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
6303 | struct ust_app_channel **_ua_chan) | |
6304 | { | |
6305 | int ret = 0; | |
6306 | struct ust_app_channel *ua_chan = NULL; | |
6307 | ||
6308 | assert(ua_sess); | |
6309 | ASSERT_LOCKED(ua_sess->lock); | |
6310 | ||
6311 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, | |
6312 | sizeof(uchan->name))) { | |
6313 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, | |
6314 | &uchan->attr); | |
6315 | ret = 0; | |
6316 | } else { | |
6317 | struct ltt_ust_context *uctx = NULL; | |
6318 | ||
6319 | /* | |
6320 | * Create channel onto application and synchronize its | |
6321 | * configuration. | |
6322 | */ | |
6323 | ret = ust_app_channel_allocate(ua_sess, uchan, | |
6324 | LTTNG_UST_CHAN_PER_CPU, usess, | |
6325 | &ua_chan); | |
88ebf5a7 JR |
6326 | if (ret < 0) { |
6327 | goto error; | |
6328 | } | |
6329 | ||
6330 | ret = ust_app_channel_send(app, usess, | |
6331 | ua_sess, ua_chan); | |
6332 | if (ret) { | |
6333 | goto error; | |
88e3c2f5 JG |
6334 | } |
6335 | ||
6336 | /* Add contexts. */ | |
6337 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { | |
6338 | ret = create_ust_app_channel_context(ua_chan, | |
6339 | &uctx->ctx, app); | |
6340 | if (ret) { | |
88ebf5a7 | 6341 | goto error; |
88e3c2f5 JG |
6342 | } |
6343 | } | |
6344 | } | |
88ebf5a7 JR |
6345 | |
6346 | error: | |
88e3c2f5 JG |
6347 | if (ret < 0) { |
6348 | switch (ret) { | |
6349 | case -ENOTCONN: | |
6350 | /* | |
6351 | * The application's socket is not valid. Either a bad socket | |
6352 | * or a timeout on it. We can't inform the caller that for a | |
6353 | * specific app, the session failed so lets continue here. | |
6354 | */ | |
6355 | ret = 0; /* Not an error. */ | |
6356 | break; | |
6357 | case -ENOMEM: | |
6358 | default: | |
6359 | break; | |
6360 | } | |
6361 | } | |
88ebf5a7 | 6362 | |
88e3c2f5 JG |
6363 | if (ret == 0 && _ua_chan) { |
6364 | /* | |
6365 | * Only return the application's channel on success. Note | |
6366 | * that the channel can still be part of the application's | |
6367 | * channel hashtable on error. | |
6368 | */ | |
6369 | *_ua_chan = ua_chan; | |
6370 | } | |
b0a40d28 DG |
6371 | return ret; |
6372 | } | |
6373 | ||
ebdb334b JR |
6374 | /* The ua_sess lock must be held by the caller. */ |
6375 | static | |
6376 | int ust_app_map_create(struct ltt_ust_session *usess, | |
6377 | struct ust_app_session *ua_sess, | |
6378 | struct ltt_ust_map *umap, struct ust_app *app, | |
6379 | struct ust_app_map **_ua_map) | |
6380 | { | |
6381 | int ret = 0; | |
6382 | struct ust_app_map *ua_map = NULL; | |
6383 | ||
6384 | assert(ua_sess); | |
6385 | ASSERT_LOCKED(ua_sess->lock); | |
6386 | ||
6387 | /* | |
6388 | * Create map onto application and synchronize its | |
6389 | * configuration. | |
6390 | */ | |
6391 | ret = ust_app_map_allocate(ua_sess, umap, | |
6392 | LTTNG_UST_CHAN_PER_CPU, usess, | |
6393 | &ua_map); | |
6394 | if (ret < 0) { | |
6395 | goto error; | |
6396 | } | |
6397 | ||
6398 | ret = ust_app_map_send(app, usess, ua_sess, ua_map); | |
6399 | if (ret) { | |
6400 | goto error; | |
6401 | } | |
6402 | ||
6403 | error: | |
6404 | if (ret < 0) { | |
6405 | switch (ret) { | |
6406 | case -ENOTCONN: | |
6407 | /* | |
6408 | * The application's socket is not valid. Either a bad socket | |
6409 | * or a timeout on it. We can't inform the caller that for a | |
6410 | * specific app, the session failed so lets continue here. | |
6411 | */ | |
6412 | ret = 0; /* Not an error. */ | |
6413 | break; | |
6414 | case -ENOMEM: | |
6415 | default: | |
6416 | break; | |
6417 | } | |
6418 | } | |
6419 | ||
6420 | if (ret == 0 && _ua_map) { | |
6421 | /* | |
6422 | * Only return the application's map on success. Note | |
6423 | * that the map can still be part of the application's | |
6424 | * map hashtable on error. | |
6425 | */ | |
6426 | *_ua_map = ua_map; | |
6427 | } | |
6428 | return ret; | |
6429 | } | |
6430 | ||
5b4a0ec0 | 6431 | /* |
edb67388 | 6432 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 6433 | */ |
ebdb334b | 6434 | int ust_app_enable_channel_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
6435 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
6436 | { | |
6437 | int ret = 0; | |
bec39940 | 6438 | struct lttng_ht_iter iter, uiter; |
18eace3b | 6439 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
6440 | struct ust_app *app; |
6441 | struct ust_app_session *ua_sess; | |
6442 | struct ust_app_channel *ua_chan; | |
6443 | struct ust_app_event *ua_event; | |
48842b30 | 6444 | |
88e3c2f5 | 6445 | assert(usess->active); |
d9bf3ca4 | 6446 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
a991f516 | 6447 | uevent->attr.name, usess->id); |
48842b30 | 6448 | |
edb67388 DG |
6449 | /* |
6450 | * NOTE: At this point, this function is called only if the session and | |
6451 | * channel passed are already created for all apps. and enabled on the | |
6452 | * tracer also. | |
6453 | */ | |
6454 | ||
48842b30 | 6455 | rcu_read_lock(); |
421cb601 DG |
6456 | |
6457 | /* For all registered applications */ | |
852d0037 | 6458 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
6459 | if (!app->compatible) { |
6460 | /* | |
6461 | * TODO: In time, we should notice the caller of this error by | |
6462 | * telling him that this is a version error. | |
6463 | */ | |
6464 | continue; | |
6465 | } | |
edb67388 | 6466 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
6467 | if (!ua_sess) { |
6468 | /* The application has problem or is probably dead. */ | |
6469 | continue; | |
6470 | } | |
ba767faf | 6471 | |
ebdb334b JR |
6472 | pthread_mutex_lock(&ua_sess->lock); |
6473 | ||
6474 | if (ua_sess->deleted) { | |
6475 | pthread_mutex_unlock(&ua_sess->lock); | |
6476 | continue; | |
6477 | } | |
6478 | ||
6479 | /* Lookup channel in the ust app session */ | |
6480 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); | |
6481 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
6482 | /* | |
6483 | * It is possible that the channel cannot be found is | |
6484 | * the channel/event creation occurs concurrently with | |
6485 | * an application exit. | |
6486 | */ | |
6487 | if (!ua_chan_node) { | |
6488 | pthread_mutex_unlock(&ua_sess->lock); | |
6489 | continue; | |
6490 | } | |
6491 | ||
6492 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
6493 | ||
6494 | /* Get event node */ | |
6495 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
6496 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, | |
6497 | uevent->attr.token); | |
6498 | if (ua_event == NULL) { | |
6499 | DBG3("UST app enable event %s not found for app PID %d." | |
6500 | "Skipping app", uevent->attr.name, app->pid); | |
6501 | goto next_app; | |
6502 | } | |
6503 | ||
6504 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
6505 | if (ret < 0) { | |
6506 | pthread_mutex_unlock(&ua_sess->lock); | |
6507 | goto error; | |
6508 | } | |
6509 | next_app: | |
6510 | pthread_mutex_unlock(&ua_sess->lock); | |
6511 | } | |
6512 | ||
6513 | error: | |
6514 | rcu_read_unlock(); | |
6515 | return ret; | |
6516 | } | |
6517 | ||
6518 | /* | |
6519 | * Enable event for a specific session and map on the tracer. | |
6520 | */ | |
6521 | int ust_app_enable_map_event_glb(struct ltt_ust_session *usess, | |
6522 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) | |
6523 | { | |
6524 | int ret = 0; | |
6525 | struct lttng_ht_iter iter, uiter; | |
6526 | struct lttng_ht_node_str *ua_map_node; | |
6527 | struct ust_app *app; | |
6528 | struct ust_app_session *ua_sess; | |
6529 | struct ust_app_map *ua_map; | |
6530 | struct ust_app_event *ua_event; | |
6531 | ||
6532 | assert(usess->active); | |
6533 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, | |
6534 | uevent->attr.name, usess->id); | |
6535 | ||
6536 | /* | |
6537 | * NOTE: At this point, this function is called only if the session and | |
6538 | * map passed are already created for all apps. and enabled on the | |
6539 | * tracer also. | |
6540 | */ | |
6541 | ||
6542 | rcu_read_lock(); | |
6543 | ||
6544 | /* For all registered applications */ | |
6545 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6546 | if (!app->compatible) { | |
6547 | /* | |
6548 | * TODO: In time, we should notice the caller of this error by | |
6549 | * telling him that this is a version error. | |
6550 | */ | |
6551 | continue; | |
6552 | } | |
6553 | ua_sess = lookup_session_by_app(usess, app); | |
6554 | if (!ua_sess) { | |
6555 | /* The application has problem or is probably dead. */ | |
6556 | continue; | |
6557 | } | |
6558 | ||
6559 | pthread_mutex_lock(&ua_sess->lock); | |
6560 | ||
6561 | if (ua_sess->deleted) { | |
6562 | pthread_mutex_unlock(&ua_sess->lock); | |
6563 | continue; | |
6564 | } | |
6565 | ||
6566 | /* Lookup map in the ust app session */ | |
6567 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); | |
6568 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); | |
6569 | /* | |
6570 | * It is possible that the map cannot be found is | |
6571 | * the map/event creation occurs concurrently with | |
6572 | * an application exit. | |
6573 | */ | |
6574 | if (!ua_map_node) { | |
6575 | pthread_mutex_unlock(&ua_sess->lock); | |
6576 | continue; | |
6577 | } | |
6578 | ||
6579 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
6580 | ||
6581 | /* Get event node */ | |
6582 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, | |
6583 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, | |
6584 | uevent->attr.token); | |
6585 | if (ua_event == NULL) { | |
6586 | DBG3("UST app enable event %s not found for app PID %d." | |
6587 | "Skipping app", uevent->attr.name, app->pid); | |
6588 | goto next_app; | |
6589 | } | |
6590 | ||
6591 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
6592 | if (ret < 0) { | |
6593 | pthread_mutex_unlock(&ua_sess->lock); | |
6594 | goto error; | |
6595 | } | |
6596 | next_app: | |
6597 | pthread_mutex_unlock(&ua_sess->lock); | |
6598 | } | |
6599 | ||
6600 | error: | |
6601 | rcu_read_unlock(); | |
6602 | return ret; | |
6603 | } | |
6604 | ||
6605 | /* | |
6606 | * For a specific existing UST session and UST channel, creates the event for | |
6607 | * all registered apps. | |
6608 | */ | |
6609 | int ust_app_create_channel_event_glb(struct ltt_ust_session *usess, | |
6610 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
6611 | { | |
6612 | int ret = 0; | |
6613 | struct lttng_ht_iter iter, uiter; | |
6614 | struct lttng_ht_node_str *ua_chan_node; | |
6615 | struct ust_app *app; | |
6616 | struct ust_app_session *ua_sess; | |
6617 | struct ust_app_channel *ua_chan; | |
6618 | ||
6619 | assert(usess->active); | |
6620 | DBG("UST app creating event %s for all apps for session id %" PRIu64, | |
6621 | uevent->attr.name, usess->id); | |
6622 | ||
6623 | rcu_read_lock(); | |
6624 | ||
6625 | /* For all registered applications */ | |
6626 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
6627 | if (!app->compatible) { | |
6628 | /* | |
6629 | * TODO: In time, we should notice the caller of this error by | |
6630 | * telling him that this is a version error. | |
6631 | */ | |
6632 | continue; | |
6633 | } | |
6634 | ua_sess = lookup_session_by_app(usess, app); | |
6635 | if (!ua_sess) { | |
6636 | /* The application has problem or is probably dead. */ | |
6637 | continue; | |
6638 | } | |
6639 | ||
6640 | pthread_mutex_lock(&ua_sess->lock); | |
6641 | ||
6642 | if (ua_sess->deleted) { | |
6643 | pthread_mutex_unlock(&ua_sess->lock); | |
6644 | continue; | |
6645 | } | |
6646 | ||
6647 | /* Lookup channel in the ust app session */ | |
6648 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); | |
6649 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
6650 | /* If the channel is not found, there is a code flow error */ | |
6651 | assert(ua_chan_node); | |
6652 | ||
6653 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
6654 | ||
6655 | ret = create_ust_app_channel_event(ua_sess, ua_chan, uevent, app); | |
6656 | pthread_mutex_unlock(&ua_sess->lock); | |
6657 | if (ret < 0) { | |
6658 | if (ret != -LTTNG_UST_ERR_EXIST) { | |
6659 | /* Possible value at this point: -ENOMEM. If so, we stop! */ | |
6660 | break; | |
6661 | } | |
6662 | DBG2("UST app event %s already exist on app PID %d", | |
6663 | uevent->attr.name, app->pid); | |
6664 | continue; | |
6665 | } | |
6666 | } | |
6667 | ||
6668 | rcu_read_unlock(); | |
6669 | return ret; | |
6670 | } | |
6671 | ||
6672 | static | |
6673 | int snapshot_key_values(struct ustctl_daemon_counter *map_handle, | |
6674 | struct lttng_ht *key_to_bucket_index_ht, int cpu, | |
6675 | const char *key_filter, struct lttng_ht *values) | |
6676 | { | |
6677 | int ret; | |
6678 | struct lttng_ht_iter key_iter; | |
6679 | struct ust_registry_map_index_ht_entry *map_index_entry; | |
6680 | ||
6681 | /* Iterate over all the formated_key -> counter index */ | |
6682 | cds_lfht_for_each_entry(key_to_bucket_index_ht->ht, | |
6683 | &key_iter.iter, map_index_entry, node.node) { | |
6684 | bool overflow = 0, underflow = 0; | |
6685 | int64_t local_value = 0; | |
6686 | size_t dimension_indexes[1] = {map_index_entry->index}; | |
6687 | ||
6688 | if (key_filter && strcmp(key_filter, | |
6689 | map_index_entry->formated_key) != 0) { | |
6690 | continue; | |
6691 | } | |
6692 | ||
6693 | ret = ustctl_counter_read(map_handle, | |
6694 | dimension_indexes, cpu, &local_value, | |
6695 | &overflow, &underflow); | |
6696 | if (ret) { | |
6697 | ERR("Error getting counter value from the tracer: key = '%s'", | |
6698 | map_index_entry->formated_key); | |
6699 | ret = -1; | |
6700 | goto end; | |
6701 | } | |
6702 | ||
6703 | map_add_or_increment_map_values(values, | |
6704 | map_index_entry->formated_key, local_value, | |
6705 | underflow, overflow); | |
6706 | } | |
6707 | ret = 0; | |
6708 | end: | |
6709 | return ret; | |
6710 | } | |
6711 | ||
6712 | static | |
6713 | int ust_app_map_list_values_per_uid_with_bitness_and_cpu( | |
6714 | const struct ltt_ust_session *usess, | |
6715 | const struct ltt_ust_map *umap, | |
6716 | uint32_t app_bitness, | |
6717 | uint32_t cpu, | |
6718 | const char *key_filter, | |
6719 | struct lttng_ht *values) | |
6720 | { | |
6721 | int ret = 0; | |
6722 | struct lttng_ht_iter iter; | |
6723 | struct buffer_reg_uid *buf_reg_uid; | |
6724 | struct buffer_reg_map *buf_reg_map; | |
6725 | struct ust_registry_session *ust_reg_sess; | |
6726 | struct lttng_ht_node_u64 *ust_reg_map_node; | |
6727 | struct ust_registry_map *ust_reg_map; | |
6728 | ||
6729 | buf_reg_uid = buffer_reg_uid_find(usess->id, app_bitness, usess->uid); | |
6730 | if (!buf_reg_uid) { | |
6731 | /* | |
6732 | * Buffer registry entry for uid not found. Probably no app for | |
6733 | * this UID at the moment. | |
6734 | */ | |
6735 | DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64", bitness = %"PRIu32", uid = %d", | |
6736 | usess->id, app_bitness, usess->uid); | |
6737 | /* | |
6738 | * Not an error. Leave the key value pair unchanged and return. | |
6739 | */ | |
6740 | ret = 0; | |
6741 | goto end; | |
6742 | } | |
6743 | ||
6744 | buf_reg_map = buffer_reg_map_find(umap->id, buf_reg_uid); | |
6745 | if (!buf_reg_uid) { | |
6746 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, | |
6747 | umap->id); | |
6748 | ret = -1; | |
6749 | goto end; | |
6750 | } | |
6751 | ||
6752 | ust_reg_sess = buf_reg_uid->registry->reg.ust; | |
6753 | ||
6754 | /* Get the ust_reg map object from the registry */ | |
6755 | // FIXME: frdeso: This can be changed to ust_registry_map_find() right? | |
6756 | ||
6757 | lttng_ht_lookup(ust_reg_sess->maps, (void *) &umap->id, &iter); | |
6758 | ust_reg_map_node = lttng_ht_iter_get_node_u64(&iter); | |
6759 | if (!ust_reg_map_node) { | |
6760 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, | |
6761 | umap->id); | |
6762 | ret = -1; | |
6763 | goto end; | |
6764 | } | |
6765 | ust_reg_map = caa_container_of(ust_reg_map_node, | |
6766 | struct ust_registry_map, node); | |
6767 | ||
6768 | ret = snapshot_key_values(buf_reg_map->daemon_counter, | |
6769 | ust_reg_map->key_string_to_bucket_index_ht, | |
6770 | cpu, key_filter, values); | |
6771 | if (ret) { | |
6772 | abort(); | |
6773 | } | |
6774 | ||
6775 | ||
6776 | ret = 0; | |
6777 | end: | |
6778 | return ret; | |
6779 | } | |
6780 | ||
6781 | static | |
6782 | int ust_app_map_list_values_per_uid(const struct ltt_ust_session *usess, | |
6783 | const struct ltt_ust_map *umap, | |
6784 | const struct lttng_map_query *query, | |
6785 | struct lttng_map_content *map_content) | |
6786 | { | |
6787 | int i, ret = 0; | |
6788 | enum lttng_map_query_status map_query_status; | |
6789 | const char *key_filter; | |
6790 | struct lttng_ht *values = NULL; | |
6791 | bool sum_cpus = lttng_map_query_get_config_sum_by_cpu(query); | |
6792 | enum lttng_map_query_config_buffer config_buffer; | |
6793 | enum lttng_map_query_config_cpu config_cpu; | |
6794 | int selected_cpu; | |
6795 | ||
6796 | map_query_status = lttng_map_query_get_key_filter(query, &key_filter); | |
6797 | if (map_query_status == LTTNG_MAP_QUERY_STATUS_NONE) { | |
6798 | key_filter = NULL; | |
6799 | } else if (map_query_status != LTTNG_MAP_QUERY_STATUS_OK) { | |
6800 | ret = -1; | |
6801 | goto end; | |
6802 | } | |
6803 | ||
6804 | config_cpu = lttng_map_query_get_config_cpu(query); | |
6805 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { | |
6806 | unsigned int count; | |
6807 | map_query_status = lttng_map_query_get_cpu_count(query, &count); | |
6808 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
6809 | assert(count == 1); | |
6810 | ||
6811 | map_query_status = lttng_map_query_get_cpu_at_index(query, 0, | |
6812 | &selected_cpu); | |
6813 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
6814 | } | |
6815 | ||
6816 | config_buffer = lttng_map_query_get_config_buffer(query); | |
6817 | if (config_buffer == LTTNG_MAP_QUERY_CONFIG_BUFFER_UST_UID_SUBSET) { | |
6818 | unsigned int count; | |
6819 | uid_t selected_uid; | |
6820 | ||
6821 | map_query_status = lttng_map_query_get_uid_count(query, &count); | |
6822 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
6823 | assert(count == 1); | |
6824 | ||
6825 | map_query_status = lttng_map_query_get_uid_at_index(query, 0, | |
6826 | &selected_uid); | |
6827 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
6828 | ||
6829 | if (selected_uid != usess->uid) { | |
6830 | ret = 0; | |
6831 | goto end; | |
6832 | } | |
6833 | } | |
6834 | ||
6835 | if (sum_cpus) { | |
6836 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
6837 | } | |
6838 | ||
6839 | for (i = 0; i < umap->nr_cpu; i++) { | |
6840 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { | |
6841 | if (selected_cpu != i) { | |
6842 | continue; | |
6843 | } | |
6844 | } | |
6845 | ||
6846 | if (!sum_cpus) { | |
6847 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
6848 | } | |
6849 | ||
6850 | ret = ust_app_map_list_values_per_uid_with_bitness_and_cpu( | |
6851 | usess, umap, 32, i, key_filter, | |
6852 | values); | |
6853 | if (ret) { | |
6854 | abort(); | |
6855 | } | |
6856 | ||
6857 | ret = ust_app_map_list_values_per_uid_with_bitness_and_cpu( | |
6858 | usess, umap, 64, i, key_filter, | |
6859 | values); | |
6860 | if (ret) { | |
6861 | abort(); | |
6862 | } | |
6863 | if (!sum_cpus) { | |
6864 | ret = map_new_content_section(map_content, | |
6865 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID, | |
6866 | sum_cpus, usess->uid, i, values); | |
6867 | if (ret) { | |
6868 | abort(); | |
6869 | } | |
6870 | ||
6871 | lttng_ht_destroy(values); | |
6872 | } | |
6873 | } | |
6874 | ||
6875 | if (sum_cpus) { | |
6876 | ret = map_new_content_section(map_content, | |
6877 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_UID, | |
6878 | sum_cpus, usess->uid, 0, values); | |
6879 | if (ret) { | |
6880 | abort(); | |
6881 | } | |
6882 | lttng_ht_destroy(values); | |
6883 | } | |
6884 | ||
6885 | end: | |
6886 | return ret; | |
6887 | } | |
6888 | ||
6889 | static | |
6890 | int append_dead_app_kv(struct ltt_ust_map *umap, | |
6891 | const char *key_filter, | |
6892 | struct lttng_map_content *map_content) | |
6893 | { | |
6894 | int ret; | |
6895 | struct lttng_ht *dead_app_kv_ht; | |
6896 | struct map_kv_ht_entry *kv_entry; | |
6897 | struct lttng_ht_iter key_iter; | |
6898 | ||
6899 | struct lttng_ht *values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
6900 | ||
6901 | pthread_mutex_lock(&(umap->dead_app_kv_values.lock)); | |
6902 | ||
6903 | assert(umap->dead_app_kv_values.dead_app_kv_values_64bits); | |
6904 | dead_app_kv_ht = umap->dead_app_kv_values.dead_app_kv_values_64bits; | |
6905 | ||
6906 | cds_lfht_for_each_entry(dead_app_kv_ht->ht, &key_iter.iter, kv_entry, | |
6907 | node.node) { | |
6908 | if (key_filter && strcmp(key_filter, kv_entry->key) != 0) { | |
6909 | continue; | |
6910 | } | |
6911 | map_add_or_increment_map_values(values, kv_entry->key, | |
6912 | kv_entry->value, kv_entry->has_underflowed, | |
6913 | kv_entry->has_overflowed); | |
6914 | } | |
6915 | ||
6916 | assert(umap->dead_app_kv_values.dead_app_kv_values_32bits); | |
6917 | ||
6918 | dead_app_kv_ht = umap->dead_app_kv_values.dead_app_kv_values_32bits; | |
6919 | cds_lfht_for_each_entry(dead_app_kv_ht->ht, &key_iter.iter, kv_entry, | |
6920 | node.node) { | |
6921 | if (key_filter && strcmp(key_filter, kv_entry->key) != 0) { | |
6922 | continue; | |
6923 | } | |
6924 | map_add_or_increment_map_values(values, kv_entry->key, | |
6925 | kv_entry->value, kv_entry->has_underflowed, | |
6926 | kv_entry->has_overflowed); | |
6927 | } | |
6928 | ||
6929 | pthread_mutex_unlock(&umap->dead_app_kv_values.lock); | |
6930 | ||
6931 | ret = map_new_content_section(map_content, | |
6932 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID_AGGREGATED, | |
6933 | true, 0, 0, values); | |
6934 | ||
6935 | lttng_ht_destroy(values); | |
6936 | if (ret) { | |
6937 | ERR("Error appending deadapp kv"); | |
6938 | goto end; | |
6939 | } | |
6940 | ||
6941 | ||
6942 | ret = 0; | |
6943 | ||
6944 | end: | |
6945 | return ret; | |
6946 | } | |
6947 | ||
6948 | static | |
6949 | int ust_app_map_list_values_per_pid_with_bitness_and_cpu( | |
6950 | const struct ltt_ust_session *usess, | |
6951 | struct ust_app *app, | |
6952 | struct ltt_ust_map *umap, | |
6953 | uint32_t app_bitness, | |
6954 | uint32_t cpu, | |
6955 | const char *key_filter, | |
6956 | struct lttng_ht *values) | |
6957 | { | |
6958 | int ret = 0; | |
6959 | ||
6960 | struct lttng_ht_iter map_iter; | |
6961 | struct lttng_ht_node_str *ua_map_node; | |
6962 | struct ust_app_map *ua_map; | |
6963 | struct ust_app_session *ua_sess; | |
6964 | struct ust_registry_session *ust_reg_sess; | |
6965 | struct ust_registry_map *ust_reg_map; | |
6966 | ||
6967 | if (app->bits_per_long != app_bitness) { | |
6968 | ret = 0; | |
6969 | goto end;; | |
6970 | } | |
6971 | ||
6972 | ua_sess = lookup_session_by_app(usess, app); | |
6973 | if (!ua_sess) { | |
6974 | /* Session not associated with this app. */ | |
6975 | ret = 0; | |
6976 | goto end;; | |
6977 | } | |
6978 | ||
6979 | ust_reg_sess = get_session_registry(ua_sess); | |
6980 | if (!ust_reg_sess) { | |
6981 | DBG("Application session is being torn down. Skip application."); | |
6982 | ret = 0; | |
6983 | goto end;; | |
6984 | } | |
6985 | ||
6986 | /* Lookup map in the ust app session */ | |
6987 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &map_iter); | |
6988 | ua_map_node = lttng_ht_iter_get_node_str(&map_iter); | |
6989 | ||
6990 | assert(ua_map_node != NULL); | |
6991 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
6992 | ||
6993 | pthread_mutex_lock(&ust_reg_sess->lock); | |
6994 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); | |
6995 | pthread_mutex_unlock(&ust_reg_sess->lock); | |
6996 | assert(ust_reg_map); | |
6997 | ||
6998 | ret = snapshot_key_values(ua_map->map_handle, | |
6999 | ust_reg_map->key_string_to_bucket_index_ht, | |
7000 | cpu, key_filter, values); | |
7001 | if (ret) { | |
7002 | ERR("Error snapshoting the content of map"); | |
7003 | goto end; | |
7004 | } | |
7005 | ||
7006 | end: | |
7007 | return ret; | |
7008 | } | |
7009 | ||
7010 | static | |
7011 | int ust_app_map_list_values_per_pid(const struct ltt_ust_session *usess, | |
7012 | struct ltt_ust_map *umap, | |
7013 | const struct lttng_map_query *query, | |
7014 | struct lttng_map_content *map_content) | |
7015 | { | |
7016 | enum lttng_map_query_status map_query_status; | |
7017 | const char *key_filter; | |
7018 | struct lttng_ht *values; | |
7019 | bool sum_cpus = lttng_map_query_get_config_sum_by_cpu(query); | |
7020 | bool sum_pids = lttng_map_query_get_config_sum_by_pid(query); | |
7021 | enum lttng_map_query_config_cpu config_cpu; | |
7022 | int selected_cpu, i, ret = 0; | |
7023 | struct lttng_ht_iter app_iter; | |
7024 | struct ust_app *app; | |
7025 | ||
7026 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
7027 | ||
7028 | map_query_status = lttng_map_query_get_key_filter(query, &key_filter); | |
7029 | if (map_query_status == LTTNG_MAP_QUERY_STATUS_NONE) { | |
7030 | key_filter = NULL; | |
7031 | } else if (map_query_status != LTTNG_MAP_QUERY_STATUS_OK) { | |
7032 | ret = -1; | |
7033 | goto end; | |
7034 | } | |
7035 | ||
7036 | config_cpu = lttng_map_query_get_config_cpu(query); | |
7037 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { | |
7038 | unsigned int count; | |
7039 | map_query_status = lttng_map_query_get_cpu_count(query, &count); | |
7040 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
7041 | assert(count == 1); | |
7042 | ||
7043 | map_query_status = lttng_map_query_get_cpu_at_index(query, 0, | |
7044 | &selected_cpu); | |
7045 | assert(map_query_status == LTTNG_MAP_QUERY_STATUS_OK); | |
7046 | } | |
7047 | ||
7048 | /* Sum all cpus and pids on the same table. */ | |
7049 | if (sum_cpus && sum_pids) { | |
7050 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
7051 | } | |
7052 | ||
7053 | if (!sum_cpus && sum_pids) { | |
7054 | /* Iterate over all currently registered apps. */ | |
7055 | for (i = 0; i < umap->nr_cpu; i++) { | |
7056 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
7057 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { | |
7058 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( | |
7059 | usess, app, umap, 32, i, key_filter, values); | |
7060 | if (ret) { | |
7061 | abort(); | |
7062 | } | |
7063 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( | |
7064 | usess, app, umap, 64, i, key_filter, values); | |
7065 | if (ret) { | |
7066 | abort(); | |
7067 | } | |
7068 | } | |
7069 | ret = map_new_content_section(map_content, | |
7070 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, | |
7071 | sum_cpus, app->pid, i, values); | |
7072 | if (ret) { | |
7073 | abort(); | |
7074 | } | |
7075 | ||
7076 | lttng_ht_destroy(values); | |
7077 | } | |
7078 | } else { | |
7079 | /* Iterate over all currently registered apps. */ | |
7080 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { | |
7081 | ||
7082 | if (sum_cpus && !sum_pids) { | |
7083 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
7084 | } | |
7085 | ||
7086 | for (i = 0; i < umap->nr_cpu; i++) { | |
7087 | ||
7088 | if (config_cpu == LTTNG_MAP_QUERY_CONFIG_CPU_SUBSET) { | |
7089 | if (selected_cpu != i) { | |
7090 | continue; | |
7091 | } | |
7092 | } | |
7093 | ||
7094 | if (!sum_cpus && !sum_pids) { | |
7095 | values = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
7096 | } | |
7097 | ||
7098 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( | |
7099 | usess, app, umap, 32, i, key_filter, values); | |
7100 | if (ret) { | |
7101 | abort(); | |
7102 | } | |
7103 | ret = ust_app_map_list_values_per_pid_with_bitness_and_cpu( | |
7104 | usess, app, umap, 64, i, key_filter, values); | |
7105 | if (ret) { | |
7106 | abort(); | |
7107 | } | |
7108 | ||
7109 | if (!sum_cpus && !sum_pids) { | |
7110 | ret = map_new_content_section(map_content, | |
7111 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, | |
7112 | sum_cpus, app->pid, i, values); | |
7113 | if (ret) { | |
7114 | abort(); | |
7115 | } | |
d0b96690 | 7116 | |
ebdb334b JR |
7117 | lttng_ht_destroy(values); |
7118 | } | |
7119 | } | |
7120 | if (sum_cpus && !sum_pids) { | |
7121 | ret = map_new_content_section(map_content, | |
7122 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, | |
7123 | sum_cpus, app->pid, i, values); | |
7124 | if (ret) { | |
7125 | abort(); | |
7126 | } | |
7127 | ||
7128 | lttng_ht_destroy(values); | |
7129 | } | |
b161602a | 7130 | } |
ebdb334b | 7131 | } |
b161602a | 7132 | |
ebdb334b JR |
7133 | if (sum_cpus && sum_pids) { |
7134 | ret = map_new_content_section(map_content, | |
7135 | LTTNG_MAP_KEY_VALUE_PAIR_LIST_TYPE_UST_PER_PID, | |
7136 | sum_cpus, 0, 0, values); | |
7137 | if (ret) { | |
7138 | abort(); | |
a7169585 | 7139 | } |
ebdb334b JR |
7140 | lttng_ht_destroy(values); |
7141 | } | |
edb67388 | 7142 | |
ebdb334b JR |
7143 | /* Append dead app aggregated key-value pairs. */ |
7144 | ret = append_dead_app_kv(umap, key_filter, map_content); | |
7145 | if (ret) { | |
7146 | ERR("Error appending values from dead apps map"); | |
7147 | goto end; | |
7148 | } | |
edb67388 | 7149 | |
ebdb334b JR |
7150 | end: |
7151 | return ret; | |
7152 | } | |
35a9059d | 7153 | |
ebdb334b JR |
7154 | int ust_app_map_list_values(const struct ltt_ust_session *usess, |
7155 | struct ltt_ust_map *umap, | |
7156 | const struct lttng_map_query *query, | |
7157 | struct lttng_map_content **map_content) | |
7158 | { | |
7159 | int ret; | |
7160 | struct lttng_map_content *local_map_content = NULL; | |
7161 | ||
7162 | local_map_content = lttng_map_content_create(usess->buffer_type); | |
7163 | if (!local_map_content) { | |
7164 | ERR("Error creating a map content list"); | |
7165 | ret = -1; | |
7166 | goto end; | |
7167 | } | |
7168 | rcu_read_lock(); | |
7169 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
7170 | ret = ust_app_map_list_values_per_uid(usess, umap, query, | |
7171 | local_map_content); | |
7172 | if (ret) { | |
7173 | ERR("Error adding per-uid map value"); | |
7174 | ret = -1; | |
7175 | goto end; | |
7176 | } | |
7177 | } else { | |
7178 | ret = ust_app_map_list_values_per_pid(usess, umap, query, | |
7179 | local_map_content); | |
7180 | if (ret) { | |
7181 | ERR("Error adding per-pid map value"); | |
7182 | ret = -1; | |
7183 | goto end; | |
48842b30 | 7184 | } |
edb67388 | 7185 | } |
ebdb334b JR |
7186 | *map_content = local_map_content; |
7187 | local_map_content = NULL; | |
7188 | ret = 0; | |
7189 | end: | |
edb67388 | 7190 | rcu_read_unlock(); |
ebdb334b JR |
7191 | |
7192 | lttng_map_content_destroy(local_map_content); | |
edb67388 DG |
7193 | return ret; |
7194 | } | |
7195 | ||
7196 | /* | |
ebdb334b | 7197 | * For a specific existing UST session and UST map, creates the event for |
edb67388 DG |
7198 | * all registered apps. |
7199 | */ | |
ebdb334b JR |
7200 | int ust_app_create_map_event_glb(struct ltt_ust_session *usess, |
7201 | struct ltt_ust_map *umap, struct ltt_ust_event *uevent) | |
edb67388 DG |
7202 | { |
7203 | int ret = 0; | |
bec39940 | 7204 | struct lttng_ht_iter iter, uiter; |
ebdb334b | 7205 | struct lttng_ht_node_str *ua_map_node; |
edb67388 DG |
7206 | struct ust_app *app; |
7207 | struct ust_app_session *ua_sess; | |
ebdb334b | 7208 | struct ust_app_map *ua_map; |
edb67388 | 7209 | |
88e3c2f5 | 7210 | assert(usess->active); |
ebdb334b JR |
7211 | DBG("UST app creating event %s in map %s for all apps for session id %" PRIu64, |
7212 | uevent->attr.name, umap->name, usess->id); | |
edb67388 | 7213 | |
edb67388 DG |
7214 | rcu_read_lock(); |
7215 | ||
7216 | /* For all registered applications */ | |
852d0037 | 7217 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
7218 | if (!app->compatible) { |
7219 | /* | |
7220 | * TODO: In time, we should notice the caller of this error by | |
7221 | * telling him that this is a version error. | |
7222 | */ | |
7223 | continue; | |
7224 | } | |
edb67388 | 7225 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
7226 | if (!ua_sess) { |
7227 | /* The application has problem or is probably dead. */ | |
7228 | continue; | |
7229 | } | |
48842b30 | 7230 | |
d0b96690 | 7231 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
7232 | |
7233 | if (ua_sess->deleted) { | |
7234 | pthread_mutex_unlock(&ua_sess->lock); | |
7235 | continue; | |
7236 | } | |
7237 | ||
ebdb334b JR |
7238 | /* Lookup map in the ust app session */ |
7239 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &uiter); | |
7240 | ua_map_node = lttng_ht_iter_get_node_str(&uiter); | |
7241 | /* If the map is not found, there is a code flow error */ | |
7242 | assert(ua_map_node); | |
edb67388 | 7243 | |
ebdb334b | 7244 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); |
48842b30 | 7245 | |
ebdb334b JR |
7246 | ret = create_ust_app_map_event(ua_sess, ua_map, uevent, app); |
7247 | assert(!ret); | |
d0b96690 | 7248 | pthread_mutex_unlock(&ua_sess->lock); |
edb67388 | 7249 | if (ret < 0) { |
49c336c1 | 7250 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
7251 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
7252 | break; | |
7253 | } | |
7254 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 7255 | uevent->attr.name, app->pid); |
5b4a0ec0 | 7256 | continue; |
48842b30 | 7257 | } |
48842b30 | 7258 | } |
5b4a0ec0 | 7259 | |
48842b30 | 7260 | rcu_read_unlock(); |
48842b30 DG |
7261 | return ret; |
7262 | } | |
7263 | ||
5b4a0ec0 DG |
7264 | /* |
7265 | * Start tracing for a specific UST session and app. | |
fad1ed2f JR |
7266 | * |
7267 | * Called with UST app session lock held. | |
7268 | * | |
5b4a0ec0 | 7269 | */ |
b34cbebf | 7270 | static |
421cb601 | 7271 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
7272 | { |
7273 | int ret = 0; | |
48842b30 | 7274 | struct ust_app_session *ua_sess; |
48842b30 | 7275 | |
852d0037 | 7276 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 7277 | |
509cbaf8 MD |
7278 | rcu_read_lock(); |
7279 | ||
e0c7ec2b DG |
7280 | if (!app->compatible) { |
7281 | goto end; | |
7282 | } | |
7283 | ||
421cb601 DG |
7284 | ua_sess = lookup_session_by_app(usess, app); |
7285 | if (ua_sess == NULL) { | |
d42f20df DG |
7286 | /* The session is in teardown process. Ignore and continue. */ |
7287 | goto end; | |
421cb601 | 7288 | } |
48842b30 | 7289 | |
d0b96690 DG |
7290 | pthread_mutex_lock(&ua_sess->lock); |
7291 | ||
b161602a MD |
7292 | if (ua_sess->deleted) { |
7293 | pthread_mutex_unlock(&ua_sess->lock); | |
7294 | goto end; | |
7295 | } | |
7296 | ||
b0a1c741 JR |
7297 | if (ua_sess->enabled) { |
7298 | pthread_mutex_unlock(&ua_sess->lock); | |
7299 | goto end; | |
7300 | } | |
7301 | ||
aea829b3 DG |
7302 | /* Upon restart, we skip the setup, already done */ |
7303 | if (ua_sess->started) { | |
8be98f9a | 7304 | goto skip_setup; |
aea829b3 | 7305 | } |
8be98f9a | 7306 | |
840cb59c | 7307 | health_code_update(); |
86acf0da | 7308 | |
8be98f9a | 7309 | skip_setup: |
a945cdc7 | 7310 | /* This starts the UST tracing */ |
fb45065e | 7311 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 7312 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
fb45065e | 7313 | pthread_mutex_unlock(&app->sock_lock); |
421cb601 | 7314 | if (ret < 0) { |
ffe60014 DG |
7315 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
7316 | ERR("Error starting tracing for app pid: %d (ret: %d)", | |
7317 | app->pid, ret); | |
7318 | } else { | |
7319 | DBG("UST app start session failed. Application is dead."); | |
3757b385 DG |
7320 | /* |
7321 | * This is normal behavior, an application can die during the | |
7322 | * creation process. Don't report an error so the execution can | |
7323 | * continue normally. | |
7324 | */ | |
7325 | pthread_mutex_unlock(&ua_sess->lock); | |
7326 | goto end; | |
ffe60014 | 7327 | } |
d0b96690 | 7328 | goto error_unlock; |
421cb601 | 7329 | } |
5b4a0ec0 | 7330 | |
55c3953d DG |
7331 | /* Indicate that the session has been started once */ |
7332 | ua_sess->started = 1; | |
b0a1c741 | 7333 | ua_sess->enabled = 1; |
55c3953d | 7334 | |
d0b96690 DG |
7335 | pthread_mutex_unlock(&ua_sess->lock); |
7336 | ||
840cb59c | 7337 | health_code_update(); |
86acf0da | 7338 | |
421cb601 | 7339 | /* Quiescent wait after starting trace */ |
fb45065e | 7340 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 7341 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 7342 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
7343 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
7344 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
7345 | app->pid, ret); | |
7346 | } | |
48842b30 | 7347 | |
e0c7ec2b DG |
7348 | end: |
7349 | rcu_read_unlock(); | |
840cb59c | 7350 | health_code_update(); |
421cb601 | 7351 | return 0; |
48842b30 | 7352 | |
d0b96690 DG |
7353 | error_unlock: |
7354 | pthread_mutex_unlock(&ua_sess->lock); | |
509cbaf8 | 7355 | rcu_read_unlock(); |
840cb59c | 7356 | health_code_update(); |
421cb601 DG |
7357 | return -1; |
7358 | } | |
48842b30 | 7359 | |
8be98f9a MD |
7360 | /* |
7361 | * Stop tracing for a specific UST session and app. | |
7362 | */ | |
b34cbebf | 7363 | static |
8be98f9a MD |
7364 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
7365 | { | |
7366 | int ret = 0; | |
7367 | struct ust_app_session *ua_sess; | |
7972aab2 | 7368 | struct ust_registry_session *registry; |
8be98f9a | 7369 | |
852d0037 | 7370 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
7371 | |
7372 | rcu_read_lock(); | |
7373 | ||
e0c7ec2b | 7374 | if (!app->compatible) { |
d88aee68 | 7375 | goto end_no_session; |
e0c7ec2b DG |
7376 | } |
7377 | ||
8be98f9a MD |
7378 | ua_sess = lookup_session_by_app(usess, app); |
7379 | if (ua_sess == NULL) { | |
d88aee68 | 7380 | goto end_no_session; |
8be98f9a MD |
7381 | } |
7382 | ||
d88aee68 DG |
7383 | pthread_mutex_lock(&ua_sess->lock); |
7384 | ||
b161602a MD |
7385 | if (ua_sess->deleted) { |
7386 | pthread_mutex_unlock(&ua_sess->lock); | |
7387 | goto end_no_session; | |
7388 | } | |
7389 | ||
9bc07046 DG |
7390 | /* |
7391 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
7392 | * that was never started. It's possible since we can have a fail start |
7393 | * from either the application manager thread or the command thread. Simply | |
7394 | * indicate that this is a stop error. | |
9bc07046 | 7395 | */ |
f9dfc3d9 | 7396 | if (!ua_sess->started) { |
c45536e1 DG |
7397 | goto error_rcu_unlock; |
7398 | } | |
7db205b5 | 7399 | |
840cb59c | 7400 | health_code_update(); |
86acf0da | 7401 | |
9d6c7d3f | 7402 | /* This inhibits UST tracing */ |
fb45065e | 7403 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 7404 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
fb45065e | 7405 | pthread_mutex_unlock(&app->sock_lock); |
9d6c7d3f | 7406 | if (ret < 0) { |
ffe60014 DG |
7407 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
7408 | ERR("Error stopping tracing for app pid: %d (ret: %d)", | |
7409 | app->pid, ret); | |
7410 | } else { | |
7411 | DBG("UST app stop session failed. Application is dead."); | |
3757b385 DG |
7412 | /* |
7413 | * This is normal behavior, an application can die during the | |
7414 | * creation process. Don't report an error so the execution can | |
7415 | * continue normally. | |
7416 | */ | |
7417 | goto end_unlock; | |
ffe60014 | 7418 | } |
9d6c7d3f DG |
7419 | goto error_rcu_unlock; |
7420 | } | |
7421 | ||
840cb59c | 7422 | health_code_update(); |
b0a1c741 | 7423 | ua_sess->enabled = 0; |
86acf0da | 7424 | |
9d6c7d3f | 7425 | /* Quiescent wait after stopping trace */ |
fb45065e | 7426 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 7427 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 7428 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
7429 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
7430 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
7431 | app->pid, ret); | |
7432 | } | |
9d6c7d3f | 7433 | |
840cb59c | 7434 | health_code_update(); |
86acf0da | 7435 | |
b34cbebf | 7436 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
7437 | |
7438 | /* The UST app session is held registry shall not be null. */ | |
b34cbebf | 7439 | assert(registry); |
1b532a60 | 7440 | |
ce34fcd0 MD |
7441 | /* Push metadata for application before freeing the application. */ |
7442 | (void) push_metadata(registry, ua_sess->consumer); | |
b34cbebf | 7443 | |
3757b385 | 7444 | end_unlock: |
b34cbebf MD |
7445 | pthread_mutex_unlock(&ua_sess->lock); |
7446 | end_no_session: | |
7447 | rcu_read_unlock(); | |
7448 | health_code_update(); | |
7449 | return 0; | |
7450 | ||
7451 | error_rcu_unlock: | |
7452 | pthread_mutex_unlock(&ua_sess->lock); | |
7453 | rcu_read_unlock(); | |
7454 | health_code_update(); | |
7455 | return -1; | |
7456 | } | |
7457 | ||
b34cbebf | 7458 | static |
c4b88406 MD |
7459 | int ust_app_flush_app_session(struct ust_app *app, |
7460 | struct ust_app_session *ua_sess) | |
b34cbebf | 7461 | { |
c4b88406 | 7462 | int ret, retval = 0; |
b34cbebf | 7463 | struct lttng_ht_iter iter; |
b34cbebf | 7464 | struct ust_app_channel *ua_chan; |
c4b88406 | 7465 | struct consumer_socket *socket; |
b34cbebf | 7466 | |
c4b88406 | 7467 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
b34cbebf MD |
7468 | |
7469 | rcu_read_lock(); | |
7470 | ||
7471 | if (!app->compatible) { | |
c4b88406 | 7472 | goto end_not_compatible; |
b34cbebf MD |
7473 | } |
7474 | ||
7475 | pthread_mutex_lock(&ua_sess->lock); | |
7476 | ||
b161602a MD |
7477 | if (ua_sess->deleted) { |
7478 | goto end_deleted; | |
7479 | } | |
7480 | ||
b34cbebf MD |
7481 | health_code_update(); |
7482 | ||
9d6c7d3f | 7483 | /* Flushing buffers */ |
c4b88406 MD |
7484 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
7485 | ua_sess->consumer); | |
ce34fcd0 MD |
7486 | |
7487 | /* Flush buffers and push metadata. */ | |
7488 | switch (ua_sess->buffer_type) { | |
7489 | case LTTNG_BUFFER_PER_PID: | |
7490 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, | |
7491 | node.node) { | |
7492 | health_code_update(); | |
ce34fcd0 MD |
7493 | ret = consumer_flush_channel(socket, ua_chan->key); |
7494 | if (ret) { | |
7495 | ERR("Error flushing consumer channel"); | |
7496 | retval = -1; | |
7497 | continue; | |
7498 | } | |
8be98f9a | 7499 | } |
ce34fcd0 MD |
7500 | break; |
7501 | case LTTNG_BUFFER_PER_UID: | |
7502 | default: | |
7503 | assert(0); | |
7504 | break; | |
8be98f9a | 7505 | } |
8be98f9a | 7506 | |
840cb59c | 7507 | health_code_update(); |
86acf0da | 7508 | |
b161602a | 7509 | end_deleted: |
d88aee68 | 7510 | pthread_mutex_unlock(&ua_sess->lock); |
ce34fcd0 | 7511 | |
c4b88406 MD |
7512 | end_not_compatible: |
7513 | rcu_read_unlock(); | |
7514 | health_code_update(); | |
7515 | return retval; | |
7516 | } | |
7517 | ||
7518 | /* | |
ce34fcd0 MD |
7519 | * Flush buffers for all applications for a specific UST session. |
7520 | * Called with UST session lock held. | |
c4b88406 MD |
7521 | */ |
7522 | static | |
ce34fcd0 | 7523 | int ust_app_flush_session(struct ltt_ust_session *usess) |
c4b88406 MD |
7524 | |
7525 | { | |
99b1411c | 7526 | int ret = 0; |
c4b88406 | 7527 | |
ce34fcd0 | 7528 | DBG("Flushing session buffers for all ust apps"); |
c4b88406 MD |
7529 | |
7530 | rcu_read_lock(); | |
7531 | ||
ce34fcd0 MD |
7532 | /* Flush buffers and push metadata. */ |
7533 | switch (usess->buffer_type) { | |
7534 | case LTTNG_BUFFER_PER_UID: | |
7535 | { | |
7536 | struct buffer_reg_uid *reg; | |
7537 | struct lttng_ht_iter iter; | |
7538 | ||
7539 | /* Flush all per UID buffers associated to that session. */ | |
7540 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7541 | struct ust_registry_session *ust_session_reg; | |
ebdb334b | 7542 | struct buffer_reg_channel *buf_reg_chan; |
ce34fcd0 MD |
7543 | struct consumer_socket *socket; |
7544 | ||
7545 | /* Get consumer socket to use to push the metadata.*/ | |
7546 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
7547 | usess->consumer); | |
7548 | if (!socket) { | |
7549 | /* Ignore request if no consumer is found for the session. */ | |
7550 | continue; | |
7551 | } | |
7552 | ||
7553 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
ebdb334b | 7554 | buf_reg_chan, node.node) { |
ce34fcd0 MD |
7555 | /* |
7556 | * The following call will print error values so the return | |
7557 | * code is of little importance because whatever happens, we | |
7558 | * have to try them all. | |
7559 | */ | |
ebdb334b | 7560 | (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key); |
ce34fcd0 MD |
7561 | } |
7562 | ||
7563 | ust_session_reg = reg->registry->reg.ust; | |
7564 | /* Push metadata. */ | |
7565 | (void) push_metadata(ust_session_reg, usess->consumer); | |
7566 | } | |
ce34fcd0 MD |
7567 | break; |
7568 | } | |
7569 | case LTTNG_BUFFER_PER_PID: | |
7570 | { | |
7571 | struct ust_app_session *ua_sess; | |
7572 | struct lttng_ht_iter iter; | |
7573 | struct ust_app *app; | |
7574 | ||
7575 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
7576 | ua_sess = lookup_session_by_app(usess, app); | |
7577 | if (ua_sess == NULL) { | |
7578 | continue; | |
7579 | } | |
7580 | (void) ust_app_flush_app_session(app, ua_sess); | |
7581 | } | |
7582 | break; | |
7583 | } | |
7584 | default: | |
99b1411c | 7585 | ret = -1; |
ce34fcd0 MD |
7586 | assert(0); |
7587 | break; | |
c4b88406 | 7588 | } |
c4b88406 | 7589 | |
7db205b5 | 7590 | rcu_read_unlock(); |
840cb59c | 7591 | health_code_update(); |
c4b88406 | 7592 | return ret; |
8be98f9a MD |
7593 | } |
7594 | ||
0dd01979 MD |
7595 | static |
7596 | int ust_app_clear_quiescent_app_session(struct ust_app *app, | |
7597 | struct ust_app_session *ua_sess) | |
7598 | { | |
7599 | int ret = 0; | |
7600 | struct lttng_ht_iter iter; | |
7601 | struct ust_app_channel *ua_chan; | |
7602 | struct consumer_socket *socket; | |
7603 | ||
7604 | DBG("Clearing stream quiescent state for ust app pid %d", app->pid); | |
7605 | ||
7606 | rcu_read_lock(); | |
7607 | ||
7608 | if (!app->compatible) { | |
7609 | goto end_not_compatible; | |
7610 | } | |
7611 | ||
7612 | pthread_mutex_lock(&ua_sess->lock); | |
7613 | ||
7614 | if (ua_sess->deleted) { | |
7615 | goto end_unlock; | |
7616 | } | |
7617 | ||
7618 | health_code_update(); | |
7619 | ||
7620 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
7621 | ua_sess->consumer); | |
7622 | if (!socket) { | |
7623 | ERR("Failed to find consumer (%" PRIu32 ") socket", | |
7624 | app->bits_per_long); | |
7625 | ret = -1; | |
7626 | goto end_unlock; | |
7627 | } | |
7628 | ||
7629 | /* Clear quiescent state. */ | |
7630 | switch (ua_sess->buffer_type) { | |
7631 | case LTTNG_BUFFER_PER_PID: | |
7632 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, | |
7633 | ua_chan, node.node) { | |
7634 | health_code_update(); | |
7635 | ret = consumer_clear_quiescent_channel(socket, | |
7636 | ua_chan->key); | |
7637 | if (ret) { | |
7638 | ERR("Error clearing quiescent state for consumer channel"); | |
7639 | ret = -1; | |
7640 | continue; | |
7641 | } | |
7642 | } | |
7643 | break; | |
7644 | case LTTNG_BUFFER_PER_UID: | |
7645 | default: | |
7646 | assert(0); | |
7647 | ret = -1; | |
7648 | break; | |
7649 | } | |
7650 | ||
7651 | health_code_update(); | |
7652 | ||
7653 | end_unlock: | |
7654 | pthread_mutex_unlock(&ua_sess->lock); | |
7655 | ||
7656 | end_not_compatible: | |
7657 | rcu_read_unlock(); | |
7658 | health_code_update(); | |
7659 | return ret; | |
7660 | } | |
7661 | ||
7662 | /* | |
7663 | * Clear quiescent state in each stream for all applications for a | |
7664 | * specific UST session. | |
7665 | * Called with UST session lock held. | |
7666 | */ | |
7667 | static | |
7668 | int ust_app_clear_quiescent_session(struct ltt_ust_session *usess) | |
7669 | ||
7670 | { | |
7671 | int ret = 0; | |
7672 | ||
7673 | DBG("Clearing stream quiescent state for all ust apps"); | |
7674 | ||
7675 | rcu_read_lock(); | |
7676 | ||
7677 | switch (usess->buffer_type) { | |
7678 | case LTTNG_BUFFER_PER_UID: | |
7679 | { | |
7680 | struct lttng_ht_iter iter; | |
7681 | struct buffer_reg_uid *reg; | |
7682 | ||
7683 | /* | |
7684 | * Clear quiescent for all per UID buffers associated to | |
7685 | * that session. | |
7686 | */ | |
7687 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
7688 | struct consumer_socket *socket; | |
ebdb334b | 7689 | struct buffer_reg_channel *buf_reg_chan; |
0dd01979 MD |
7690 | |
7691 | /* Get associated consumer socket.*/ | |
7692 | socket = consumer_find_socket_by_bitness( | |
7693 | reg->bits_per_long, usess->consumer); | |
7694 | if (!socket) { | |
7695 | /* | |
7696 | * Ignore request if no consumer is found for | |
7697 | * the session. | |
7698 | */ | |
7699 | continue; | |
7700 | } | |
7701 | ||
7702 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
ebdb334b | 7703 | &iter.iter, buf_reg_chan, node.node) { |
0dd01979 MD |
7704 | /* |
7705 | * The following call will print error values so | |
7706 | * the return code is of little importance | |
7707 | * because whatever happens, we have to try them | |
7708 | * all. | |
7709 | */ | |
7710 | (void) consumer_clear_quiescent_channel(socket, | |
ebdb334b | 7711 | buf_reg_chan->consumer_key); |
0dd01979 MD |
7712 | } |
7713 | } | |
7714 | break; | |
7715 | } | |
7716 | case LTTNG_BUFFER_PER_PID: | |
7717 | { | |
7718 | struct ust_app_session *ua_sess; | |
7719 | struct lttng_ht_iter iter; | |
7720 | struct ust_app *app; | |
7721 | ||
7722 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, | |
7723 | pid_n.node) { | |
7724 | ua_sess = lookup_session_by_app(usess, app); | |
7725 | if (ua_sess == NULL) { | |
7726 | continue; | |
7727 | } | |
7728 | (void) ust_app_clear_quiescent_app_session(app, | |
7729 | ua_sess); | |
7730 | } | |
7731 | break; | |
7732 | } | |
7733 | default: | |
7734 | ret = -1; | |
7735 | assert(0); | |
7736 | break; | |
7737 | } | |
7738 | ||
7739 | rcu_read_unlock(); | |
7740 | health_code_update(); | |
7741 | return ret; | |
7742 | } | |
7743 | ||
84cd17c6 MD |
7744 | /* |
7745 | * Destroy a specific UST session in apps. | |
7746 | */ | |
3353de95 | 7747 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 | 7748 | { |
ffe60014 | 7749 | int ret; |
84cd17c6 | 7750 | struct ust_app_session *ua_sess; |
bec39940 | 7751 | struct lttng_ht_iter iter; |
d9bf3ca4 | 7752 | struct lttng_ht_node_u64 *node; |
84cd17c6 | 7753 | |
852d0037 | 7754 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
7755 | |
7756 | rcu_read_lock(); | |
7757 | ||
e0c7ec2b DG |
7758 | if (!app->compatible) { |
7759 | goto end; | |
7760 | } | |
7761 | ||
84cd17c6 | 7762 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 7763 | node = lttng_ht_iter_get_node_u64(&iter); |
84cd17c6 | 7764 | if (node == NULL) { |
d42f20df DG |
7765 | /* Session is being or is deleted. */ |
7766 | goto end; | |
84cd17c6 MD |
7767 | } |
7768 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
c4a1715b | 7769 | |
840cb59c | 7770 | health_code_update(); |
d0b96690 | 7771 | destroy_app_session(app, ua_sess); |
84cd17c6 | 7772 | |
840cb59c | 7773 | health_code_update(); |
7db205b5 | 7774 | |
84cd17c6 | 7775 | /* Quiescent wait after stopping trace */ |
fb45065e | 7776 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 7777 | ret = ustctl_wait_quiescent(app->sock); |
fb45065e | 7778 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
7779 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
7780 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
7781 | app->pid, ret); | |
7782 | } | |
e0c7ec2b DG |
7783 | end: |
7784 | rcu_read_unlock(); | |
840cb59c | 7785 | health_code_update(); |
84cd17c6 | 7786 | return 0; |
84cd17c6 MD |
7787 | } |
7788 | ||
5b4a0ec0 DG |
7789 | /* |
7790 | * Start tracing for the UST session. | |
7791 | */ | |
421cb601 DG |
7792 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
7793 | { | |
bec39940 | 7794 | struct lttng_ht_iter iter; |
421cb601 | 7795 | struct ust_app *app; |
48842b30 | 7796 | |
421cb601 DG |
7797 | DBG("Starting all UST traces"); |
7798 | ||
bb2452c8 MD |
7799 | /* |
7800 | * Even though the start trace might fail, flag this session active so | |
7801 | * other application coming in are started by default. | |
7802 | */ | |
7803 | usess->active = 1; | |
7804 | ||
421cb601 | 7805 | rcu_read_lock(); |
421cb601 | 7806 | |
0dd01979 MD |
7807 | /* |
7808 | * In a start-stop-start use-case, we need to clear the quiescent state | |
7809 | * of each channel set by the prior stop command, thus ensuring that a | |
7810 | * following stop or destroy is sure to grab a timestamp_end near those | |
7811 | * operations, even if the packet is empty. | |
7812 | */ | |
7813 | (void) ust_app_clear_quiescent_session(usess); | |
7814 | ||
0498a00c MD |
7815 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
7816 | ust_app_global_update(usess, app); | |
7817 | } | |
7818 | ||
48842b30 DG |
7819 | rcu_read_unlock(); |
7820 | ||
7821 | return 0; | |
7822 | } | |
487cf67c | 7823 | |
8be98f9a MD |
7824 | /* |
7825 | * Start tracing for the UST session. | |
ce34fcd0 | 7826 | * Called with UST session lock held. |
8be98f9a MD |
7827 | */ |
7828 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
7829 | { | |
7830 | int ret = 0; | |
bec39940 | 7831 | struct lttng_ht_iter iter; |
8be98f9a MD |
7832 | struct ust_app *app; |
7833 | ||
7834 | DBG("Stopping all UST traces"); | |
7835 | ||
bb2452c8 MD |
7836 | /* |
7837 | * Even though the stop trace might fail, flag this session inactive so | |
7838 | * other application coming in are not started by default. | |
7839 | */ | |
7840 | usess->active = 0; | |
7841 | ||
8be98f9a MD |
7842 | rcu_read_lock(); |
7843 | ||
b34cbebf MD |
7844 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
7845 | ret = ust_app_stop_trace(usess, app); | |
7846 | if (ret < 0) { | |
7847 | /* Continue to next apps even on error */ | |
7848 | continue; | |
7849 | } | |
7850 | } | |
7851 | ||
ce34fcd0 | 7852 | (void) ust_app_flush_session(usess); |
8be98f9a MD |
7853 | |
7854 | rcu_read_unlock(); | |
7855 | ||
7856 | return 0; | |
7857 | } | |
7858 | ||
84cd17c6 MD |
7859 | /* |
7860 | * Destroy app UST session. | |
7861 | */ | |
7862 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
7863 | { | |
7864 | int ret = 0; | |
bec39940 | 7865 | struct lttng_ht_iter iter; |
84cd17c6 MD |
7866 | struct ust_app *app; |
7867 | ||
7868 | DBG("Destroy all UST traces"); | |
7869 | ||
7870 | rcu_read_lock(); | |
7871 | ||
852d0037 | 7872 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 7873 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
7874 | if (ret < 0) { |
7875 | /* Continue to next apps even on error */ | |
7876 | continue; | |
7877 | } | |
7878 | } | |
7879 | ||
7880 | rcu_read_unlock(); | |
7881 | ||
7882 | return 0; | |
7883 | } | |
7884 | ||
88e3c2f5 | 7885 | /* The ua_sess lock must be held by the caller. */ |
a9ad0c8f | 7886 | static |
88e3c2f5 JG |
7887 | int find_or_create_ust_app_channel( |
7888 | struct ltt_ust_session *usess, | |
7889 | struct ust_app_session *ua_sess, | |
7890 | struct ust_app *app, | |
7891 | struct ltt_ust_channel *uchan, | |
7892 | struct ust_app_channel **ua_chan) | |
487cf67c | 7893 | { |
55c54cce | 7894 | int ret = 0; |
88e3c2f5 JG |
7895 | struct lttng_ht_iter iter; |
7896 | struct lttng_ht_node_str *ua_chan_node; | |
7897 | ||
7898 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter); | |
7899 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
7900 | if (ua_chan_node) { | |
7901 | *ua_chan = caa_container_of(ua_chan_node, | |
7902 | struct ust_app_channel, node); | |
7903 | goto end; | |
7904 | } | |
7905 | ||
7906 | ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan); | |
7907 | if (ret) { | |
7908 | goto end; | |
7909 | } | |
7910 | end: | |
7911 | return ret; | |
7912 | } | |
7913 | ||
ebdb334b JR |
7914 | /* The ua_sess lock must be held by the caller. */ |
7915 | static | |
7916 | int find_or_create_ust_app_map( | |
7917 | struct ltt_ust_session *usess, | |
7918 | struct ust_app_session *ua_sess, | |
7919 | struct ust_app *app, | |
7920 | struct ltt_ust_map *umap, | |
7921 | struct ust_app_map **ua_map) | |
7922 | { | |
7923 | int ret = 0; | |
7924 | struct lttng_ht_iter iter; | |
7925 | struct lttng_ht_node_str *ua_map_node; | |
7926 | ||
7927 | lttng_ht_lookup(ua_sess->maps, (void *) umap->name, &iter); | |
7928 | ua_map_node = lttng_ht_iter_get_node_str(&iter); | |
7929 | if (ua_map_node) { | |
7930 | *ua_map = caa_container_of(ua_map_node, | |
7931 | struct ust_app_map, node); | |
7932 | goto end; | |
7933 | } | |
7934 | ||
7935 | DBG("UST map id = %"PRIu64" not found. Creating it.", umap->id); | |
7936 | ret = ust_app_map_create(usess, ua_sess, umap, app, ua_map); | |
7937 | if (ret) { | |
7938 | goto end; | |
7939 | } | |
7940 | end: | |
7941 | return ret; | |
7942 | } | |
7943 | ||
88e3c2f5 JG |
7944 | static |
7945 | int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan, | |
7946 | struct ltt_ust_event *uevent, struct ust_app_session *ua_sess, | |
7947 | struct ust_app *app) | |
7948 | { | |
7949 | int ret = 0; | |
7950 | struct ust_app_event *ua_event = NULL; | |
7951 | ||
7952 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
ebdb334b JR |
7953 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, |
7954 | uevent->attr.token); | |
88e3c2f5 | 7955 | if (!ua_event) { |
ebdb334b JR |
7956 | ret = create_ust_app_channel_event(ua_sess, ua_chan, uevent, app); |
7957 | ||
88e3c2f5 JG |
7958 | if (ret < 0) { |
7959 | goto end; | |
7960 | } | |
7961 | } else { | |
7962 | if (ua_event->enabled != uevent->enabled) { | |
7963 | ret = uevent->enabled ? | |
7964 | enable_ust_app_event(ua_sess, ua_event, app) : | |
7965 | disable_ust_app_event(ua_sess, ua_event, app); | |
7966 | } | |
7967 | } | |
7968 | ||
7969 | end: | |
7970 | return ret; | |
7971 | } | |
7972 | ||
993578ff | 7973 | /* Called with RCU read-side lock held. */ |
ebdb334b JR |
7974 | static |
7975 | int ust_app_map_synchronize_event(struct ust_app_map *ua_map, | |
7976 | struct ltt_ust_event *uevent, struct ust_app_session *ua_sess, | |
7977 | struct ust_app *app) | |
7978 | { | |
7979 | int ret = 0; | |
7980 | struct ust_app_event *ua_event = NULL; | |
7981 | ||
7982 | ua_event = find_ust_app_event(ua_map->events, uevent->attr.name, | |
7983 | uevent->filter, uevent->attr.loglevel, uevent->exclusion, | |
7984 | uevent->attr.token); | |
7985 | if (!ua_event) { | |
7986 | ret = create_ust_app_map_event(ua_sess, ua_map, uevent, app); | |
7987 | if (ret < 0) { | |
7988 | goto end; | |
7989 | } | |
7990 | } else { | |
7991 | if (ua_event->enabled != uevent->enabled) { | |
7992 | ret = uevent->enabled ? | |
7993 | enable_ust_app_event(ua_sess, ua_event, app) : | |
7994 | disable_ust_app_event(ua_sess, ua_event, app); | |
7995 | } | |
7996 | } | |
7997 | ||
7998 | end: | |
7999 | return ret; | |
8000 | } | |
8001 | ||
993578ff JR |
8002 | static |
8003 | void ust_app_synchronize_event_notifier_rules(struct ust_app *app) | |
8004 | { | |
8005 | int ret = 0; | |
8006 | enum lttng_error_code ret_code; | |
8007 | enum lttng_trigger_status t_status; | |
8008 | struct lttng_ht_iter app_trigger_iter; | |
8009 | struct lttng_triggers *triggers = NULL; | |
8010 | struct ust_app_event_notifier_rule *event_notifier_rule; | |
8011 | unsigned int count, i; | |
8012 | ||
8013 | /* | |
8014 | * Currrently, registering or unregistering a trigger with an | |
8015 | * event rule condition causes a full synchronization of the event | |
8016 | * notifiers. | |
8017 | * | |
8018 | * The first step attempts to add an event notifier for all registered | |
8019 | * triggers that apply to the user space tracers. Then, the | |
8020 | * application's event notifiers rules are all checked against the list | |
8021 | * of registered triggers. Any event notifier that doesn't have a | |
8022 | * matching trigger can be assumed to have been disabled. | |
8023 | * | |
8024 | * All of this is inefficient, but is put in place to get the feature | |
8025 | * rolling as it is simpler at this moment. It will be optimized Soon™ | |
8026 | * to allow the state of enabled | |
8027 | * event notifiers to be synchronized in a piece-wise way. | |
8028 | */ | |
8029 | ||
8030 | /* Get all triggers using uid 0 (root) */ | |
8031 | ret_code = notification_thread_command_list_triggers( | |
8032 | notification_thread_handle, 0, &triggers); | |
8033 | if (ret_code != LTTNG_OK) { | |
8034 | ret = -1; | |
8035 | goto end; | |
8036 | } | |
8037 | ||
8038 | assert(triggers); | |
8039 | ||
8040 | t_status = lttng_triggers_get_count(triggers, &count); | |
8041 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
8042 | ret = -1; | |
8043 | goto end; | |
8044 | } | |
8045 | ||
8046 | for (i = 0; i < count; i++) { | |
ebdb334b JR |
8047 | const struct lttng_condition *condition; |
8048 | const struct lttng_event_rule *event_rule; | |
993578ff JR |
8049 | struct lttng_trigger *trigger; |
8050 | const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule; | |
8051 | enum lttng_condition_status condition_status; | |
8052 | uint64_t token; | |
8053 | ||
8054 | trigger = lttng_triggers_borrow_mutable_at_index(triggers, i); | |
8055 | assert(trigger); | |
8056 | ||
8057 | token = lttng_trigger_get_tracer_token(trigger); | |
ebdb334b | 8058 | condition = lttng_trigger_get_const_condition(trigger); |
993578ff | 8059 | |
ebdb334b | 8060 | if (!lttng_trigger_needs_tracer_notifier(trigger)) { |
993578ff JR |
8061 | continue; |
8062 | } | |
8063 | ||
ebdb334b | 8064 | condition_status = lttng_condition_on_event_get_rule(condition, &event_rule); |
993578ff JR |
8065 | assert(condition_status == LTTNG_CONDITION_STATUS_OK); |
8066 | ||
8067 | if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) { | |
8068 | /* Skip kernel related triggers. */ | |
8069 | continue; | |
8070 | } | |
8071 | ||
8072 | /* | |
8073 | * Find or create the associated token event rule. The caller | |
8074 | * holds the RCU read lock, so this is safe to call without | |
8075 | * explicitly acquiring it here. | |
8076 | */ | |
8077 | looked_up_event_notifier_rule = find_ust_app_event_notifier_rule( | |
8078 | app->token_to_event_notifier_rule_ht, token); | |
8079 | if (!looked_up_event_notifier_rule) { | |
267d66aa | 8080 | ret = create_ust_app_event_notifier_rule(trigger, app); |
993578ff JR |
8081 | if (ret < 0) { |
8082 | goto end; | |
8083 | } | |
8084 | } | |
8085 | } | |
8086 | ||
8087 | rcu_read_lock(); | |
8088 | /* Remove all unknown event sources from the app. */ | |
8089 | cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht, | |
8090 | &app_trigger_iter.iter, event_notifier_rule, | |
8091 | node.node) { | |
8092 | const uint64_t app_token = event_notifier_rule->token; | |
8093 | bool found = false; | |
8094 | ||
8095 | /* | |
8096 | * Check if the app event trigger still exists on the | |
8097 | * notification side. | |
8098 | */ | |
8099 | for (i = 0; i < count; i++) { | |
8100 | uint64_t notification_thread_token; | |
8101 | const struct lttng_trigger *trigger = | |
8102 | lttng_triggers_get_at_index( | |
8103 | triggers, i); | |
8104 | ||
8105 | assert(trigger); | |
8106 | ||
8107 | notification_thread_token = | |
8108 | lttng_trigger_get_tracer_token(trigger); | |
8109 | ||
8110 | if (notification_thread_token == app_token) { | |
8111 | found = true; | |
8112 | break; | |
8113 | } | |
8114 | } | |
8115 | ||
8116 | if (found) { | |
8117 | /* Still valid. */ | |
8118 | continue; | |
8119 | } | |
8120 | ||
8121 | /* | |
8122 | * This trigger was unregistered, disable it on the tracer's | |
8123 | * side. | |
8124 | */ | |
8125 | ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, | |
8126 | &app_trigger_iter); | |
8127 | assert(ret == 0); | |
8128 | ||
8129 | /* Callee logs errors. */ | |
8130 | (void) disable_ust_object(app, event_notifier_rule->obj); | |
8131 | ||
8132 | delete_ust_app_event_notifier_rule( | |
8133 | app->sock, event_notifier_rule, app); | |
8134 | } | |
8135 | ||
8136 | rcu_read_unlock(); | |
8137 | ||
8138 | end: | |
8139 | lttng_triggers_destroy(triggers); | |
8140 | return; | |
8141 | } | |
8142 | ||
88e3c2f5 | 8143 | /* |
ebdb334b | 8144 | * Called with RCU read-side lock held. |
88e3c2f5 JG |
8145 | */ |
8146 | static | |
ebdb334b JR |
8147 | void ust_app_synchronize_all_channels(struct ltt_ust_session *usess, |
8148 | struct ust_app_session *ua_sess, | |
88e3c2f5 JG |
8149 | struct ust_app *app) |
8150 | { | |
8151 | int ret = 0; | |
8152 | struct cds_lfht_iter uchan_iter; | |
8153 | struct ltt_ust_channel *uchan; | |
88e3c2f5 | 8154 | |
ebdb334b | 8155 | assert(usess); |
3d8ca23b | 8156 | assert(ua_sess); |
ebdb334b | 8157 | assert(app); |
ef67c072 | 8158 | |
88e3c2f5 JG |
8159 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter, |
8160 | uchan, node.node) { | |
8161 | struct ust_app_channel *ua_chan; | |
8162 | struct cds_lfht_iter uevent_iter; | |
8163 | struct ltt_ust_event *uevent; | |
487cf67c | 8164 | |
31746f93 | 8165 | /* |
88e3c2f5 JG |
8166 | * Search for a matching ust_app_channel. If none is found, |
8167 | * create it. Creating the channel will cause the ua_chan | |
8168 | * structure to be allocated, the channel buffers to be | |
8169 | * allocated (if necessary) and sent to the application, and | |
8170 | * all enabled contexts will be added to the channel. | |
31746f93 | 8171 | */ |
f3db82be | 8172 | ret = find_or_create_ust_app_channel(usess, ua_sess, |
88e3c2f5 JG |
8173 | app, uchan, &ua_chan); |
8174 | if (ret) { | |
8175 | /* Tracer is probably gone or ENOMEM. */ | |
ebdb334b | 8176 | goto end; |
727d5404 DG |
8177 | } |
8178 | ||
88e3c2f5 JG |
8179 | if (!ua_chan) { |
8180 | /* ua_chan will be NULL for the metadata channel */ | |
8181 | continue; | |
8182 | } | |
727d5404 | 8183 | |
88e3c2f5 | 8184 | cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent, |
bec39940 | 8185 | node.node) { |
88e3c2f5 JG |
8186 | ret = ust_app_channel_synchronize_event(ua_chan, |
8187 | uevent, ua_sess, app); | |
8188 | if (ret) { | |
ebdb334b | 8189 | goto end; |
487cf67c | 8190 | } |
36dc12cc | 8191 | } |
d0b96690 | 8192 | |
88e3c2f5 JG |
8193 | if (ua_chan->enabled != uchan->enabled) { |
8194 | ret = uchan->enabled ? | |
8195 | enable_ust_app_channel(ua_sess, uchan, app) : | |
8196 | disable_ust_app_channel(ua_sess, ua_chan, app); | |
8197 | if (ret) { | |
ebdb334b JR |
8198 | goto end; |
8199 | } | |
8200 | } | |
8201 | } | |
8202 | end: | |
8203 | return; | |
8204 | } | |
8205 | ||
8206 | /* | |
8207 | * Called with RCU read-side lock held. | |
8208 | */ | |
8209 | static | |
8210 | void ust_app_synchronize_all_maps(struct ltt_ust_session *usess, | |
8211 | struct ust_app_session *ua_sess, | |
8212 | struct ust_app *app) | |
8213 | { | |
8214 | int ret = 0; | |
8215 | struct cds_lfht_iter umap_iter; | |
8216 | struct ltt_ust_map *umap; | |
8217 | ||
8218 | assert(usess); | |
8219 | assert(ua_sess); | |
8220 | assert(app); | |
8221 | ||
8222 | cds_lfht_for_each_entry(usess->domain_global.maps->ht, &umap_iter, | |
8223 | umap, node.node) { | |
8224 | struct ust_app_map *ua_map; | |
8225 | struct cds_lfht_iter uevent_iter; | |
8226 | struct ltt_ust_event *uevent; | |
8227 | ||
8228 | DBG("Synchronizing UST map id = %"PRIu64, umap->id); | |
8229 | ||
8230 | ret = find_or_create_ust_app_map(usess, ua_sess, | |
8231 | app, umap, &ua_map); | |
8232 | if (ret) { | |
8233 | /* Tracer is probably gone or ENOMEM. */ | |
8234 | goto end; | |
8235 | } | |
8236 | ||
8237 | DBG("Synchronizing all events of UST map id = %"PRIu64, umap->id); | |
8238 | cds_lfht_for_each_entry(umap->events->ht, &uevent_iter, uevent, | |
8239 | node.node) { | |
8240 | ret = ust_app_map_synchronize_event(ua_map, | |
8241 | uevent, ua_sess, app); | |
8242 | if (ret) { | |
8243 | goto end; | |
8244 | } | |
8245 | } | |
8246 | ||
8247 | if (ua_map->enabled != umap->enabled) { | |
8248 | if (umap->enabled) { | |
8249 | DBG("Map disabled on the tracer side but shouldn't"); | |
8250 | ret = enable_ust_app_map(ua_sess, umap, app); | |
8251 | } else { | |
8252 | DBG("Map enabled on the tracer side but shouldn't"); | |
8253 | ret = disable_ust_app_map(ua_sess, ua_map, app); | |
8254 | } | |
8255 | if (ret) { | |
8256 | goto end; | |
88e3c2f5 JG |
8257 | } |
8258 | } | |
36dc12cc | 8259 | } |
ebdb334b JR |
8260 | end: |
8261 | return; | |
8262 | } | |
8263 | ||
8264 | /* | |
8265 | * The caller must ensure that the application is compatible and is tracked | |
8266 | * by the process attribute trackers. | |
8267 | */ | |
8268 | static | |
8269 | void ust_app_synchronize(struct ltt_ust_session *usess, | |
8270 | struct ust_app *app) | |
8271 | { | |
8272 | int ret = 0; | |
8273 | struct ust_app_session *ua_sess = NULL; | |
8274 | ||
8275 | /* | |
8276 | * The application's configuration should only be synchronized for | |
8277 | * active sessions. | |
8278 | */ | |
8279 | assert(usess->active); | |
8280 | ||
8281 | ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL); | |
8282 | if (ret < 0) { | |
8283 | /* Tracer is probably gone or ENOMEM. */ | |
8284 | goto error; | |
8285 | } | |
8286 | assert(ua_sess); | |
8287 | ||
8288 | ||
8289 | rcu_read_lock(); | |
8290 | ||
8291 | pthread_mutex_lock(&ua_sess->lock); | |
8292 | if (ua_sess->deleted) { | |
8293 | pthread_mutex_unlock(&ua_sess->lock); | |
8294 | goto end; | |
8295 | } | |
8296 | ust_app_synchronize_all_channels(usess, ua_sess, app); | |
8297 | ust_app_synchronize_all_maps(usess, ua_sess, app); | |
ef67c072 JG |
8298 | |
8299 | /* | |
8300 | * Create the metadata for the application. This returns gracefully if a | |
8301 | * metadata was already set for the session. | |
8302 | * | |
8303 | * The metadata channel must be created after the data channels as the | |
8304 | * consumer daemon assumes this ordering. When interacting with a relay | |
8305 | * daemon, the consumer will use this assumption to send the | |
8306 | * "STREAMS_SENT" message to the relay daemon. | |
8307 | */ | |
8308 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); | |
8309 | if (ret < 0) { | |
8310 | goto error_unlock; | |
8311 | } | |
8312 | ||
88e3c2f5 | 8313 | rcu_read_unlock(); |
0498a00c | 8314 | |
a9ad0c8f | 8315 | end: |
88e3c2f5 | 8316 | pthread_mutex_unlock(&ua_sess->lock); |
ffe60014 | 8317 | /* Everything went well at this point. */ |
ffe60014 DG |
8318 | return; |
8319 | ||
d0b96690 | 8320 | error_unlock: |
88e3c2f5 | 8321 | rcu_read_unlock(); |
d0b96690 | 8322 | pthread_mutex_unlock(&ua_sess->lock); |
487cf67c | 8323 | error: |
ffe60014 | 8324 | if (ua_sess) { |
d0b96690 | 8325 | destroy_app_session(app, ua_sess); |
ffe60014 | 8326 | } |
487cf67c DG |
8327 | return; |
8328 | } | |
55cc08a6 | 8329 | |
a9ad0c8f MD |
8330 | static |
8331 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) | |
8332 | { | |
8333 | struct ust_app_session *ua_sess; | |
8334 | ||
8335 | ua_sess = lookup_session_by_app(usess, app); | |
8336 | if (ua_sess == NULL) { | |
8337 | return; | |
8338 | } | |
8339 | destroy_app_session(app, ua_sess); | |
8340 | } | |
8341 | ||
8342 | /* | |
8343 | * Add channels/events from UST global domain to registered apps at sock. | |
8344 | * | |
8345 | * Called with session lock held. | |
8346 | * Called with RCU read-side lock held. | |
8347 | */ | |
8348 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) | |
8349 | { | |
8350 | assert(usess); | |
88e3c2f5 | 8351 | assert(usess->active); |
a9ad0c8f MD |
8352 | |
8353 | DBG2("UST app global update for app sock %d for session id %" PRIu64, | |
8354 | app->sock, usess->id); | |
8355 | ||
8356 | if (!app->compatible) { | |
8357 | return; | |
8358 | } | |
159b042f JG |
8359 | if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, |
8360 | usess, app->pid) && | |
55c9e7ca | 8361 | trace_ust_id_tracker_lookup( |
159b042f JG |
8362 | LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, |
8363 | usess, app->uid) && | |
55c9e7ca | 8364 | trace_ust_id_tracker_lookup( |
159b042f JG |
8365 | LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, |
8366 | usess, app->gid)) { | |
88e3c2f5 JG |
8367 | /* |
8368 | * Synchronize the application's internal tracing configuration | |
8369 | * and start tracing. | |
8370 | */ | |
8371 | ust_app_synchronize(usess, app); | |
8372 | ust_app_start_trace(usess, app); | |
a9ad0c8f MD |
8373 | } else { |
8374 | ust_app_global_destroy(usess, app); | |
8375 | } | |
8376 | } | |
8377 | ||
993578ff JR |
8378 | /* |
8379 | * Add all event notifiers to an application. | |
8380 | * | |
8381 | * Called with session lock held. | |
8382 | * Called with RCU read-side lock held. | |
8383 | */ | |
8384 | void ust_app_global_update_event_notifier_rules(struct ust_app *app) | |
8385 | { | |
8386 | DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)", | |
8387 | app->name, app->ppid); | |
8388 | ||
8389 | if (!app->compatible) { | |
8390 | return; | |
8391 | } | |
8392 | ||
8393 | if (app->event_notifier_group.object == NULL) { | |
8394 | WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)", | |
8395 | app->name, app->ppid); | |
8396 | return; | |
8397 | } | |
8398 | ||
8399 | ust_app_synchronize_event_notifier_rules(app); | |
8400 | } | |
8401 | ||
a9ad0c8f MD |
8402 | /* |
8403 | * Called with session lock held. | |
8404 | */ | |
8405 | void ust_app_global_update_all(struct ltt_ust_session *usess) | |
8406 | { | |
8407 | struct lttng_ht_iter iter; | |
8408 | struct ust_app *app; | |
8409 | ||
8410 | rcu_read_lock(); | |
8411 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
8412 | ust_app_global_update(usess, app); | |
8413 | } | |
8414 | rcu_read_unlock(); | |
8415 | } | |
8416 | ||
993578ff JR |
8417 | void ust_app_global_update_all_event_notifier_rules(void) |
8418 | { | |
8419 | struct lttng_ht_iter iter; | |
8420 | struct ust_app *app; | |
8421 | ||
8422 | rcu_read_lock(); | |
8423 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
8424 | ust_app_global_update_event_notifier_rules(app); | |
8425 | } | |
8426 | ||
8427 | rcu_read_unlock(); | |
8428 | } | |
8429 | ||
ebdb334b JR |
8430 | void ust_app_update_event_notifier_error_count(struct lttng_trigger *trigger) |
8431 | { | |
8432 | uint64_t error_count = 0; | |
8433 | enum event_notifier_error_accounting_status status; | |
8434 | struct lttng_condition *condition = lttng_trigger_get_condition(trigger); | |
8435 | ||
8436 | status = event_notifier_error_accounting_get_count(trigger, &error_count); | |
8437 | if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) { | |
8438 | ERR("Error getting trigger error count."); | |
8439 | } | |
8440 | ||
8441 | lttng_condition_on_event_set_error_count(condition, error_count); | |
8442 | } | |
8443 | ||
55cc08a6 DG |
8444 | /* |
8445 | * Add context to a specific channel for global UST domain. | |
8446 | */ | |
8447 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
8448 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
8449 | { | |
8450 | int ret = 0; | |
bec39940 DG |
8451 | struct lttng_ht_node_str *ua_chan_node; |
8452 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
8453 | struct ust_app_channel *ua_chan = NULL; |
8454 | struct ust_app_session *ua_sess; | |
8455 | struct ust_app *app; | |
8456 | ||
88e3c2f5 | 8457 | assert(usess->active); |
0498a00c | 8458 | |
55cc08a6 | 8459 | rcu_read_lock(); |
852d0037 | 8460 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
8461 | if (!app->compatible) { |
8462 | /* | |
8463 | * TODO: In time, we should notice the caller of this error by | |
8464 | * telling him that this is a version error. | |
8465 | */ | |
8466 | continue; | |
8467 | } | |
55cc08a6 DG |
8468 | ua_sess = lookup_session_by_app(usess, app); |
8469 | if (ua_sess == NULL) { | |
8470 | continue; | |
8471 | } | |
8472 | ||
d0b96690 | 8473 | pthread_mutex_lock(&ua_sess->lock); |
b161602a MD |
8474 | |
8475 | if (ua_sess->deleted) { | |
8476 | pthread_mutex_unlock(&ua_sess->lock); | |
8477 | continue; | |
8478 | } | |
8479 | ||
55cc08a6 | 8480 | /* Lookup channel in the ust app session */ |
bec39940 DG |
8481 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
8482 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 | 8483 | if (ua_chan_node == NULL) { |
d0b96690 | 8484 | goto next_app; |
55cc08a6 DG |
8485 | } |
8486 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
8487 | node); | |
c9edf082 | 8488 | ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app); |
55cc08a6 | 8489 | if (ret < 0) { |
d0b96690 | 8490 | goto next_app; |
55cc08a6 | 8491 | } |
d0b96690 DG |
8492 | next_app: |
8493 | pthread_mutex_unlock(&ua_sess->lock); | |
55cc08a6 DG |
8494 | } |
8495 | ||
55cc08a6 | 8496 | rcu_read_unlock(); |
76d45b40 DG |
8497 | return ret; |
8498 | } | |
7f79d3a1 | 8499 | |
d0b96690 DG |
8500 | /* |
8501 | * Receive registration and populate the given msg structure. | |
8502 | * | |
8503 | * On success return 0 else a negative value returned by the ustctl call. | |
8504 | */ | |
8505 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) | |
8506 | { | |
8507 | int ret; | |
8508 | uint32_t pid, ppid, uid, gid; | |
8509 | ||
8510 | assert(msg); | |
8511 | ||
8512 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, | |
8513 | &pid, &ppid, &uid, &gid, | |
8514 | &msg->bits_per_long, | |
8515 | &msg->uint8_t_alignment, | |
8516 | &msg->uint16_t_alignment, | |
8517 | &msg->uint32_t_alignment, | |
8518 | &msg->uint64_t_alignment, | |
8519 | &msg->long_alignment, | |
8520 | &msg->byte_order, | |
8521 | msg->name); | |
8522 | if (ret < 0) { | |
8523 | switch (-ret) { | |
8524 | case EPIPE: | |
8525 | case ECONNRESET: | |
8526 | case LTTNG_UST_ERR_EXITING: | |
8527 | DBG3("UST app recv reg message failed. Application died"); | |
8528 | break; | |
8529 | case LTTNG_UST_ERR_UNSUP_MAJOR: | |
8530 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", | |
8531 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, | |
8532 | LTTNG_UST_ABI_MINOR_VERSION); | |
8533 | break; | |
8534 | default: | |
8535 | ERR("UST app recv reg message failed with ret %d", ret); | |
8536 | break; | |
8537 | } | |
8538 | goto error; | |
8539 | } | |
8540 | msg->pid = (pid_t) pid; | |
8541 | msg->ppid = (pid_t) ppid; | |
8542 | msg->uid = (uid_t) uid; | |
8543 | msg->gid = (gid_t) gid; | |
8544 | ||
8545 | error: | |
8546 | return ret; | |
8547 | } | |
8548 | ||
10b56aef MD |
8549 | /* |
8550 | * Return a ust app session object using the application object and the | |
8551 | * session object descriptor has a key. If not found, NULL is returned. | |
8552 | * A RCU read side lock MUST be acquired when calling this function. | |
8553 | */ | |
8554 | static struct ust_app_session *find_session_by_objd(struct ust_app *app, | |
8555 | int objd) | |
8556 | { | |
8557 | struct lttng_ht_node_ulong *node; | |
8558 | struct lttng_ht_iter iter; | |
8559 | struct ust_app_session *ua_sess = NULL; | |
8560 | ||
8561 | assert(app); | |
8562 | ||
8563 | lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter); | |
8564 | node = lttng_ht_iter_get_node_ulong(&iter); | |
8565 | if (node == NULL) { | |
8566 | DBG2("UST app session find by objd %d not found", objd); | |
8567 | goto error; | |
8568 | } | |
8569 | ||
8570 | ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node); | |
8571 | ||
8572 | error: | |
ebdb334b JR |
8573 | return ua_sess; |
8574 | } | |
8575 | ||
8576 | /* | |
8577 | * Return a ust app channel object using the application object and the channel | |
8578 | * object descriptor has a key. If not found, NULL is returned. A RCU read side | |
8579 | * lock MUST be acquired before calling this function. | |
8580 | */ | |
8581 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, | |
8582 | int objd) | |
8583 | { | |
8584 | struct lttng_ht_node_ulong *node; | |
8585 | struct lttng_ht_iter iter; | |
8586 | struct ust_app_channel *ua_chan = NULL; | |
8587 | ||
8588 | assert(app); | |
8589 | ||
8590 | lttng_ht_lookup(app->ust_chan_objd, (void *)((unsigned long) objd), &iter); | |
8591 | node = lttng_ht_iter_get_node_ulong(&iter); | |
8592 | if (node == NULL) { | |
8593 | DBG2("UST app channel find by objd %d not found", objd); | |
8594 | goto error; | |
8595 | } | |
8596 | ||
8597 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); | |
8598 | ||
8599 | error: | |
8600 | return ua_chan; | |
10b56aef MD |
8601 | } |
8602 | ||
d88aee68 | 8603 | /* |
ebdb334b | 8604 | * Return a ust app map object using the application object and the map |
d88aee68 DG |
8605 | * object descriptor has a key. If not found, NULL is returned. A RCU read side |
8606 | * lock MUST be acquired before calling this function. | |
8607 | */ | |
ebdb334b | 8608 | static struct ust_app_map *find_map_by_objd(struct ust_app *app, |
d0b96690 DG |
8609 | int objd) |
8610 | { | |
8611 | struct lttng_ht_node_ulong *node; | |
8612 | struct lttng_ht_iter iter; | |
ebdb334b | 8613 | struct ust_app_map *ua_map = NULL; |
d0b96690 DG |
8614 | |
8615 | assert(app); | |
8616 | ||
ebdb334b | 8617 | lttng_ht_lookup(app->ust_map_objd, (void *)((unsigned long) objd), &iter); |
d0b96690 DG |
8618 | node = lttng_ht_iter_get_node_ulong(&iter); |
8619 | if (node == NULL) { | |
ebdb334b | 8620 | DBG2("UST app map find by objd %d not found", objd); |
d0b96690 DG |
8621 | goto error; |
8622 | } | |
8623 | ||
ebdb334b | 8624 | ua_map = caa_container_of(node, struct ust_app_map, ust_objd_node); |
d0b96690 DG |
8625 | |
8626 | error: | |
ebdb334b | 8627 | return ua_map; |
d0b96690 DG |
8628 | } |
8629 | ||
d88aee68 DG |
8630 | /* |
8631 | * Reply to a register channel notification from an application on the notify | |
8632 | * socket. The channel metadata is also created. | |
8633 | * | |
8634 | * The session UST registry lock is acquired in this function. | |
8635 | * | |
8636 | * On success 0 is returned else a negative value. | |
8637 | */ | |
8eede835 | 8638 | static int reply_ust_register_channel(int sock, int cobjd, |
d0b96690 DG |
8639 | size_t nr_fields, struct ustctl_field *fields) |
8640 | { | |
8641 | int ret, ret_code = 0; | |
294e218e | 8642 | uint32_t chan_id; |
7972aab2 | 8643 | uint64_t chan_reg_key; |
d0b96690 DG |
8644 | enum ustctl_channel_header type; |
8645 | struct ust_app *app; | |
8646 | struct ust_app_channel *ua_chan; | |
8647 | struct ust_app_session *ua_sess; | |
7972aab2 | 8648 | struct ust_registry_session *registry; |
ebdb334b | 8649 | struct ust_registry_channel *ust_reg_chan; |
d0b96690 DG |
8650 | |
8651 | rcu_read_lock(); | |
8652 | ||
8653 | /* Lookup application. If not found, there is a code flow error. */ | |
8654 | app = find_app_by_notify_sock(sock); | |
d88aee68 | 8655 | if (!app) { |
fad1ed2f | 8656 | DBG("Application socket %d is being torn down. Abort event notify", |
d88aee68 DG |
8657 | sock); |
8658 | ret = 0; | |
8659 | goto error_rcu_unlock; | |
8660 | } | |
d0b96690 | 8661 | |
4950b860 | 8662 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 8663 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 | 8664 | if (!ua_chan) { |
fad1ed2f | 8665 | DBG("Application channel is being torn down. Abort event notify"); |
4950b860 MD |
8666 | ret = 0; |
8667 | goto error_rcu_unlock; | |
8668 | } | |
8669 | ||
d0b96690 DG |
8670 | assert(ua_chan->session); |
8671 | ua_sess = ua_chan->session; | |
d0b96690 | 8672 | |
7972aab2 DG |
8673 | /* Get right session registry depending on the session buffer type. */ |
8674 | registry = get_session_registry(ua_sess); | |
fad1ed2f JR |
8675 | if (!registry) { |
8676 | DBG("Application session is being torn down. Abort event notify"); | |
8677 | ret = 0; | |
8678 | goto error_rcu_unlock; | |
8679 | }; | |
45893984 | 8680 | |
7972aab2 DG |
8681 | /* Depending on the buffer type, a different channel key is used. */ |
8682 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
8683 | chan_reg_key = ua_chan->tracing_channel_id; | |
d0b96690 | 8684 | } else { |
7972aab2 | 8685 | chan_reg_key = ua_chan->key; |
d0b96690 DG |
8686 | } |
8687 | ||
7972aab2 DG |
8688 | pthread_mutex_lock(®istry->lock); |
8689 | ||
ebdb334b JR |
8690 | ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key); |
8691 | assert(ust_reg_chan); | |
7972aab2 | 8692 | |
ebdb334b | 8693 | if (!ust_reg_chan->register_done) { |
294e218e MD |
8694 | /* |
8695 | * TODO: eventually use the registry event count for | |
8696 | * this channel to better guess header type for per-pid | |
8697 | * buffers. | |
8698 | */ | |
8699 | type = USTCTL_CHANNEL_HEADER_LARGE; | |
ebdb334b JR |
8700 | ust_reg_chan->nr_ctx_fields = nr_fields; |
8701 | ust_reg_chan->ctx_fields = fields; | |
fad1ed2f | 8702 | fields = NULL; |
ebdb334b | 8703 | ust_reg_chan->header_type = type; |
d0b96690 | 8704 | } else { |
7972aab2 | 8705 | /* Get current already assigned values. */ |
ebdb334b | 8706 | type = ust_reg_chan->header_type; |
d0b96690 | 8707 | } |
7972aab2 | 8708 | /* Channel id is set during the object creation. */ |
ebdb334b | 8709 | chan_id = ust_reg_chan->chan_id; |
d0b96690 DG |
8710 | |
8711 | /* Append to metadata */ | |
ebdb334b JR |
8712 | if (!ust_reg_chan->metadata_dumped) { |
8713 | ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan); | |
d0b96690 DG |
8714 | if (ret_code) { |
8715 | ERR("Error appending channel metadata (errno = %d)", ret_code); | |
8716 | goto reply; | |
8717 | } | |
8718 | } | |
8719 | ||
8720 | reply: | |
7972aab2 DG |
8721 | DBG3("UST app replying to register channel key %" PRIu64 |
8722 | " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type, | |
8723 | ret_code); | |
d0b96690 DG |
8724 | |
8725 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); | |
8726 | if (ret < 0) { | |
8727 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
8728 | ERR("UST app reply channel failed with ret %d", ret); | |
8729 | } else { | |
8730 | DBG3("UST app reply channel failed. Application died"); | |
8731 | } | |
8732 | goto error; | |
8733 | } | |
8734 | ||
7972aab2 | 8735 | /* This channel registry registration is completed. */ |
ebdb334b | 8736 | ust_reg_chan->register_done = 1; |
7972aab2 | 8737 | |
d0b96690 | 8738 | error: |
7972aab2 | 8739 | pthread_mutex_unlock(®istry->lock); |
d88aee68 | 8740 | error_rcu_unlock: |
d0b96690 | 8741 | rcu_read_unlock(); |
fad1ed2f | 8742 | free(fields); |
d0b96690 DG |
8743 | return ret; |
8744 | } | |
8745 | ||
ebdb334b JR |
8746 | static int add_event_ust_chan_registry(int sock, struct ust_app *ua, |
8747 | struct ust_app_channel *ua_chan, int sobjd, int cobjd, char *name, | |
2106efa0 PP |
8748 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
8749 | int loglevel_value, char *model_emf_uri) | |
d0b96690 DG |
8750 | { |
8751 | int ret, ret_code; | |
8752 | uint32_t event_id = 0; | |
7972aab2 | 8753 | uint64_t chan_reg_key; |
d0b96690 | 8754 | struct ust_app_session *ua_sess; |
7972aab2 | 8755 | struct ust_registry_session *registry; |
ebdb334b JR |
8756 | /* |
8757 | * The counter index is unused for channel events. It's only used for | |
8758 | * map events. | |
8759 | */ | |
8760 | uint64_t counter_index = 0; | |
4950b860 | 8761 | |
d0b96690 DG |
8762 | assert(ua_chan->session); |
8763 | ua_sess = ua_chan->session; | |
8764 | ||
7972aab2 | 8765 | registry = get_session_registry(ua_sess); |
fad1ed2f JR |
8766 | if (!registry) { |
8767 | DBG("Application session is being torn down. Abort event notify"); | |
8768 | ret = 0; | |
ebdb334b | 8769 | goto error; |
fad1ed2f | 8770 | } |
7972aab2 DG |
8771 | |
8772 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
8773 | chan_reg_key = ua_chan->tracing_channel_id; | |
8774 | } else { | |
8775 | chan_reg_key = ua_chan->key; | |
8776 | } | |
8777 | ||
8778 | pthread_mutex_lock(®istry->lock); | |
d0b96690 | 8779 | |
d5d629b5 DG |
8780 | /* |
8781 | * From this point on, this call acquires the ownership of the sig, fields | |
8782 | * and model_emf_uri meaning any free are done inside it if needed. These | |
8783 | * three variables MUST NOT be read/write after this. | |
8784 | */ | |
ebdb334b | 8785 | ret_code = ust_registry_chan_create_event(registry, chan_reg_key, |
2106efa0 PP |
8786 | sobjd, cobjd, name, sig, nr_fields, fields, |
8787 | loglevel_value, model_emf_uri, ua_sess->buffer_type, | |
ebdb334b | 8788 | &event_id, ua); |
fad1ed2f JR |
8789 | sig = NULL; |
8790 | fields = NULL; | |
8791 | model_emf_uri = NULL; | |
d0b96690 DG |
8792 | |
8793 | /* | |
8794 | * The return value is returned to ustctl so in case of an error, the | |
8795 | * application can be notified. In case of an error, it's important not to | |
8796 | * return a negative error or else the application will get closed. | |
8797 | */ | |
ebdb334b | 8798 | ret = ustctl_reply_register_event(sock, event_id, counter_index, ret_code); |
d0b96690 DG |
8799 | if (ret < 0) { |
8800 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
8801 | ERR("UST app reply event failed with ret %d", ret); | |
8802 | } else { | |
8803 | DBG3("UST app reply event failed. Application died"); | |
8804 | } | |
8805 | /* | |
8806 | * No need to wipe the create event since the application socket will | |
8807 | * get close on error hence cleaning up everything by itself. | |
8808 | */ | |
8809 | goto error; | |
8810 | } | |
8811 | ||
7972aab2 DG |
8812 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
8813 | name, event_id); | |
d88aee68 | 8814 | |
d0b96690 | 8815 | error: |
7972aab2 | 8816 | pthread_mutex_unlock(®istry->lock); |
ebdb334b JR |
8817 | return ret; |
8818 | } | |
8819 | ||
8820 | static int add_event_ust_map_registry(int sock, struct ust_app *ua, | |
8821 | struct ust_app_map *ua_map, int sobjd, int cobjd, char *name, | |
8822 | char *sig, size_t nr_fields, struct ustctl_field *fields, | |
8823 | int loglevel_value, char *model_emf_uri, uint64_t tracer_token) | |
8824 | { | |
8825 | int ret, ret_code; | |
8826 | uint64_t map_reg_key, counter_index; | |
8827 | struct ust_app_session *ua_sess; | |
8828 | struct ust_registry_session *registry; | |
8829 | ||
8830 | assert(ua_map->session); | |
8831 | ua_sess = ua_map->session; | |
8832 | ||
8833 | registry = get_session_registry(ua_sess); | |
8834 | if (!registry) { | |
8835 | DBG("Application session is being torn down. Abort event notify"); | |
8836 | ret = 0; | |
8837 | goto error; | |
8838 | } | |
8839 | ||
8840 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
8841 | map_reg_key = ua_map->tracing_map_id; | |
8842 | } else { | |
8843 | map_reg_key = ua_map->key; | |
8844 | } | |
8845 | ||
8846 | pthread_mutex_lock(®istry->lock); | |
8847 | ||
8848 | /* | |
8849 | * From this point on, this call acquires the ownership of the sig, fields | |
8850 | * and model_emf_uri meaning any free are done inside it if needed. These | |
8851 | * three variables MUST NOT be read/write after this. | |
8852 | */ | |
8853 | DBG("Registry_map_create_event on map=%"PRIu64" with token=%"PRIu64, | |
8854 | map_reg_key, tracer_token); | |
8855 | ret_code = ust_registry_map_create_event(registry, map_reg_key, | |
8856 | sobjd, cobjd, name, sig, nr_fields, fields, | |
8857 | loglevel_value, model_emf_uri, ua_sess->buffer_type, | |
8858 | tracer_token, &counter_index, ua); | |
8859 | assert(!ret_code); | |
8860 | ||
8861 | sig = NULL; | |
8862 | fields = NULL; | |
8863 | model_emf_uri = NULL; | |
8864 | ||
8865 | /* | |
8866 | * The return value is returned to ustctl so in case of an error, the | |
8867 | * application can be notified. In case of an error, it's important not to | |
8868 | * return a negative error or else the application will get closed. | |
8869 | */ | |
8870 | ret = ustctl_reply_register_event(sock, counter_index, counter_index, | |
8871 | ret_code); | |
8872 | if (ret < 0) { | |
8873 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
8874 | ERR("UST app reply event failed with ret %d", ret); | |
8875 | } else { | |
8876 | DBG3("UST app reply event failed. Application died"); | |
8877 | } | |
8878 | /* | |
8879 | * No need to wipe the create event since the application socket will | |
8880 | * get close on error hence cleaning up everything by itself. | |
8881 | */ | |
8882 | goto error; | |
8883 | } | |
8884 | ||
8885 | DBG3("UST registry map event %s with counter index %" PRIu64 " added successfully", | |
8886 | name, counter_index); | |
8887 | ||
8888 | error: | |
8889 | pthread_mutex_unlock(®istry->lock); | |
8890 | return ret; | |
8891 | } | |
8892 | ||
8893 | ||
8894 | /* | |
8895 | * Add event to the UST channel registry. When the event is added to the | |
8896 | * registry, the metadata is also created. Once done, this replies to the | |
8897 | * application with the appropriate error code. | |
8898 | * | |
8899 | * The session UST registry lock is acquired in the function. | |
8900 | * | |
8901 | * On success 0 is returned else a negative value. | |
8902 | */ | |
8903 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, | |
8904 | char *sig, size_t nr_fields, struct ustctl_field *fields, | |
8905 | int loglevel_value, char *model_emf_uri, uint64_t tracer_token) | |
8906 | { | |
8907 | int ret; | |
8908 | struct ust_app *app; | |
8909 | struct ust_app_channel *ua_chan = NULL; | |
8910 | struct ust_app_map *ua_map = NULL; | |
8911 | ||
8912 | rcu_read_lock(); | |
8913 | ||
8914 | /* Lookup application. If not found, there is a code flow error. */ | |
8915 | app = find_app_by_notify_sock(sock); | |
8916 | if (!app) { | |
8917 | DBG("Application socket %d is being torn down. Abort event notify", | |
8918 | sock); | |
8919 | ret = 0; | |
8920 | goto end; | |
8921 | } | |
8922 | ||
8923 | /* Lookup channel by UST object descriptor. */ | |
8924 | ua_chan = find_channel_by_objd(app, cobjd); | |
8925 | if (ua_chan) { | |
8926 | ret = add_event_ust_chan_registry(sock, app, ua_chan, sobjd, cobjd, | |
8927 | name, sig, nr_fields, fields, loglevel_value, | |
8928 | model_emf_uri); | |
8929 | if (ret) { | |
8930 | ERR("Error adding channel event to registry: event_name = '%s'", name); | |
8931 | } | |
8932 | goto found; | |
8933 | } | |
8934 | ||
8935 | /* Lookup map by UST object descriptor. */ | |
8936 | ua_map = find_map_by_objd(app, cobjd); | |
8937 | if (ua_map) { | |
8938 | ret = add_event_ust_map_registry(sock, app, ua_map, sobjd, cobjd, | |
8939 | name, sig, nr_fields, fields, loglevel_value, | |
8940 | model_emf_uri, tracer_token); | |
8941 | if (ret) { | |
8942 | ERR("Error adding map event to registry: event_name = '%s'", name); | |
8943 | goto end; | |
8944 | } | |
8945 | goto found; | |
8946 | } | |
8947 | ||
8948 | if (!ua_chan && !ua_map) { | |
8949 | DBG("Application event container is being torn down. Abort event notify"); | |
8950 | ret = 0; | |
8951 | goto end; | |
8952 | } | |
8953 | ||
8954 | found: | |
8955 | ret = 0; | |
8956 | ||
8957 | end: | |
d0b96690 DG |
8958 | rcu_read_unlock(); |
8959 | return ret; | |
8960 | } | |
8961 | ||
10b56aef MD |
8962 | /* |
8963 | * Add enum to the UST session registry. Once done, this replies to the | |
8964 | * application with the appropriate error code. | |
8965 | * | |
8966 | * The session UST registry lock is acquired within this function. | |
8967 | * | |
8968 | * On success 0 is returned else a negative value. | |
8969 | */ | |
8970 | static int add_enum_ust_registry(int sock, int sobjd, char *name, | |
8971 | struct ustctl_enum_entry *entries, size_t nr_entries) | |
8972 | { | |
8973 | int ret = 0, ret_code; | |
8974 | struct ust_app *app; | |
8975 | struct ust_app_session *ua_sess; | |
8976 | struct ust_registry_session *registry; | |
8977 | uint64_t enum_id = -1ULL; | |
8978 | ||
8979 | rcu_read_lock(); | |
8980 | ||
8981 | /* Lookup application. If not found, there is a code flow error. */ | |
8982 | app = find_app_by_notify_sock(sock); | |
8983 | if (!app) { | |
8984 | /* Return an error since this is not an error */ | |
8985 | DBG("Application socket %d is being torn down. Aborting enum registration", | |
8986 | sock); | |
8987 | free(entries); | |
8988 | goto error_rcu_unlock; | |
8989 | } | |
8990 | ||
8991 | /* Lookup session by UST object descriptor. */ | |
8992 | ua_sess = find_session_by_objd(app, sobjd); | |
8993 | if (!ua_sess) { | |
8994 | /* Return an error since this is not an error */ | |
fad1ed2f | 8995 | DBG("Application session is being torn down (session not found). Aborting enum registration."); |
10b56aef MD |
8996 | free(entries); |
8997 | goto error_rcu_unlock; | |
8998 | } | |
8999 | ||
9000 | registry = get_session_registry(ua_sess); | |
fad1ed2f JR |
9001 | if (!registry) { |
9002 | DBG("Application session is being torn down (registry not found). Aborting enum registration."); | |
9003 | free(entries); | |
9004 | goto error_rcu_unlock; | |
9005 | } | |
10b56aef MD |
9006 | |
9007 | pthread_mutex_lock(®istry->lock); | |
9008 | ||
9009 | /* | |
9010 | * From this point on, the callee acquires the ownership of | |
9011 | * entries. The variable entries MUST NOT be read/written after | |
9012 | * call. | |
9013 | */ | |
9014 | ret_code = ust_registry_create_or_find_enum(registry, sobjd, name, | |
9015 | entries, nr_entries, &enum_id); | |
9016 | entries = NULL; | |
9017 | ||
9018 | /* | |
9019 | * The return value is returned to ustctl so in case of an error, the | |
9020 | * application can be notified. In case of an error, it's important not to | |
9021 | * return a negative error or else the application will get closed. | |
9022 | */ | |
9023 | ret = ustctl_reply_register_enum(sock, enum_id, ret_code); | |
9024 | if (ret < 0) { | |
9025 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
9026 | ERR("UST app reply enum failed with ret %d", ret); | |
9027 | } else { | |
9028 | DBG3("UST app reply enum failed. Application died"); | |
9029 | } | |
9030 | /* | |
9031 | * No need to wipe the create enum since the application socket will | |
9032 | * get close on error hence cleaning up everything by itself. | |
9033 | */ | |
9034 | goto error; | |
9035 | } | |
9036 | ||
9037 | DBG3("UST registry enum %s added successfully or already found", name); | |
9038 | ||
9039 | error: | |
9040 | pthread_mutex_unlock(®istry->lock); | |
9041 | error_rcu_unlock: | |
9042 | rcu_read_unlock(); | |
9043 | return ret; | |
9044 | } | |
9045 | ||
d88aee68 DG |
9046 | /* |
9047 | * Handle application notification through the given notify socket. | |
9048 | * | |
9049 | * Return 0 on success or else a negative value. | |
9050 | */ | |
d0b96690 DG |
9051 | int ust_app_recv_notify(int sock) |
9052 | { | |
9053 | int ret; | |
9054 | enum ustctl_notify_cmd cmd; | |
9055 | ||
9056 | DBG3("UST app receiving notify from sock %d", sock); | |
9057 | ||
9058 | ret = ustctl_recv_notify(sock, &cmd); | |
9059 | if (ret < 0) { | |
9060 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
9061 | ERR("UST app recv notify failed with ret %d", ret); | |
9062 | } else { | |
9063 | DBG3("UST app recv notify failed. Application died"); | |
9064 | } | |
9065 | goto error; | |
9066 | } | |
9067 | ||
9068 | switch (cmd) { | |
9069 | case USTCTL_NOTIFY_CMD_EVENT: | |
9070 | { | |
2106efa0 | 9071 | int sobjd, cobjd, loglevel_value; |
d0b96690 DG |
9072 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
9073 | size_t nr_fields; | |
ebdb334b | 9074 | uint64_t tracer_token = 0; |
d0b96690 DG |
9075 | struct ustctl_field *fields; |
9076 | ||
9077 | DBG2("UST app ustctl register event received"); | |
9078 | ||
2106efa0 PP |
9079 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, |
9080 | &loglevel_value, &sig, &nr_fields, &fields, | |
ebdb334b | 9081 | &model_emf_uri, &tracer_token); |
d0b96690 DG |
9082 | if (ret < 0) { |
9083 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
9084 | ERR("UST app recv event failed with ret %d", ret); | |
9085 | } else { | |
9086 | DBG3("UST app recv event failed. Application died"); | |
9087 | } | |
9088 | goto error; | |
9089 | } | |
9090 | ||
d5d629b5 DG |
9091 | /* |
9092 | * Add event to the UST registry coming from the notify socket. This | |
9093 | * call will free if needed the sig, fields and model_emf_uri. This | |
9094 | * code path loses the ownsership of these variables and transfer them | |
9095 | * to the this function. | |
9096 | */ | |
d0b96690 | 9097 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
ebdb334b | 9098 | fields, loglevel_value, model_emf_uri, tracer_token); |
d0b96690 DG |
9099 | if (ret < 0) { |
9100 | goto error; | |
9101 | } | |
9102 | ||
9103 | break; | |
9104 | } | |
9105 | case USTCTL_NOTIFY_CMD_CHANNEL: | |
9106 | { | |
9107 | int sobjd, cobjd; | |
9108 | size_t nr_fields; | |
9109 | struct ustctl_field *fields; | |
9110 | ||
9111 | DBG2("UST app ustctl register channel received"); | |
9112 | ||
9113 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, | |
9114 | &fields); | |
9115 | if (ret < 0) { | |
9116 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
9117 | ERR("UST app recv channel failed with ret %d", ret); | |
9118 | } else { | |
9119 | DBG3("UST app recv channel failed. Application died"); | |
9120 | } | |
9121 | goto error; | |
9122 | } | |
9123 | ||
d5d629b5 DG |
9124 | /* |
9125 | * The fields ownership are transfered to this function call meaning | |
9126 | * that if needed it will be freed. After this, it's invalid to access | |
9127 | * fields or clean it up. | |
9128 | */ | |
8eede835 | 9129 | ret = reply_ust_register_channel(sock, cobjd, nr_fields, |
d0b96690 DG |
9130 | fields); |
9131 | if (ret < 0) { | |
9132 | goto error; | |
9133 | } | |
9134 | ||
9135 | break; | |
9136 | } | |
10b56aef MD |
9137 | case USTCTL_NOTIFY_CMD_ENUM: |
9138 | { | |
9139 | int sobjd; | |
9140 | char name[LTTNG_UST_SYM_NAME_LEN]; | |
9141 | size_t nr_entries; | |
9142 | struct ustctl_enum_entry *entries; | |
9143 | ||
9144 | DBG2("UST app ustctl register enum received"); | |
9145 | ||
9146 | ret = ustctl_recv_register_enum(sock, &sobjd, name, | |
9147 | &entries, &nr_entries); | |
9148 | if (ret < 0) { | |
9149 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
9150 | ERR("UST app recv enum failed with ret %d", ret); | |
9151 | } else { | |
9152 | DBG3("UST app recv enum failed. Application died"); | |
9153 | } | |
9154 | goto error; | |
9155 | } | |
9156 | ||
9157 | /* Callee assumes ownership of entries */ | |
9158 | ret = add_enum_ust_registry(sock, sobjd, name, | |
9159 | entries, nr_entries); | |
9160 | if (ret < 0) { | |
9161 | goto error; | |
9162 | } | |
9163 | ||
9164 | break; | |
9165 | } | |
d0b96690 DG |
9166 | default: |
9167 | /* Should NEVER happen. */ | |
9168 | assert(0); | |
9169 | } | |
9170 | ||
9171 | error: | |
9172 | return ret; | |
9173 | } | |
d88aee68 DG |
9174 | |
9175 | /* | |
9176 | * Once the notify socket hangs up, this is called. First, it tries to find the | |
9177 | * corresponding application. On failure, the call_rcu to close the socket is | |
9178 | * executed. If an application is found, it tries to delete it from the notify | |
9179 | * socket hash table. Whathever the result, it proceeds to the call_rcu. | |
9180 | * | |
9181 | * Note that an object needs to be allocated here so on ENOMEM failure, the | |
9182 | * call RCU is not done but the rest of the cleanup is. | |
9183 | */ | |
9184 | void ust_app_notify_sock_unregister(int sock) | |
9185 | { | |
9186 | int err_enomem = 0; | |
9187 | struct lttng_ht_iter iter; | |
9188 | struct ust_app *app; | |
9189 | struct ust_app_notify_sock_obj *obj; | |
9190 | ||
9191 | assert(sock >= 0); | |
9192 | ||
9193 | rcu_read_lock(); | |
9194 | ||
9195 | obj = zmalloc(sizeof(*obj)); | |
9196 | if (!obj) { | |
9197 | /* | |
9198 | * An ENOMEM is kind of uncool. If this strikes we continue the | |
9199 | * procedure but the call_rcu will not be called. In this case, we | |
9200 | * accept the fd leak rather than possibly creating an unsynchronized | |
9201 | * state between threads. | |
9202 | * | |
9203 | * TODO: The notify object should be created once the notify socket is | |
9204 | * registered and stored independantely from the ust app object. The | |
9205 | * tricky part is to synchronize the teardown of the application and | |
9206 | * this notify object. Let's keep that in mind so we can avoid this | |
9207 | * kind of shenanigans with ENOMEM in the teardown path. | |
9208 | */ | |
9209 | err_enomem = 1; | |
9210 | } else { | |
9211 | obj->fd = sock; | |
9212 | } | |
9213 | ||
9214 | DBG("UST app notify socket unregister %d", sock); | |
9215 | ||
9216 | /* | |
9217 | * Lookup application by notify socket. If this fails, this means that the | |
9218 | * hash table delete has already been done by the application | |
9219 | * unregistration process so we can safely close the notify socket in a | |
9220 | * call RCU. | |
9221 | */ | |
9222 | app = find_app_by_notify_sock(sock); | |
9223 | if (!app) { | |
9224 | goto close_socket; | |
9225 | } | |
9226 | ||
9227 | iter.iter.node = &app->notify_sock_n.node; | |
9228 | ||
9229 | /* | |
9230 | * Whatever happens here either we fail or succeed, in both cases we have | |
9231 | * to close the socket after a grace period to continue to the call RCU | |
9232 | * here. If the deletion is successful, the application is not visible | |
9233 | * anymore by other threads and is it fails it means that it was already | |
9234 | * deleted from the hash table so either way we just have to close the | |
9235 | * socket. | |
9236 | */ | |
9237 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
9238 | ||
9239 | close_socket: | |
9240 | rcu_read_unlock(); | |
9241 | ||
9242 | /* | |
9243 | * Close socket after a grace period to avoid for the socket to be reused | |
9244 | * before the application object is freed creating potential race between | |
9245 | * threads trying to add unique in the global hash table. | |
9246 | */ | |
9247 | if (!err_enomem) { | |
9248 | call_rcu(&obj->head, close_notify_sock_rcu); | |
9249 | } | |
9250 | } | |
f45e313d DG |
9251 | |
9252 | /* | |
9253 | * Destroy a ust app data structure and free its memory. | |
9254 | */ | |
9255 | void ust_app_destroy(struct ust_app *app) | |
9256 | { | |
9257 | if (!app) { | |
9258 | return; | |
9259 | } | |
9260 | ||
9261 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
9262 | } | |
6dc3064a DG |
9263 | |
9264 | /* | |
9265 | * Take a snapshot for a given UST session. The snapshot is sent to the given | |
9266 | * output. | |
9267 | * | |
9a654598 | 9268 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 9269 | */ |
fb9a95c4 JG |
9270 | enum lttng_error_code ust_app_snapshot_record( |
9271 | const struct ltt_ust_session *usess, | |
348a81dc | 9272 | const struct consumer_output *output, int wait, |
d07ceecd | 9273 | uint64_t nb_packets_per_stream) |
6dc3064a DG |
9274 | { |
9275 | int ret = 0; | |
9a654598 | 9276 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
9277 | struct lttng_ht_iter iter; |
9278 | struct ust_app *app; | |
affce97e | 9279 | char *trace_path = NULL; |
6dc3064a DG |
9280 | |
9281 | assert(usess); | |
9282 | assert(output); | |
9283 | ||
9284 | rcu_read_lock(); | |
9285 | ||
8c924c7b MD |
9286 | switch (usess->buffer_type) { |
9287 | case LTTNG_BUFFER_PER_UID: | |
9288 | { | |
9289 | struct buffer_reg_uid *reg; | |
6dc3064a | 9290 | |
8c924c7b | 9291 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
ebdb334b | 9292 | struct buffer_reg_channel *buf_reg_chan; |
8c924c7b | 9293 | struct consumer_socket *socket; |
3b967712 | 9294 | char pathname[PATH_MAX]; |
5da88b0f | 9295 | size_t consumer_path_offset = 0; |
6dc3064a | 9296 | |
2b269489 JR |
9297 | if (!reg->registry->reg.ust->metadata_key) { |
9298 | /* Skip since no metadata is present */ | |
9299 | continue; | |
9300 | } | |
9301 | ||
8c924c7b MD |
9302 | /* Get consumer socket to use to push the metadata.*/ |
9303 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
9304 | usess->consumer); | |
9305 | if (!socket) { | |
9a654598 | 9306 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
9307 | goto error; |
9308 | } | |
6dc3064a | 9309 | |
8c924c7b MD |
9310 | memset(pathname, 0, sizeof(pathname)); |
9311 | ret = snprintf(pathname, sizeof(pathname), | |
5da88b0f | 9312 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, |
8c924c7b MD |
9313 | reg->uid, reg->bits_per_long); |
9314 | if (ret < 0) { | |
9315 | PERROR("snprintf snapshot path"); | |
9a654598 | 9316 | status = LTTNG_ERR_INVALID; |
8c924c7b MD |
9317 | goto error; |
9318 | } | |
affce97e JG |
9319 | /* Free path allowed on previous iteration. */ |
9320 | free(trace_path); | |
5da88b0f MD |
9321 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
9322 | &consumer_path_offset); | |
3b967712 MD |
9323 | if (!trace_path) { |
9324 | status = LTTNG_ERR_INVALID; | |
9325 | goto error; | |
9326 | } | |
f3db82be | 9327 | /* Add the UST default trace dir to path. */ |
8c924c7b | 9328 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
ebdb334b | 9329 | buf_reg_chan, node.node) { |
9a654598 | 9330 | status = consumer_snapshot_channel(socket, |
ebdb334b | 9331 | buf_reg_chan->consumer_key, |
e098433c | 9332 | output, 0, usess->uid, |
5da88b0f | 9333 | usess->gid, &trace_path[consumer_path_offset], wait, |
d2956687 | 9334 | nb_packets_per_stream); |
9a654598 | 9335 | if (status != LTTNG_OK) { |
8c924c7b MD |
9336 | goto error; |
9337 | } | |
9338 | } | |
9a654598 | 9339 | status = consumer_snapshot_channel(socket, |
68808f4e | 9340 | reg->registry->reg.ust->metadata_key, output, 1, |
5da88b0f MD |
9341 | usess->uid, usess->gid, &trace_path[consumer_path_offset], |
9342 | wait, 0); | |
9a654598 | 9343 | if (status != LTTNG_OK) { |
8c924c7b MD |
9344 | goto error; |
9345 | } | |
af706bb7 | 9346 | } |
8c924c7b MD |
9347 | break; |
9348 | } | |
9349 | case LTTNG_BUFFER_PER_PID: | |
9350 | { | |
9351 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9352 | struct consumer_socket *socket; | |
9353 | struct lttng_ht_iter chan_iter; | |
9354 | struct ust_app_channel *ua_chan; | |
9355 | struct ust_app_session *ua_sess; | |
9356 | struct ust_registry_session *registry; | |
3b967712 | 9357 | char pathname[PATH_MAX]; |
5da88b0f | 9358 | size_t consumer_path_offset = 0; |
8c924c7b MD |
9359 | |
9360 | ua_sess = lookup_session_by_app(usess, app); | |
9361 | if (!ua_sess) { | |
9362 | /* Session not associated with this app. */ | |
9363 | continue; | |
9364 | } | |
af706bb7 | 9365 | |
8c924c7b MD |
9366 | /* Get the right consumer socket for the application. */ |
9367 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
348a81dc | 9368 | output); |
8c924c7b | 9369 | if (!socket) { |
9a654598 | 9370 | status = LTTNG_ERR_INVALID; |
5c786ded JD |
9371 | goto error; |
9372 | } | |
9373 | ||
8c924c7b MD |
9374 | /* Add the UST default trace dir to path. */ |
9375 | memset(pathname, 0, sizeof(pathname)); | |
5da88b0f | 9376 | ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", |
8c924c7b | 9377 | ua_sess->path); |
6dc3064a | 9378 | if (ret < 0) { |
9a654598 | 9379 | status = LTTNG_ERR_INVALID; |
8c924c7b | 9380 | PERROR("snprintf snapshot path"); |
6dc3064a DG |
9381 | goto error; |
9382 | } | |
affce97e JG |
9383 | /* Free path allowed on previous iteration. */ |
9384 | free(trace_path); | |
5da88b0f MD |
9385 | trace_path = setup_channel_trace_path(usess->consumer, pathname, |
9386 | &consumer_path_offset); | |
3b967712 MD |
9387 | if (!trace_path) { |
9388 | status = LTTNG_ERR_INVALID; | |
9389 | goto error; | |
9390 | } | |
f3db82be | 9391 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
8c924c7b | 9392 | ua_chan, node.node) { |
9a654598 | 9393 | status = consumer_snapshot_channel(socket, |
470cc211 | 9394 | ua_chan->key, output, 0, |
ff588497 JR |
9395 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
9396 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
5da88b0f | 9397 | &trace_path[consumer_path_offset], wait, |
d2956687 | 9398 | nb_packets_per_stream); |
9a654598 JG |
9399 | switch (status) { |
9400 | case LTTNG_OK: | |
9401 | break; | |
9402 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
9403 | continue; | |
9404 | default: | |
8c924c7b MD |
9405 | goto error; |
9406 | } | |
9407 | } | |
9408 | ||
9409 | registry = get_session_registry(ua_sess); | |
fad1ed2f | 9410 | if (!registry) { |
9bbfb88c MD |
9411 | DBG("Application session is being torn down. Skip application."); |
9412 | continue; | |
fad1ed2f | 9413 | } |
9a654598 | 9414 | status = consumer_snapshot_channel(socket, |
470cc211 | 9415 | registry->metadata_key, output, 1, |
ff588497 JR |
9416 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
9417 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
5da88b0f | 9418 | &trace_path[consumer_path_offset], wait, 0); |
9a654598 JG |
9419 | switch (status) { |
9420 | case LTTNG_OK: | |
9421 | break; | |
9422 | case LTTNG_ERR_CHAN_NOT_FOUND: | |
9423 | continue; | |
9424 | default: | |
8c924c7b MD |
9425 | goto error; |
9426 | } | |
9427 | } | |
9428 | break; | |
9429 | } | |
9430 | default: | |
9431 | assert(0); | |
9432 | break; | |
6dc3064a DG |
9433 | } |
9434 | ||
9435 | error: | |
affce97e | 9436 | free(trace_path); |
6dc3064a | 9437 | rcu_read_unlock(); |
9a654598 | 9438 | return status; |
6dc3064a | 9439 | } |
5c786ded JD |
9440 | |
9441 | /* | |
d07ceecd | 9442 | * Return the size taken by one more packet per stream. |
5c786ded | 9443 | */ |
fb9a95c4 JG |
9444 | uint64_t ust_app_get_size_one_more_packet_per_stream( |
9445 | const struct ltt_ust_session *usess, uint64_t cur_nr_packets) | |
5c786ded | 9446 | { |
d07ceecd | 9447 | uint64_t tot_size = 0; |
5c786ded JD |
9448 | struct ust_app *app; |
9449 | struct lttng_ht_iter iter; | |
9450 | ||
9451 | assert(usess); | |
9452 | ||
9453 | switch (usess->buffer_type) { | |
9454 | case LTTNG_BUFFER_PER_UID: | |
9455 | { | |
9456 | struct buffer_reg_uid *reg; | |
9457 | ||
9458 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
ebdb334b | 9459 | struct buffer_reg_channel *buf_reg_chan; |
5c786ded | 9460 | |
b7064eaa | 9461 | rcu_read_lock(); |
5c786ded | 9462 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
ebdb334b JR |
9463 | buf_reg_chan, node.node) { |
9464 | if (cur_nr_packets >= buf_reg_chan->num_subbuf) { | |
d07ceecd MD |
9465 | /* |
9466 | * Don't take channel into account if we | |
9467 | * already grab all its packets. | |
9468 | */ | |
9469 | continue; | |
9470 | } | |
ebdb334b | 9471 | tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count; |
5c786ded | 9472 | } |
b7064eaa | 9473 | rcu_read_unlock(); |
5c786ded JD |
9474 | } |
9475 | break; | |
9476 | } | |
9477 | case LTTNG_BUFFER_PER_PID: | |
9478 | { | |
9479 | rcu_read_lock(); | |
9480 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9481 | struct ust_app_channel *ua_chan; | |
9482 | struct ust_app_session *ua_sess; | |
9483 | struct lttng_ht_iter chan_iter; | |
9484 | ||
9485 | ua_sess = lookup_session_by_app(usess, app); | |
9486 | if (!ua_sess) { | |
9487 | /* Session not associated with this app. */ | |
9488 | continue; | |
9489 | } | |
9490 | ||
9491 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
9492 | ua_chan, node.node) { | |
d07ceecd MD |
9493 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
9494 | /* | |
9495 | * Don't take channel into account if we | |
9496 | * already grab all its packets. | |
9497 | */ | |
9498 | continue; | |
9499 | } | |
9500 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; | |
5c786ded JD |
9501 | } |
9502 | } | |
9503 | rcu_read_unlock(); | |
9504 | break; | |
9505 | } | |
9506 | default: | |
9507 | assert(0); | |
9508 | break; | |
9509 | } | |
9510 | ||
d07ceecd | 9511 | return tot_size; |
5c786ded | 9512 | } |
fb83fe64 JD |
9513 | |
9514 | int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id, | |
9515 | struct cds_list_head *buffer_reg_uid_list, | |
9516 | struct consumer_output *consumer, uint64_t uchan_id, | |
9517 | int overwrite, uint64_t *discarded, uint64_t *lost) | |
9518 | { | |
9519 | int ret; | |
9520 | uint64_t consumer_chan_key; | |
9521 | ||
70dd8162 MD |
9522 | *discarded = 0; |
9523 | *lost = 0; | |
9524 | ||
fb83fe64 | 9525 | ret = buffer_reg_uid_consumer_channel_key( |
76604852 | 9526 | buffer_reg_uid_list, uchan_id, &consumer_chan_key); |
fb83fe64 | 9527 | if (ret < 0) { |
70dd8162 MD |
9528 | /* Not found */ |
9529 | ret = 0; | |
fb83fe64 JD |
9530 | goto end; |
9531 | } | |
9532 | ||
9533 | if (overwrite) { | |
9534 | ret = consumer_get_lost_packets(ust_session_id, | |
9535 | consumer_chan_key, consumer, lost); | |
9536 | } else { | |
9537 | ret = consumer_get_discarded_events(ust_session_id, | |
9538 | consumer_chan_key, consumer, discarded); | |
9539 | } | |
9540 | ||
9541 | end: | |
9542 | return ret; | |
9543 | } | |
9544 | ||
9545 | int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess, | |
9546 | struct ltt_ust_channel *uchan, | |
9547 | struct consumer_output *consumer, int overwrite, | |
9548 | uint64_t *discarded, uint64_t *lost) | |
9549 | { | |
9550 | int ret = 0; | |
9551 | struct lttng_ht_iter iter; | |
9552 | struct lttng_ht_node_str *ua_chan_node; | |
9553 | struct ust_app *app; | |
9554 | struct ust_app_session *ua_sess; | |
9555 | struct ust_app_channel *ua_chan; | |
9556 | ||
70dd8162 MD |
9557 | *discarded = 0; |
9558 | *lost = 0; | |
9559 | ||
fb83fe64 JD |
9560 | rcu_read_lock(); |
9561 | /* | |
70dd8162 MD |
9562 | * Iterate over every registered applications. Sum counters for |
9563 | * all applications containing requested session and channel. | |
fb83fe64 JD |
9564 | */ |
9565 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9566 | struct lttng_ht_iter uiter; | |
9567 | ||
9568 | ua_sess = lookup_session_by_app(usess, app); | |
9569 | if (ua_sess == NULL) { | |
9570 | continue; | |
9571 | } | |
9572 | ||
9573 | /* Get channel */ | |
ee022399 | 9574 | lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter); |
fb83fe64 JD |
9575 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); |
9576 | /* If the session is found for the app, the channel must be there */ | |
9577 | assert(ua_chan_node); | |
9578 | ||
9579 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
9580 | ||
9581 | if (overwrite) { | |
70dd8162 MD |
9582 | uint64_t _lost; |
9583 | ||
fb83fe64 | 9584 | ret = consumer_get_lost_packets(usess->id, ua_chan->key, |
70dd8162 MD |
9585 | consumer, &_lost); |
9586 | if (ret < 0) { | |
9587 | break; | |
9588 | } | |
9589 | (*lost) += _lost; | |
fb83fe64 | 9590 | } else { |
70dd8162 MD |
9591 | uint64_t _discarded; |
9592 | ||
fb83fe64 | 9593 | ret = consumer_get_discarded_events(usess->id, |
70dd8162 MD |
9594 | ua_chan->key, consumer, &_discarded); |
9595 | if (ret < 0) { | |
9596 | break; | |
9597 | } | |
9598 | (*discarded) += _discarded; | |
fb83fe64 | 9599 | } |
fb83fe64 JD |
9600 | } |
9601 | ||
fb83fe64 JD |
9602 | rcu_read_unlock(); |
9603 | return ret; | |
9604 | } | |
c2561365 JD |
9605 | |
9606 | static | |
9607 | int ust_app_regenerate_statedump(struct ltt_ust_session *usess, | |
9608 | struct ust_app *app) | |
9609 | { | |
9610 | int ret = 0; | |
9611 | struct ust_app_session *ua_sess; | |
9612 | ||
9613 | DBG("Regenerating the metadata for ust app pid %d", app->pid); | |
9614 | ||
9615 | rcu_read_lock(); | |
9616 | ||
9617 | ua_sess = lookup_session_by_app(usess, app); | |
9618 | if (ua_sess == NULL) { | |
9619 | /* The session is in teardown process. Ignore and continue. */ | |
9620 | goto end; | |
9621 | } | |
9622 | ||
9623 | pthread_mutex_lock(&ua_sess->lock); | |
9624 | ||
9625 | if (ua_sess->deleted) { | |
9626 | goto end_unlock; | |
9627 | } | |
9628 | ||
9629 | pthread_mutex_lock(&app->sock_lock); | |
9630 | ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle); | |
9631 | pthread_mutex_unlock(&app->sock_lock); | |
9632 | ||
9633 | end_unlock: | |
9634 | pthread_mutex_unlock(&ua_sess->lock); | |
9635 | ||
9636 | end: | |
9637 | rcu_read_unlock(); | |
9638 | health_code_update(); | |
9639 | return ret; | |
9640 | } | |
9641 | ||
9642 | /* | |
9643 | * Regenerate the statedump for each app in the session. | |
9644 | */ | |
9645 | int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess) | |
9646 | { | |
9647 | int ret = 0; | |
9648 | struct lttng_ht_iter iter; | |
9649 | struct ust_app *app; | |
9650 | ||
9651 | DBG("Regenerating the metadata for all UST apps"); | |
9652 | ||
9653 | rcu_read_lock(); | |
9654 | ||
9655 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9656 | if (!app->compatible) { | |
9657 | continue; | |
9658 | } | |
9659 | ||
9660 | ret = ust_app_regenerate_statedump(usess, app); | |
9661 | if (ret < 0) { | |
9662 | /* Continue to the next app even on error */ | |
9663 | continue; | |
9664 | } | |
9665 | } | |
9666 | ||
9667 | rcu_read_unlock(); | |
9668 | ||
9669 | return 0; | |
9670 | } | |
5c408ad8 JD |
9671 | |
9672 | /* | |
9673 | * Rotate all the channels of a session. | |
9674 | * | |
6f6d3b69 | 9675 | * Return LTTNG_OK on success or else an LTTng error code. |
5c408ad8 | 9676 | */ |
6f6d3b69 | 9677 | enum lttng_error_code ust_app_rotate_session(struct ltt_session *session) |
5c408ad8 | 9678 | { |
6f6d3b69 MD |
9679 | int ret; |
9680 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
5c408ad8 JD |
9681 | struct lttng_ht_iter iter; |
9682 | struct ust_app *app; | |
9683 | struct ltt_ust_session *usess = session->ust_session; | |
5c408ad8 JD |
9684 | |
9685 | assert(usess); | |
9686 | ||
9687 | rcu_read_lock(); | |
9688 | ||
9689 | switch (usess->buffer_type) { | |
9690 | case LTTNG_BUFFER_PER_UID: | |
9691 | { | |
9692 | struct buffer_reg_uid *reg; | |
9693 | ||
9694 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
ebdb334b | 9695 | struct buffer_reg_channel *buf_reg_chan; |
5c408ad8 JD |
9696 | struct consumer_socket *socket; |
9697 | ||
14d3fca9 JR |
9698 | if (!reg->registry->reg.ust->metadata_key) { |
9699 | /* Skip since no metadata is present */ | |
9700 | continue; | |
9701 | } | |
9702 | ||
5c408ad8 JD |
9703 | /* Get consumer socket to use to push the metadata.*/ |
9704 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
9705 | usess->consumer); | |
9706 | if (!socket) { | |
6f6d3b69 | 9707 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
9708 | goto error; |
9709 | } | |
9710 | ||
5c408ad8 JD |
9711 | /* Rotate the data channels. */ |
9712 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
ebdb334b | 9713 | buf_reg_chan, node.node) { |
5c408ad8 | 9714 | ret = consumer_rotate_channel(socket, |
ebdb334b | 9715 | buf_reg_chan->consumer_key, |
5c408ad8 | 9716 | usess->uid, usess->gid, |
d2956687 JG |
9717 | usess->consumer, |
9718 | /* is_metadata_channel */ false); | |
5c408ad8 | 9719 | if (ret < 0) { |
6f6d3b69 | 9720 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
9721 | goto error; |
9722 | } | |
9723 | } | |
9724 | ||
9725 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); | |
9726 | ||
9727 | ret = consumer_rotate_channel(socket, | |
9728 | reg->registry->reg.ust->metadata_key, | |
9729 | usess->uid, usess->gid, | |
d2956687 JG |
9730 | usess->consumer, |
9731 | /* is_metadata_channel */ true); | |
5c408ad8 | 9732 | if (ret < 0) { |
6f6d3b69 | 9733 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; |
5c408ad8 JD |
9734 | goto error; |
9735 | } | |
5c408ad8 JD |
9736 | } |
9737 | break; | |
9738 | } | |
9739 | case LTTNG_BUFFER_PER_PID: | |
9740 | { | |
9741 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9742 | struct consumer_socket *socket; | |
9743 | struct lttng_ht_iter chan_iter; | |
9744 | struct ust_app_channel *ua_chan; | |
9745 | struct ust_app_session *ua_sess; | |
9746 | struct ust_registry_session *registry; | |
9747 | ||
9748 | ua_sess = lookup_session_by_app(usess, app); | |
9749 | if (!ua_sess) { | |
9750 | /* Session not associated with this app. */ | |
9751 | continue; | |
9752 | } | |
5c408ad8 JD |
9753 | |
9754 | /* Get the right consumer socket for the application. */ | |
9755 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
9756 | usess->consumer); | |
9757 | if (!socket) { | |
6f6d3b69 | 9758 | cmd_ret = LTTNG_ERR_INVALID; |
5c408ad8 JD |
9759 | goto error; |
9760 | } | |
9761 | ||
9762 | registry = get_session_registry(ua_sess); | |
9763 | if (!registry) { | |
6f6d3b69 MD |
9764 | DBG("Application session is being torn down. Skip application."); |
9765 | continue; | |
5c408ad8 JD |
9766 | } |
9767 | ||
5c408ad8 JD |
9768 | /* Rotate the data channels. */ |
9769 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
9770 | ua_chan, node.node) { | |
470cc211 JG |
9771 | ret = consumer_rotate_channel(socket, |
9772 | ua_chan->key, | |
ff588497 JR |
9773 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
9774 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
d2956687 JG |
9775 | ua_sess->consumer, |
9776 | /* is_metadata_channel */ false); | |
5c408ad8 | 9777 | if (ret < 0) { |
6f6d3b69 MD |
9778 | /* Per-PID buffer and application going away. */ |
9779 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
9780 | continue; | |
9781 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
9782 | goto error; |
9783 | } | |
9784 | } | |
9785 | ||
9786 | /* Rotate the metadata channel. */ | |
9787 | (void) push_metadata(registry, usess->consumer); | |
470cc211 JG |
9788 | ret = consumer_rotate_channel(socket, |
9789 | registry->metadata_key, | |
ff588497 JR |
9790 | lttng_credentials_get_uid(&ua_sess->effective_credentials), |
9791 | lttng_credentials_get_gid(&ua_sess->effective_credentials), | |
d2956687 JG |
9792 | ua_sess->consumer, |
9793 | /* is_metadata_channel */ true); | |
5c408ad8 | 9794 | if (ret < 0) { |
6f6d3b69 MD |
9795 | /* Per-PID buffer and application going away. */ |
9796 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) | |
9797 | continue; | |
9798 | cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER; | |
5c408ad8 JD |
9799 | goto error; |
9800 | } | |
5c408ad8 JD |
9801 | } |
9802 | break; | |
9803 | } | |
9804 | default: | |
9805 | assert(0); | |
9806 | break; | |
9807 | } | |
9808 | ||
6f6d3b69 | 9809 | cmd_ret = LTTNG_OK; |
5c408ad8 JD |
9810 | |
9811 | error: | |
9812 | rcu_read_unlock(); | |
6f6d3b69 | 9813 | return cmd_ret; |
5c408ad8 | 9814 | } |
d2956687 JG |
9815 | |
9816 | enum lttng_error_code ust_app_create_channel_subdirectories( | |
9817 | const struct ltt_ust_session *usess) | |
9818 | { | |
9819 | enum lttng_error_code ret = LTTNG_OK; | |
9820 | struct lttng_ht_iter iter; | |
9821 | enum lttng_trace_chunk_status chunk_status; | |
9822 | char *pathname_index; | |
9823 | int fmt_ret; | |
9824 | ||
9825 | assert(usess->current_trace_chunk); | |
9826 | rcu_read_lock(); | |
9827 | ||
9828 | switch (usess->buffer_type) { | |
9829 | case LTTNG_BUFFER_PER_UID: | |
9830 | { | |
9831 | struct buffer_reg_uid *reg; | |
9832 | ||
9833 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
9834 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 9835 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR, |
d2956687 JG |
9836 | reg->uid, reg->bits_per_long); |
9837 | if (fmt_ret < 0) { | |
9838 | ERR("Failed to format channel index directory"); | |
9839 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
9840 | goto error; | |
9841 | } | |
9842 | ||
9843 | /* | |
9844 | * Create the index subdirectory which will take care | |
9845 | * of implicitly creating the channel's path. | |
9846 | */ | |
9847 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
9848 | usess->current_trace_chunk, | |
9849 | pathname_index); | |
9850 | free(pathname_index); | |
9851 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
9852 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
9853 | goto error; | |
9854 | } | |
9855 | } | |
9856 | break; | |
9857 | } | |
9858 | case LTTNG_BUFFER_PER_PID: | |
9859 | { | |
9860 | struct ust_app *app; | |
9861 | ||
495dece5 MD |
9862 | /* |
9863 | * Create the toplevel ust/ directory in case no apps are running. | |
9864 | */ | |
9865 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
9866 | usess->current_trace_chunk, | |
9867 | DEFAULT_UST_TRACE_DIR); | |
9868 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
9869 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
9870 | goto error; | |
9871 | } | |
9872 | ||
d2956687 JG |
9873 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, |
9874 | pid_n.node) { | |
9875 | struct ust_app_session *ua_sess; | |
9876 | struct ust_registry_session *registry; | |
9877 | ||
9878 | ua_sess = lookup_session_by_app(usess, app); | |
9879 | if (!ua_sess) { | |
9880 | /* Session not associated with this app. */ | |
9881 | continue; | |
9882 | } | |
9883 | ||
9884 | registry = get_session_registry(ua_sess); | |
9885 | if (!registry) { | |
9886 | DBG("Application session is being torn down. Skip application."); | |
9887 | continue; | |
9888 | } | |
9889 | ||
9890 | fmt_ret = asprintf(&pathname_index, | |
5da88b0f | 9891 | DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR, |
d2956687 JG |
9892 | ua_sess->path); |
9893 | if (fmt_ret < 0) { | |
9894 | ERR("Failed to format channel index directory"); | |
9895 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
9896 | goto error; | |
9897 | } | |
9898 | /* | |
9899 | * Create the index subdirectory which will take care | |
9900 | * of implicitly creating the channel's path. | |
9901 | */ | |
9902 | chunk_status = lttng_trace_chunk_create_subdirectory( | |
9903 | usess->current_trace_chunk, | |
9904 | pathname_index); | |
9905 | free(pathname_index); | |
9906 | if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) { | |
9907 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
9908 | goto error; | |
9909 | } | |
9910 | } | |
9911 | break; | |
9912 | } | |
9913 | default: | |
9914 | abort(); | |
9915 | } | |
9916 | ||
9917 | ret = LTTNG_OK; | |
9918 | error: | |
9919 | rcu_read_unlock(); | |
9920 | return ret; | |
9921 | } | |
4a9b9759 MD |
9922 | |
9923 | /* | |
9924 | * Clear all the channels of a session. | |
9925 | * | |
9926 | * Return LTTNG_OK on success or else an LTTng error code. | |
9927 | */ | |
ebdb334b JR |
9928 | static |
9929 | enum lttng_error_code ust_app_clear_session_channels(struct ltt_session *session) | |
4a9b9759 MD |
9930 | { |
9931 | int ret; | |
9932 | enum lttng_error_code cmd_ret = LTTNG_OK; | |
9933 | struct lttng_ht_iter iter; | |
9934 | struct ust_app *app; | |
9935 | struct ltt_ust_session *usess = session->ust_session; | |
9936 | ||
9937 | assert(usess); | |
9938 | ||
9939 | rcu_read_lock(); | |
9940 | ||
9941 | if (usess->active) { | |
9942 | ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id); | |
9943 | cmd_ret = LTTNG_ERR_FATAL; | |
9944 | goto end; | |
9945 | } | |
9946 | ||
9947 | switch (usess->buffer_type) { | |
9948 | case LTTNG_BUFFER_PER_UID: | |
9949 | { | |
9950 | struct buffer_reg_uid *reg; | |
9951 | ||
9952 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
ebdb334b | 9953 | struct buffer_reg_channel *buf_reg_chan; |
4a9b9759 MD |
9954 | struct consumer_socket *socket; |
9955 | ||
9956 | /* Get consumer socket to use to push the metadata.*/ | |
9957 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
9958 | usess->consumer); | |
9959 | if (!socket) { | |
9960 | cmd_ret = LTTNG_ERR_INVALID; | |
9961 | goto error_socket; | |
9962 | } | |
9963 | ||
9964 | /* Clear the data channels. */ | |
9965 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
ebdb334b | 9966 | buf_reg_chan, node.node) { |
4a9b9759 | 9967 | ret = consumer_clear_channel(socket, |
ebdb334b | 9968 | buf_reg_chan->consumer_key); |
4a9b9759 MD |
9969 | if (ret < 0) { |
9970 | goto error; | |
9971 | } | |
9972 | } | |
9973 | ||
9974 | (void) push_metadata(reg->registry->reg.ust, usess->consumer); | |
9975 | ||
9976 | /* | |
9977 | * Clear the metadata channel. | |
9978 | * Metadata channel is not cleared per se but we still need to | |
9979 | * perform a rotation operation on it behind the scene. | |
9980 | */ | |
9981 | ret = consumer_clear_channel(socket, | |
9982 | reg->registry->reg.ust->metadata_key); | |
9983 | if (ret < 0) { | |
9984 | goto error; | |
9985 | } | |
9986 | } | |
9987 | break; | |
9988 | } | |
9989 | case LTTNG_BUFFER_PER_PID: | |
9990 | { | |
9991 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
9992 | struct consumer_socket *socket; | |
9993 | struct lttng_ht_iter chan_iter; | |
9994 | struct ust_app_channel *ua_chan; | |
9995 | struct ust_app_session *ua_sess; | |
9996 | struct ust_registry_session *registry; | |
9997 | ||
9998 | ua_sess = lookup_session_by_app(usess, app); | |
9999 | if (!ua_sess) { | |
10000 | /* Session not associated with this app. */ | |
10001 | continue; | |
10002 | } | |
10003 | ||
10004 | /* Get the right consumer socket for the application. */ | |
10005 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
10006 | usess->consumer); | |
10007 | if (!socket) { | |
10008 | cmd_ret = LTTNG_ERR_INVALID; | |
10009 | goto error_socket; | |
10010 | } | |
10011 | ||
10012 | registry = get_session_registry(ua_sess); | |
10013 | if (!registry) { | |
10014 | DBG("Application session is being torn down. Skip application."); | |
10015 | continue; | |
10016 | } | |
10017 | ||
10018 | /* Clear the data channels. */ | |
10019 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
10020 | ua_chan, node.node) { | |
10021 | ret = consumer_clear_channel(socket, ua_chan->key); | |
10022 | if (ret < 0) { | |
10023 | /* Per-PID buffer and application going away. */ | |
10024 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
10025 | continue; | |
10026 | } | |
10027 | goto error; | |
10028 | } | |
10029 | } | |
10030 | ||
10031 | (void) push_metadata(registry, usess->consumer); | |
10032 | ||
10033 | /* | |
10034 | * Clear the metadata channel. | |
10035 | * Metadata channel is not cleared per se but we still need to | |
10036 | * perform rotation operation on it behind the scene. | |
10037 | */ | |
10038 | ret = consumer_clear_channel(socket, registry->metadata_key); | |
10039 | if (ret < 0) { | |
10040 | /* Per-PID buffer and application going away. */ | |
10041 | if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) { | |
10042 | continue; | |
10043 | } | |
10044 | goto error; | |
10045 | } | |
10046 | } | |
10047 | break; | |
10048 | } | |
10049 | default: | |
10050 | assert(0); | |
10051 | break; | |
10052 | } | |
10053 | ||
10054 | cmd_ret = LTTNG_OK; | |
10055 | goto end; | |
10056 | ||
10057 | error: | |
10058 | switch (-ret) { | |
10059 | case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED: | |
10060 | cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED; | |
10061 | break; | |
10062 | default: | |
10063 | cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER; | |
10064 | } | |
10065 | ||
10066 | error_socket: | |
10067 | end: | |
10068 | rcu_read_unlock(); | |
10069 | return cmd_ret; | |
10070 | } | |
04ed9e10 | 10071 | |
ebdb334b JR |
10072 | static |
10073 | enum lttng_error_code ust_app_clear_session_maps_per_uid( | |
10074 | struct ltt_ust_session *usess, struct ltt_ust_map *umap, | |
10075 | uint32_t app_bitness) | |
10076 | { | |
10077 | struct lttng_ht_iter iter; | |
10078 | struct buffer_reg_uid *buf_reg_uid; | |
10079 | struct buffer_reg_map *buf_reg_map; | |
10080 | struct ust_registry_session *ust_reg_sess; | |
10081 | struct lttng_ht_node_u64 *ust_reg_map_node; | |
10082 | struct ust_registry_map *ust_reg_map; | |
10083 | struct ust_registry_map_index_ht_entry *map_index_entry; | |
10084 | enum lttng_error_code status; | |
10085 | ||
10086 | buf_reg_uid = buffer_reg_uid_find(usess->id, app_bitness, usess->uid); | |
10087 | if (!buf_reg_uid) { | |
10088 | /* | |
10089 | * Buffer registry entry for uid not found. Probably no app for | |
10090 | * this UID at the moment. | |
10091 | */ | |
10092 | DBG("No buffer registry entry found for uid: ust-sess-id = %"PRIu64", bitness = %"PRIu32", uid = %d", | |
10093 | usess->id, app_bitness, usess->uid); | |
10094 | /* | |
10095 | * Not an error. Leave the key value pair unchanged and return. | |
10096 | */ | |
10097 | status = LTTNG_OK; | |
10098 | goto end; | |
10099 | } | |
10100 | ||
10101 | buf_reg_map = buffer_reg_map_find(umap->id, buf_reg_uid); | |
10102 | if (!buf_reg_uid) { | |
10103 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, | |
10104 | umap->id); | |
10105 | status = LTTNG_ERR_UNK; | |
10106 | goto end; | |
10107 | } | |
10108 | ||
10109 | ust_reg_sess = buf_reg_uid->registry->reg.ust; | |
10110 | ||
10111 | /* Get the ust_reg map object from the registry */ | |
10112 | // FIXME: frdeso: This can be changed to ust_registry_map_find() right? | |
10113 | ||
10114 | lttng_ht_lookup(ust_reg_sess->maps, (void *) &umap->id, &iter); | |
10115 | ust_reg_map_node = lttng_ht_iter_get_node_u64(&iter); | |
10116 | if (!ust_reg_map_node) { | |
10117 | ERR("Error getting per-uid map buffer registry entry: map-id = %"PRIu64, | |
10118 | umap->id); | |
10119 | status = LTTNG_ERR_UNK; | |
10120 | goto end; | |
10121 | } | |
10122 | ust_reg_map = caa_container_of(ust_reg_map_node, | |
10123 | struct ust_registry_map, node); | |
10124 | ||
10125 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, | |
10126 | &iter.iter, map_index_entry, node.node) { | |
10127 | int ret; | |
10128 | size_t dimension_indexes[1] = {map_index_entry->index}; | |
10129 | ||
10130 | ret = ustctl_counter_clear(buf_reg_map->daemon_counter, dimension_indexes); | |
10131 | if (ret) { | |
10132 | ERR("clearing counter index %"PRIu64, map_index_entry->index); | |
10133 | //fixme: frdeso: convert ust errors to tools errors | |
10134 | status = LTTNG_ERR_UNK; | |
10135 | goto end; | |
10136 | } | |
10137 | } | |
10138 | ||
10139 | status = LTTNG_OK; | |
10140 | ||
10141 | end: | |
10142 | return status; | |
10143 | } | |
10144 | ||
10145 | static | |
10146 | enum lttng_error_code ust_app_clear_session_maps_per_pid( | |
10147 | struct ltt_ust_session *usess, struct ltt_ust_map *umap, | |
10148 | uint32_t app_bitness) | |
10149 | { | |
10150 | struct lttng_ht_iter app_iter; | |
10151 | enum lttng_error_code status; | |
10152 | struct ust_app *app; | |
10153 | struct map_kv_ht_entry *kv_entry; | |
10154 | struct lttng_ht_iter iter; | |
10155 | ||
10156 | cds_lfht_for_each_entry(ust_app_ht->ht, &app_iter.iter, app, pid_n.node) { | |
10157 | struct lttng_ht_iter map_iter, key_iter; | |
10158 | struct lttng_ht_node_str *ua_map_node; | |
10159 | struct ust_app_map *ua_map; | |
10160 | struct ust_app_session *ua_sess; | |
10161 | struct ust_registry_session *ust_reg_sess; | |
10162 | struct ust_registry_map *ust_reg_map; | |
10163 | struct ust_registry_map_index_ht_entry *map_index_entry; | |
10164 | ||
10165 | if (app->bits_per_long != app_bitness) { | |
10166 | continue; | |
10167 | } | |
10168 | ||
10169 | ua_sess = lookup_session_by_app(usess, app); | |
10170 | if (!ua_sess) { | |
10171 | /* Session not associated with this app. */ | |
10172 | continue; | |
10173 | } | |
10174 | ||
10175 | ust_reg_sess = get_session_registry(ua_sess); | |
10176 | if (!ust_reg_sess) { | |
10177 | DBG("Application session is being torn down. Skip application."); | |
10178 | continue; | |
10179 | } | |
10180 | ||
10181 | /* Lookup map in the ust app session */ | |
10182 | lttng_ht_lookup(ua_sess->maps, (void *)umap->name, &map_iter); | |
10183 | ua_map_node = lttng_ht_iter_get_node_str(&map_iter); | |
10184 | ||
10185 | assert(ua_map_node != NULL); | |
10186 | ua_map = caa_container_of(ua_map_node, struct ust_app_map, node); | |
10187 | ||
10188 | pthread_mutex_lock(&ust_reg_sess->lock); | |
10189 | ust_reg_map = ust_registry_map_find(ust_reg_sess, ua_map->key); | |
10190 | pthread_mutex_unlock(&ust_reg_sess->lock); | |
10191 | assert(ust_reg_map); | |
10192 | ||
10193 | /* Iterate over all the formated_key -> counter index */ | |
10194 | cds_lfht_for_each_entry(ust_reg_map->key_string_to_bucket_index_ht->ht, | |
10195 | &key_iter.iter, map_index_entry, node.node) { | |
10196 | ||
10197 | int ret; | |
10198 | size_t dimension_indexes[1] = {map_index_entry->index}; | |
10199 | ||
10200 | ret = ustctl_counter_clear(ua_map->map_handle, | |
10201 | dimension_indexes); | |
10202 | if (ret) { | |
10203 | ERR("clearing counter index %"PRIu64, map_index_entry->index); | |
10204 | //fixme: frdeso: convert ust errors to tools errors | |
10205 | status = LTTNG_ERR_UNK; | |
10206 | goto end; | |
10207 | } | |
10208 | } | |
10209 | } | |
10210 | ||
10211 | /* | |
10212 | * Emptying the dead app key values. | |
10213 | */ | |
10214 | pthread_mutex_lock(&umap->dead_app_kv_values.lock); | |
10215 | ||
10216 | if (app_bitness == 32) { | |
10217 | cds_lfht_for_each_entry(umap->dead_app_kv_values.dead_app_kv_values_32bits->ht, | |
10218 | &iter.iter, kv_entry, node.node) { | |
10219 | kv_entry->value = 0; | |
10220 | } | |
10221 | } else { | |
10222 | ||
10223 | cds_lfht_for_each_entry(umap->dead_app_kv_values.dead_app_kv_values_64bits->ht, | |
10224 | &iter.iter, kv_entry, node.node) { | |
10225 | kv_entry->value = 0; | |
10226 | } | |
10227 | } | |
10228 | ||
10229 | pthread_mutex_unlock(&umap->dead_app_kv_values.lock); | |
10230 | ||
10231 | status = LTTNG_OK; | |
10232 | end: | |
10233 | return status; | |
10234 | } | |
10235 | ||
10236 | static | |
10237 | enum lttng_error_code ust_app_clear_session_maps(struct ltt_session *session) | |
10238 | { | |
10239 | struct ltt_ust_session *usess = session->ust_session; | |
10240 | enum lttng_error_code status; | |
10241 | struct lttng_ht_iter iter; | |
10242 | struct ltt_ust_map *umap; | |
10243 | ||
10244 | cds_lfht_for_each_entry(usess->domain_global.maps->ht, &iter.iter, | |
10245 | umap, node.node) { | |
10246 | ||
10247 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
10248 | status = ust_app_clear_session_maps_per_uid(session->ust_session, | |
10249 | umap, 32); | |
10250 | assert(status == LTTNG_OK); | |
10251 | ||
10252 | status = ust_app_clear_session_maps_per_uid(session->ust_session, | |
10253 | umap, 64); | |
10254 | assert(status == LTTNG_OK); | |
10255 | //fixme:frdeso:error handling | |
10256 | } else { | |
10257 | status = ust_app_clear_session_maps_per_pid(session->ust_session, | |
10258 | umap, 32); | |
10259 | assert(status == LTTNG_OK); | |
10260 | ||
10261 | status = ust_app_clear_session_maps_per_pid(session->ust_session, | |
10262 | umap, 64); | |
10263 | assert(status == LTTNG_OK); | |
10264 | //fixme:frdeso:error handling | |
10265 | // | |
10266 | // | |
10267 | } | |
10268 | ||
10269 | } | |
10270 | ||
10271 | return LTTNG_OK; | |
10272 | } | |
10273 | ||
10274 | enum lttng_error_code ust_app_clear_session(struct ltt_session *session) | |
10275 | { | |
10276 | enum lttng_error_code cmd_ret; | |
10277 | ||
10278 | ||
10279 | cmd_ret = ust_app_clear_session_channels(session); | |
10280 | if (cmd_ret != LTTNG_OK) { | |
10281 | ERR("Clearing session's channels"); | |
10282 | goto end; | |
10283 | } | |
10284 | ||
10285 | cmd_ret = ust_app_clear_session_maps(session); | |
10286 | if (cmd_ret != LTTNG_OK) { | |
10287 | ERR("Clearing session's maps"); | |
10288 | goto end; | |
10289 | } | |
10290 | end: | |
10291 | return cmd_ret; | |
10292 | } | |
10293 | ||
04ed9e10 JG |
10294 | /* |
10295 | * This function skips the metadata channel as the begin/end timestamps of a | |
10296 | * metadata packet are useless. | |
10297 | * | |
10298 | * Moreover, opening a packet after a "clear" will cause problems for live | |
10299 | * sessions as it will introduce padding that was not part of the first trace | |
10300 | * chunk. The relay daemon expects the content of the metadata stream of | |
10301 | * successive metadata trace chunks to be strict supersets of one another. | |
10302 | * | |
10303 | * For example, flushing a packet at the beginning of the metadata stream of | |
10304 | * a trace chunk resulting from a "clear" session command will cause the | |
10305 | * size of the metadata stream of the new trace chunk to not match the size of | |
10306 | * the metadata stream of the original chunk. This will confuse the relay | |
10307 | * daemon as the same "offset" in a metadata stream will no longer point | |
10308 | * to the same content. | |
10309 | */ | |
10310 | enum lttng_error_code ust_app_open_packets(struct ltt_session *session) | |
10311 | { | |
10312 | enum lttng_error_code ret = LTTNG_OK; | |
10313 | struct lttng_ht_iter iter; | |
10314 | struct ltt_ust_session *usess = session->ust_session; | |
10315 | ||
10316 | assert(usess); | |
10317 | ||
10318 | rcu_read_lock(); | |
10319 | ||
10320 | switch (usess->buffer_type) { | |
10321 | case LTTNG_BUFFER_PER_UID: | |
10322 | { | |
10323 | struct buffer_reg_uid *reg; | |
10324 | ||
10325 | cds_list_for_each_entry ( | |
10326 | reg, &usess->buffer_reg_uid_list, lnode) { | |
ebdb334b | 10327 | struct buffer_reg_channel *buf_reg_chan; |
04ed9e10 JG |
10328 | struct consumer_socket *socket; |
10329 | ||
10330 | socket = consumer_find_socket_by_bitness( | |
10331 | reg->bits_per_long, usess->consumer); | |
10332 | if (!socket) { | |
10333 | ret = LTTNG_ERR_FATAL; | |
10334 | goto error; | |
10335 | } | |
10336 | ||
10337 | cds_lfht_for_each_entry(reg->registry->channels->ht, | |
ebdb334b | 10338 | &iter.iter, buf_reg_chan, node.node) { |
04ed9e10 JG |
10339 | const int open_ret = |
10340 | consumer_open_channel_packets( | |
10341 | socket, | |
ebdb334b | 10342 | buf_reg_chan->consumer_key); |
04ed9e10 JG |
10343 | |
10344 | if (open_ret < 0) { | |
10345 | ret = LTTNG_ERR_UNK; | |
10346 | goto error; | |
10347 | } | |
10348 | } | |
10349 | } | |
10350 | break; | |
10351 | } | |
10352 | case LTTNG_BUFFER_PER_PID: | |
10353 | { | |
10354 | struct ust_app *app; | |
10355 | ||
10356 | cds_lfht_for_each_entry ( | |
10357 | ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
10358 | struct consumer_socket *socket; | |
10359 | struct lttng_ht_iter chan_iter; | |
10360 | struct ust_app_channel *ua_chan; | |
10361 | struct ust_app_session *ua_sess; | |
10362 | struct ust_registry_session *registry; | |
10363 | ||
10364 | ua_sess = lookup_session_by_app(usess, app); | |
10365 | if (!ua_sess) { | |
10366 | /* Session not associated with this app. */ | |
10367 | continue; | |
10368 | } | |
10369 | ||
10370 | /* Get the right consumer socket for the application. */ | |
10371 | socket = consumer_find_socket_by_bitness( | |
10372 | app->bits_per_long, usess->consumer); | |
10373 | if (!socket) { | |
10374 | ret = LTTNG_ERR_FATAL; | |
10375 | goto error; | |
10376 | } | |
10377 | ||
10378 | registry = get_session_registry(ua_sess); | |
10379 | if (!registry) { | |
10380 | DBG("Application session is being torn down. Skip application."); | |
10381 | continue; | |
10382 | } | |
10383 | ||
10384 | cds_lfht_for_each_entry(ua_sess->channels->ht, | |
10385 | &chan_iter.iter, ua_chan, node.node) { | |
10386 | const int open_ret = | |
10387 | consumer_open_channel_packets( | |
10388 | socket, | |
10389 | ua_chan->key); | |
10390 | ||
10391 | if (open_ret < 0) { | |
10392 | /* | |
10393 | * Per-PID buffer and application going | |
10394 | * away. | |
10395 | */ | |
97a171e1 | 10396 | if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) { |
04ed9e10 JG |
10397 | continue; |
10398 | } | |
10399 | ||
10400 | ret = LTTNG_ERR_UNK; | |
10401 | goto error; | |
10402 | } | |
10403 | } | |
10404 | } | |
10405 | break; | |
10406 | } | |
10407 | default: | |
10408 | abort(); | |
10409 | break; | |
10410 | } | |
10411 | ||
10412 | error: | |
10413 | rcu_read_unlock(); | |
10414 | return ret; | |
10415 | } |