Commit | Line | Data |
---|---|---|
91d76f53 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
91d76f53 DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
d14d33bf AM |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
91d76f53 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
91d76f53 | 20 | #include <errno.h> |
7972aab2 | 21 | #include <inttypes.h> |
91d76f53 DG |
22 | #include <pthread.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
099e26bd | 25 | #include <string.h> |
aba8e916 DG |
26 | #include <sys/stat.h> |
27 | #include <sys/types.h> | |
099e26bd | 28 | #include <unistd.h> |
0df502fd | 29 | #include <urcu/compiler.h> |
fb54cdbf | 30 | #include <lttng/ust-error.h> |
331744e3 | 31 | #include <signal.h> |
bec39940 | 32 | |
990570ed | 33 | #include <common/common.h> |
86acf0da | 34 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 35 | |
7972aab2 | 36 | #include "buffer-registry.h" |
86acf0da | 37 | #include "fd-limit.h" |
8782cc74 | 38 | #include "health-sessiond.h" |
56fff090 | 39 | #include "ust-app.h" |
48842b30 | 40 | #include "ust-consumer.h" |
d80a6244 | 41 | #include "ust-ctl.h" |
0b2dc8df | 42 | #include "utils.h" |
d80a6244 | 43 | |
c4b88406 MD |
44 | static |
45 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); | |
46 | ||
d9bf3ca4 MD |
47 | /* Next available channel key. Access under next_channel_key_lock. */ |
48 | static uint64_t _next_channel_key; | |
49 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
50 | ||
51 | /* Next available session ID. Access under next_session_id_lock. */ | |
52 | static uint64_t _next_session_id; | |
53 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
ffe60014 DG |
54 | |
55 | /* | |
d9bf3ca4 | 56 | * Return the incremented value of next_channel_key. |
ffe60014 | 57 | */ |
d9bf3ca4 | 58 | static uint64_t get_next_channel_key(void) |
ffe60014 | 59 | { |
d9bf3ca4 MD |
60 | uint64_t ret; |
61 | ||
62 | pthread_mutex_lock(&next_channel_key_lock); | |
63 | ret = ++_next_channel_key; | |
64 | pthread_mutex_unlock(&next_channel_key_lock); | |
65 | return ret; | |
ffe60014 DG |
66 | } |
67 | ||
68 | /* | |
7972aab2 | 69 | * Return the atomically incremented value of next_session_id. |
ffe60014 | 70 | */ |
d9bf3ca4 | 71 | static uint64_t get_next_session_id(void) |
ffe60014 | 72 | { |
d9bf3ca4 MD |
73 | uint64_t ret; |
74 | ||
75 | pthread_mutex_lock(&next_session_id_lock); | |
76 | ret = ++_next_session_id; | |
77 | pthread_mutex_unlock(&next_session_id_lock); | |
78 | return ret; | |
ffe60014 DG |
79 | } |
80 | ||
d65d2de8 DG |
81 | static void copy_channel_attr_to_ustctl( |
82 | struct ustctl_consumer_channel_attr *attr, | |
83 | struct lttng_ust_channel_attr *uattr) | |
84 | { | |
85 | /* Copy event attributes since the layout is different. */ | |
86 | attr->subbuf_size = uattr->subbuf_size; | |
87 | attr->num_subbuf = uattr->num_subbuf; | |
88 | attr->overwrite = uattr->overwrite; | |
89 | attr->switch_timer_interval = uattr->switch_timer_interval; | |
90 | attr->read_timer_interval = uattr->read_timer_interval; | |
91 | attr->output = uattr->output; | |
92 | } | |
93 | ||
025faf73 DG |
94 | /* |
95 | * Match function for the hash table lookup. | |
96 | * | |
97 | * It matches an ust app event based on three attributes which are the event | |
98 | * name, the filter bytecode and the loglevel. | |
99 | */ | |
18eace3b DG |
100 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
101 | { | |
102 | struct ust_app_event *event; | |
103 | const struct ust_app_ht_key *key; | |
79a065a0 | 104 | int ev_loglevel_value; |
18eace3b DG |
105 | |
106 | assert(node); | |
107 | assert(_key); | |
108 | ||
109 | event = caa_container_of(node, struct ust_app_event, node.node); | |
110 | key = _key; | |
79a065a0 | 111 | ev_loglevel_value = event->attr.loglevel; |
18eace3b | 112 | |
1af53eb5 | 113 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
18eace3b DG |
114 | |
115 | /* Event name */ | |
116 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
117 | goto no_match; | |
118 | } | |
119 | ||
120 | /* Event loglevel. */ | |
79a065a0 | 121 | if (ev_loglevel_value != key->loglevel_type) { |
025faf73 | 122 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
79a065a0 PP |
123 | && key->loglevel_type == 0 && |
124 | ev_loglevel_value == -1) { | |
025faf73 DG |
125 | /* |
126 | * Match is accepted. This is because on event creation, the | |
127 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and | |
128 | * -1 are accepted for this loglevel type since 0 is the one set by | |
129 | * the API when receiving an enable event. | |
130 | */ | |
131 | } else { | |
132 | goto no_match; | |
133 | } | |
18eace3b DG |
134 | } |
135 | ||
136 | /* One of the filters is NULL, fail. */ | |
137 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
138 | goto no_match; | |
139 | } | |
140 | ||
025faf73 DG |
141 | if (key->filter && event->filter) { |
142 | /* Both filters exists, check length followed by the bytecode. */ | |
143 | if (event->filter->len != key->filter->len || | |
144 | memcmp(event->filter->data, key->filter->data, | |
145 | event->filter->len) != 0) { | |
146 | goto no_match; | |
147 | } | |
18eace3b DG |
148 | } |
149 | ||
1af53eb5 JI |
150 | /* One of the exclusions is NULL, fail. */ |
151 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
152 | goto no_match; | |
153 | } | |
154 | ||
155 | if (key->exclusion && event->exclusion) { | |
156 | /* Both exclusions exists, check count followed by the names. */ | |
157 | if (event->exclusion->count != key->exclusion->count || | |
158 | memcmp(event->exclusion->names, key->exclusion->names, | |
159 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { | |
160 | goto no_match; | |
161 | } | |
162 | } | |
163 | ||
164 | ||
025faf73 | 165 | /* Match. */ |
18eace3b DG |
166 | return 1; |
167 | ||
168 | no_match: | |
169 | return 0; | |
18eace3b DG |
170 | } |
171 | ||
025faf73 DG |
172 | /* |
173 | * Unique add of an ust app event in the given ht. This uses the custom | |
174 | * ht_match_ust_app_event match function and the event name as hash. | |
175 | */ | |
d0b96690 | 176 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
18eace3b DG |
177 | struct ust_app_event *event) |
178 | { | |
179 | struct cds_lfht_node *node_ptr; | |
180 | struct ust_app_ht_key key; | |
d0b96690 | 181 | struct lttng_ht *ht; |
18eace3b | 182 | |
d0b96690 DG |
183 | assert(ua_chan); |
184 | assert(ua_chan->events); | |
18eace3b DG |
185 | assert(event); |
186 | ||
d0b96690 | 187 | ht = ua_chan->events; |
18eace3b DG |
188 | key.name = event->attr.name; |
189 | key.filter = event->filter; | |
79a065a0 | 190 | key.loglevel_type = event->attr.loglevel; |
91c89f23 | 191 | key.exclusion = event->exclusion; |
18eace3b DG |
192 | |
193 | node_ptr = cds_lfht_add_unique(ht->ht, | |
194 | ht->hash_fct(event->node.key, lttng_ht_seed), | |
195 | ht_match_ust_app_event, &key, &event->node.node); | |
196 | assert(node_ptr == &event->node.node); | |
197 | } | |
198 | ||
d88aee68 DG |
199 | /* |
200 | * Close the notify socket from the given RCU head object. This MUST be called | |
201 | * through a call_rcu(). | |
202 | */ | |
203 | static void close_notify_sock_rcu(struct rcu_head *head) | |
204 | { | |
205 | int ret; | |
206 | struct ust_app_notify_sock_obj *obj = | |
207 | caa_container_of(head, struct ust_app_notify_sock_obj, head); | |
208 | ||
209 | /* Must have a valid fd here. */ | |
210 | assert(obj->fd >= 0); | |
211 | ||
212 | ret = close(obj->fd); | |
213 | if (ret) { | |
214 | ERR("close notify sock %d RCU", obj->fd); | |
215 | } | |
216 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
217 | ||
218 | free(obj); | |
219 | } | |
220 | ||
7972aab2 DG |
221 | /* |
222 | * Return the session registry according to the buffer type of the given | |
223 | * session. | |
224 | * | |
225 | * A registry per UID object MUST exists before calling this function or else | |
226 | * it assert() if not found. RCU read side lock must be acquired. | |
227 | */ | |
228 | static struct ust_registry_session *get_session_registry( | |
229 | struct ust_app_session *ua_sess) | |
230 | { | |
231 | struct ust_registry_session *registry = NULL; | |
232 | ||
233 | assert(ua_sess); | |
234 | ||
235 | switch (ua_sess->buffer_type) { | |
236 | case LTTNG_BUFFER_PER_PID: | |
237 | { | |
238 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
239 | if (!reg_pid) { | |
240 | goto error; | |
241 | } | |
242 | registry = reg_pid->registry->reg.ust; | |
243 | break; | |
244 | } | |
245 | case LTTNG_BUFFER_PER_UID: | |
246 | { | |
247 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( | |
248 | ua_sess->tracing_id, ua_sess->bits_per_long, ua_sess->uid); | |
249 | if (!reg_uid) { | |
250 | goto error; | |
251 | } | |
252 | registry = reg_uid->registry->reg.ust; | |
253 | break; | |
254 | } | |
255 | default: | |
256 | assert(0); | |
257 | }; | |
258 | ||
259 | error: | |
260 | return registry; | |
261 | } | |
262 | ||
55cc08a6 DG |
263 | /* |
264 | * Delete ust context safely. RCU read lock must be held before calling | |
265 | * this function. | |
266 | */ | |
267 | static | |
39f0f159 MD |
268 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, |
269 | struct ust_app *app) | |
55cc08a6 | 270 | { |
ffe60014 DG |
271 | int ret; |
272 | ||
273 | assert(ua_ctx); | |
274 | ||
55cc08a6 | 275 | if (ua_ctx->obj) { |
39f0f159 | 276 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 277 | ret = ustctl_release_object(sock, ua_ctx->obj); |
39f0f159 | 278 | pthread_mutex_unlock(&app->sock_lock); |
d0b96690 DG |
279 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
280 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", | |
281 | sock, ua_ctx->obj->handle, ret); | |
ffe60014 | 282 | } |
55cc08a6 DG |
283 | free(ua_ctx->obj); |
284 | } | |
285 | free(ua_ctx); | |
286 | } | |
287 | ||
d80a6244 DG |
288 | /* |
289 | * Delete ust app event safely. RCU read lock must be held before calling | |
290 | * this function. | |
291 | */ | |
8b366481 | 292 | static |
39f0f159 MD |
293 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event, |
294 | struct ust_app *app) | |
d80a6244 | 295 | { |
ffe60014 DG |
296 | int ret; |
297 | ||
298 | assert(ua_event); | |
299 | ||
53a80697 | 300 | free(ua_event->filter); |
951f0b71 JI |
301 | if (ua_event->exclusion != NULL) |
302 | free(ua_event->exclusion); | |
edb67388 | 303 | if (ua_event->obj != NULL) { |
39f0f159 | 304 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 305 | ret = ustctl_release_object(sock, ua_event->obj); |
39f0f159 | 306 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
307 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
308 | ERR("UST app sock %d release event obj failed with ret %d", | |
309 | sock, ret); | |
310 | } | |
edb67388 DG |
311 | free(ua_event->obj); |
312 | } | |
d80a6244 DG |
313 | free(ua_event); |
314 | } | |
315 | ||
316 | /* | |
7972aab2 DG |
317 | * Release ust data object of the given stream. |
318 | * | |
319 | * Return 0 on success or else a negative value. | |
d80a6244 | 320 | */ |
39f0f159 MD |
321 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream, |
322 | struct ust_app *app) | |
d80a6244 | 323 | { |
7972aab2 | 324 | int ret = 0; |
ffe60014 DG |
325 | |
326 | assert(stream); | |
327 | ||
8b366481 | 328 | if (stream->obj) { |
39f0f159 | 329 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 330 | ret = ustctl_release_object(sock, stream->obj); |
39f0f159 | 331 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
332 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
333 | ERR("UST app sock %d release stream obj failed with ret %d", | |
334 | sock, ret); | |
335 | } | |
4063050c | 336 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
337 | free(stream->obj); |
338 | } | |
7972aab2 DG |
339 | |
340 | return ret; | |
341 | } | |
342 | ||
343 | /* | |
344 | * Delete ust app stream safely. RCU read lock must be held before calling | |
345 | * this function. | |
346 | */ | |
347 | static | |
39f0f159 MD |
348 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream, |
349 | struct ust_app *app) | |
7972aab2 DG |
350 | { |
351 | assert(stream); | |
352 | ||
39f0f159 | 353 | (void) release_ust_app_stream(sock, stream, app); |
84cd17c6 | 354 | free(stream); |
d80a6244 DG |
355 | } |
356 | ||
36b588ed MD |
357 | /* |
358 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
359 | * section and outside of call_rcu thread, so we postpone its execution |
360 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
361 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
362 | */ |
363 | static | |
364 | void delete_ust_app_channel_rcu(struct rcu_head *head) | |
365 | { | |
366 | struct ust_app_channel *ua_chan = | |
367 | caa_container_of(head, struct ust_app_channel, rcu_head); | |
368 | ||
0b2dc8df MD |
369 | ht_cleanup_push(ua_chan->ctx); |
370 | ht_cleanup_push(ua_chan->events); | |
36b588ed MD |
371 | free(ua_chan); |
372 | } | |
373 | ||
d80a6244 DG |
374 | /* |
375 | * Delete ust app channel safely. RCU read lock must be held before calling | |
376 | * this function. | |
377 | */ | |
8b366481 | 378 | static |
d0b96690 DG |
379 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
380 | struct ust_app *app) | |
d80a6244 DG |
381 | { |
382 | int ret; | |
bec39940 | 383 | struct lttng_ht_iter iter; |
d80a6244 | 384 | struct ust_app_event *ua_event; |
55cc08a6 | 385 | struct ust_app_ctx *ua_ctx; |
030a66fa | 386 | struct ust_app_stream *stream, *stmp; |
7972aab2 | 387 | struct ust_registry_session *registry; |
d80a6244 | 388 | |
ffe60014 DG |
389 | assert(ua_chan); |
390 | ||
391 | DBG3("UST app deleting channel %s", ua_chan->name); | |
392 | ||
55cc08a6 | 393 | /* Wipe stream */ |
d80a6244 | 394 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
84cd17c6 | 395 | cds_list_del(&stream->list); |
39f0f159 | 396 | delete_ust_app_stream(sock, stream, app); |
d80a6244 DG |
397 | } |
398 | ||
55cc08a6 | 399 | /* Wipe context */ |
bec39940 | 400 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
31746f93 | 401 | cds_list_del(&ua_ctx->list); |
bec39940 | 402 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
55cc08a6 | 403 | assert(!ret); |
39f0f159 | 404 | delete_ust_app_ctx(sock, ua_ctx, app); |
55cc08a6 | 405 | } |
d80a6244 | 406 | |
55cc08a6 | 407 | /* Wipe events */ |
bec39940 DG |
408 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
409 | node.node) { | |
410 | ret = lttng_ht_del(ua_chan->events, &iter); | |
525b0740 | 411 | assert(!ret); |
39f0f159 | 412 | delete_ust_app_event(sock, ua_event, app); |
d80a6244 | 413 | } |
edb67388 | 414 | |
c8335706 MD |
415 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
416 | /* Wipe and free registry from session registry. */ | |
417 | registry = get_session_registry(ua_chan->session); | |
418 | if (registry) { | |
419 | ust_registry_channel_del_free(registry, ua_chan->key); | |
420 | } | |
7972aab2 | 421 | } |
d0b96690 | 422 | |
edb67388 | 423 | if (ua_chan->obj != NULL) { |
d0b96690 DG |
424 | /* Remove channel from application UST object descriptor. */ |
425 | iter.iter.node = &ua_chan->ust_objd_node.node; | |
c6e62271 DG |
426 | ret = lttng_ht_del(app->ust_objd, &iter); |
427 | assert(!ret); | |
39f0f159 | 428 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 429 | ret = ustctl_release_object(sock, ua_chan->obj); |
39f0f159 | 430 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
431 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
432 | ERR("UST app sock %d release channel obj failed with ret %d", | |
433 | sock, ret); | |
434 | } | |
7972aab2 | 435 | lttng_fd_put(LTTNG_FD_APPS, 1); |
edb67388 DG |
436 | free(ua_chan->obj); |
437 | } | |
36b588ed | 438 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
d80a6244 DG |
439 | } |
440 | ||
39f0f159 MD |
441 | int ust_app_register_done(struct ust_app *app) |
442 | { | |
443 | int ret; | |
444 | ||
445 | pthread_mutex_lock(&app->sock_lock); | |
446 | ret = ustctl_register_done(app->sock); | |
447 | pthread_mutex_unlock(&app->sock_lock); | |
448 | return ret; | |
449 | } | |
450 | ||
451 | int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data) | |
452 | { | |
453 | int ret, sock; | |
454 | ||
455 | if (app) { | |
456 | pthread_mutex_lock(&app->sock_lock); | |
457 | sock = app->sock; | |
458 | } else { | |
459 | sock = -1; | |
460 | } | |
461 | ret = ustctl_release_object(sock, data); | |
462 | if (app) { | |
463 | pthread_mutex_unlock(&app->sock_lock); | |
464 | } | |
465 | return ret; | |
466 | } | |
467 | ||
331744e3 | 468 | /* |
1b532a60 DG |
469 | * Push metadata to consumer socket. |
470 | * | |
dc2bbdae MD |
471 | * RCU read-side lock must be held to guarantee existance of socket. |
472 | * Must be called with the ust app session lock held. | |
473 | * Must be called with the registry lock held. | |
331744e3 JD |
474 | * |
475 | * On success, return the len of metadata pushed or else a negative value. | |
2c57e06d MD |
476 | * Returning a -EPIPE return value means we could not send the metadata, |
477 | * but it can be caused by recoverable errors (e.g. the application has | |
478 | * terminated concurrently). | |
331744e3 JD |
479 | */ |
480 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, | |
481 | struct consumer_socket *socket, int send_zero_data) | |
482 | { | |
483 | int ret; | |
484 | char *metadata_str = NULL; | |
288bdb30 | 485 | size_t len, offset, new_metadata_len_sent; |
331744e3 | 486 | ssize_t ret_val; |
288bdb30 | 487 | uint64_t metadata_key; |
331744e3 JD |
488 | |
489 | assert(registry); | |
490 | assert(socket); | |
1b532a60 | 491 | |
288bdb30 MD |
492 | metadata_key = registry->metadata_key; |
493 | ||
ce34fcd0 | 494 | /* |
dc2bbdae MD |
495 | * Means that no metadata was assigned to the session. This can |
496 | * happens if no start has been done previously. | |
ce34fcd0 | 497 | */ |
288bdb30 | 498 | if (!metadata_key) { |
ce34fcd0 MD |
499 | return 0; |
500 | } | |
501 | ||
1b532a60 | 502 | /* |
dc2bbdae MD |
503 | * On a push metadata error either the consumer is dead or the |
504 | * metadata channel has been destroyed because its endpoint | |
2c57e06d MD |
505 | * might have died (e.g: relayd), or because the application has |
506 | * exited. If so, the metadata closed flag is set to 1 so we | |
507 | * deny pushing metadata again which is not valid anymore on the | |
508 | * consumer side. | |
1b532a60 DG |
509 | */ |
510 | if (registry->metadata_closed) { | |
511 | return -EPIPE; | |
512 | } | |
331744e3 | 513 | |
331744e3 JD |
514 | offset = registry->metadata_len_sent; |
515 | len = registry->metadata_len - registry->metadata_len_sent; | |
288bdb30 | 516 | new_metadata_len_sent = registry->metadata_len; |
331744e3 JD |
517 | if (len == 0) { |
518 | DBG3("No metadata to push for metadata key %" PRIu64, | |
519 | registry->metadata_key); | |
520 | ret_val = len; | |
521 | if (send_zero_data) { | |
522 | DBG("No metadata to push"); | |
523 | goto push_data; | |
524 | } | |
525 | goto end; | |
526 | } | |
527 | ||
528 | /* Allocate only what we have to send. */ | |
529 | metadata_str = zmalloc(len); | |
530 | if (!metadata_str) { | |
531 | PERROR("zmalloc ust app metadata string"); | |
532 | ret_val = -ENOMEM; | |
533 | goto error; | |
534 | } | |
288bdb30 | 535 | /* Copy what we haven't sent out. */ |
331744e3 | 536 | memcpy(metadata_str, registry->metadata + offset, len); |
331744e3 JD |
537 | |
538 | push_data: | |
288bdb30 MD |
539 | pthread_mutex_unlock(®istry->lock); |
540 | /* | |
541 | * We need to unlock the registry while we push metadata to | |
542 | * break a circular dependency between the consumerd metadata | |
543 | * lock and the sessiond registry lock. Indeed, pushing metadata | |
544 | * to the consumerd awaits that it gets pushed all the way to | |
545 | * relayd, but doing so requires grabbing the metadata lock. If | |
546 | * a concurrent metadata request is being performed by | |
547 | * consumerd, this can try to grab the registry lock on the | |
548 | * sessiond while holding the metadata lock on the consumer | |
549 | * daemon. Those push and pull schemes are performed on two | |
550 | * different bidirectionnal communication sockets. | |
551 | */ | |
552 | ret = consumer_push_metadata(socket, metadata_key, | |
331744e3 | 553 | metadata_str, len, offset); |
288bdb30 | 554 | pthread_mutex_lock(®istry->lock); |
331744e3 | 555 | if (ret < 0) { |
000baf6a | 556 | /* |
dc2bbdae MD |
557 | * There is an acceptable race here between the registry |
558 | * metadata key assignment and the creation on the | |
559 | * consumer. The session daemon can concurrently push | |
560 | * metadata for this registry while being created on the | |
561 | * consumer since the metadata key of the registry is | |
562 | * assigned *before* it is setup to avoid the consumer | |
563 | * to ask for metadata that could possibly be not found | |
564 | * in the session daemon. | |
000baf6a | 565 | * |
dc2bbdae MD |
566 | * The metadata will get pushed either by the session |
567 | * being stopped or the consumer requesting metadata if | |
568 | * that race is triggered. | |
000baf6a DG |
569 | */ |
570 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { | |
571 | ret = 0; | |
288bdb30 MD |
572 | } else { |
573 | ERR("Error pushing metadata to consumer"); | |
000baf6a | 574 | } |
331744e3 JD |
575 | ret_val = ret; |
576 | goto error_push; | |
288bdb30 MD |
577 | } else { |
578 | /* | |
579 | * Metadata may have been concurrently pushed, since | |
580 | * we're not holding the registry lock while pushing to | |
581 | * consumer. This is handled by the fact that we send | |
582 | * the metadata content, size, and the offset at which | |
583 | * that metadata belongs. This may arrive out of order | |
584 | * on the consumer side, and the consumer is able to | |
585 | * deal with overlapping fragments. The consumer | |
586 | * supports overlapping fragments, which must be | |
587 | * contiguous starting from offset 0. We keep the | |
588 | * largest metadata_len_sent value of the concurrent | |
589 | * send. | |
590 | */ | |
591 | registry->metadata_len_sent = | |
592 | max_t(size_t, registry->metadata_len_sent, | |
593 | new_metadata_len_sent); | |
331744e3 | 594 | } |
331744e3 JD |
595 | free(metadata_str); |
596 | return len; | |
597 | ||
598 | end: | |
599 | error: | |
ce34fcd0 MD |
600 | if (ret_val) { |
601 | /* | |
dc2bbdae MD |
602 | * On error, flag the registry that the metadata is |
603 | * closed. We were unable to push anything and this | |
604 | * means that either the consumer is not responding or | |
605 | * the metadata cache has been destroyed on the | |
606 | * consumer. | |
ce34fcd0 MD |
607 | */ |
608 | registry->metadata_closed = 1; | |
609 | } | |
331744e3 JD |
610 | error_push: |
611 | free(metadata_str); | |
612 | return ret_val; | |
613 | } | |
614 | ||
d88aee68 | 615 | /* |
ce34fcd0 | 616 | * For a given application and session, push metadata to consumer. |
331744e3 JD |
617 | * Either sock or consumer is required : if sock is NULL, the default |
618 | * socket to send the metadata is retrieved from consumer, if sock | |
619 | * is not NULL we use it to send the metadata. | |
ce34fcd0 | 620 | * RCU read-side lock must be held while calling this function, |
dc2bbdae MD |
621 | * therefore ensuring existance of registry. It also ensures existance |
622 | * of socket throughout this function. | |
d88aee68 DG |
623 | * |
624 | * Return 0 on success else a negative error. | |
2c57e06d MD |
625 | * Returning a -EPIPE return value means we could not send the metadata, |
626 | * but it can be caused by recoverable errors (e.g. the application has | |
627 | * terminated concurrently). | |
d88aee68 | 628 | */ |
7972aab2 DG |
629 | static int push_metadata(struct ust_registry_session *registry, |
630 | struct consumer_output *consumer) | |
d88aee68 | 631 | { |
331744e3 JD |
632 | int ret_val; |
633 | ssize_t ret; | |
d88aee68 DG |
634 | struct consumer_socket *socket; |
635 | ||
7972aab2 DG |
636 | assert(registry); |
637 | assert(consumer); | |
638 | ||
ce34fcd0 | 639 | pthread_mutex_lock(®istry->lock); |
ce34fcd0 | 640 | if (registry->metadata_closed) { |
dc2bbdae MD |
641 | ret_val = -EPIPE; |
642 | goto error; | |
d88aee68 DG |
643 | } |
644 | ||
d88aee68 | 645 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
646 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
647 | consumer); | |
d88aee68 | 648 | if (!socket) { |
331744e3 | 649 | ret_val = -1; |
ce34fcd0 | 650 | goto error; |
d88aee68 DG |
651 | } |
652 | ||
331744e3 | 653 | ret = ust_app_push_metadata(registry, socket, 0); |
d88aee68 | 654 | if (ret < 0) { |
331744e3 | 655 | ret_val = ret; |
ce34fcd0 | 656 | goto error; |
d88aee68 | 657 | } |
dc2bbdae | 658 | pthread_mutex_unlock(®istry->lock); |
d88aee68 DG |
659 | return 0; |
660 | ||
ce34fcd0 | 661 | error: |
dc2bbdae | 662 | pthread_mutex_unlock(®istry->lock); |
331744e3 | 663 | return ret_val; |
d88aee68 DG |
664 | } |
665 | ||
666 | /* | |
667 | * Send to the consumer a close metadata command for the given session. Once | |
668 | * done, the metadata channel is deleted and the session metadata pointer is | |
dc2bbdae | 669 | * nullified. The session lock MUST be held unless the application is |
d88aee68 DG |
670 | * in the destroy path. |
671 | * | |
672 | * Return 0 on success else a negative value. | |
673 | */ | |
7972aab2 DG |
674 | static int close_metadata(struct ust_registry_session *registry, |
675 | struct consumer_output *consumer) | |
d88aee68 DG |
676 | { |
677 | int ret; | |
678 | struct consumer_socket *socket; | |
679 | ||
7972aab2 DG |
680 | assert(registry); |
681 | assert(consumer); | |
d88aee68 | 682 | |
7972aab2 DG |
683 | rcu_read_lock(); |
684 | ||
ce34fcd0 MD |
685 | pthread_mutex_lock(®istry->lock); |
686 | ||
7972aab2 | 687 | if (!registry->metadata_key || registry->metadata_closed) { |
d88aee68 | 688 | ret = 0; |
1b532a60 | 689 | goto end; |
d88aee68 DG |
690 | } |
691 | ||
d88aee68 | 692 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
693 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
694 | consumer); | |
d88aee68 DG |
695 | if (!socket) { |
696 | ret = -1; | |
7972aab2 | 697 | goto error; |
d88aee68 DG |
698 | } |
699 | ||
7972aab2 | 700 | ret = consumer_close_metadata(socket, registry->metadata_key); |
d88aee68 | 701 | if (ret < 0) { |
7972aab2 | 702 | goto error; |
d88aee68 DG |
703 | } |
704 | ||
d88aee68 | 705 | error: |
1b532a60 DG |
706 | /* |
707 | * Metadata closed. Even on error this means that the consumer is not | |
708 | * responding or not found so either way a second close should NOT be emit | |
709 | * for this registry. | |
710 | */ | |
711 | registry->metadata_closed = 1; | |
712 | end: | |
ce34fcd0 | 713 | pthread_mutex_unlock(®istry->lock); |
7972aab2 | 714 | rcu_read_unlock(); |
d88aee68 DG |
715 | return ret; |
716 | } | |
717 | ||
36b588ed MD |
718 | /* |
719 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
720 | * section and outside of call_rcu thread, so we postpone its execution |
721 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
722 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
723 | */ |
724 | static | |
725 | void delete_ust_app_session_rcu(struct rcu_head *head) | |
726 | { | |
727 | struct ust_app_session *ua_sess = | |
728 | caa_container_of(head, struct ust_app_session, rcu_head); | |
729 | ||
0b2dc8df | 730 | ht_cleanup_push(ua_sess->channels); |
36b588ed MD |
731 | free(ua_sess); |
732 | } | |
733 | ||
d80a6244 DG |
734 | /* |
735 | * Delete ust app session safely. RCU read lock must be held before calling | |
736 | * this function. | |
737 | */ | |
8b366481 | 738 | static |
d0b96690 DG |
739 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
740 | struct ust_app *app) | |
d80a6244 DG |
741 | { |
742 | int ret; | |
bec39940 | 743 | struct lttng_ht_iter iter; |
d80a6244 | 744 | struct ust_app_channel *ua_chan; |
7972aab2 | 745 | struct ust_registry_session *registry; |
d80a6244 | 746 | |
d88aee68 DG |
747 | assert(ua_sess); |
748 | ||
1b532a60 DG |
749 | pthread_mutex_lock(&ua_sess->lock); |
750 | ||
d40a0cb1 MD |
751 | assert(!ua_sess->deleted); |
752 | ua_sess->deleted = true; | |
753 | ||
7972aab2 | 754 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 755 | if (registry) { |
d88aee68 | 756 | /* Push metadata for application before freeing the application. */ |
7972aab2 | 757 | (void) push_metadata(registry, ua_sess->consumer); |
d88aee68 | 758 | |
7972aab2 DG |
759 | /* |
760 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
761 | * metadata only on destroy trace session in this case. Also, the |
762 | * previous push metadata could have flag the metadata registry to | |
763 | * close so don't send a close command if closed. | |
7972aab2 | 764 | */ |
ce34fcd0 | 765 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
766 | /* And ask to close it for this session registry. */ |
767 | (void) close_metadata(registry, ua_sess->consumer); | |
768 | } | |
d80a6244 DG |
769 | } |
770 | ||
bec39940 DG |
771 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
772 | node.node) { | |
773 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
525b0740 | 774 | assert(!ret); |
d0b96690 | 775 | delete_ust_app_channel(sock, ua_chan, app); |
d80a6244 | 776 | } |
d80a6244 | 777 | |
7972aab2 DG |
778 | /* In case of per PID, the registry is kept in the session. */ |
779 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
780 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
781 | if (reg_pid) { | |
782 | buffer_reg_pid_remove(reg_pid); | |
783 | buffer_reg_pid_destroy(reg_pid); | |
784 | } | |
785 | } | |
d0b96690 | 786 | |
aee6bafd | 787 | if (ua_sess->handle != -1) { |
39f0f159 | 788 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 789 | ret = ustctl_release_handle(sock, ua_sess->handle); |
39f0f159 | 790 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
791 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
792 | ERR("UST app sock %d release session handle failed with ret %d", | |
793 | sock, ret); | |
794 | } | |
aee6bafd | 795 | } |
1b532a60 DG |
796 | pthread_mutex_unlock(&ua_sess->lock); |
797 | ||
95a90d01 MD |
798 | consumer_output_put(ua_sess->consumer); |
799 | ||
36b588ed | 800 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
d80a6244 | 801 | } |
91d76f53 DG |
802 | |
803 | /* | |
284d8f55 DG |
804 | * Delete a traceable application structure from the global list. Never call |
805 | * this function outside of a call_rcu call. | |
36b588ed MD |
806 | * |
807 | * RCU read side lock should _NOT_ be held when calling this function. | |
91d76f53 | 808 | */ |
8b366481 DG |
809 | static |
810 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 811 | { |
8b366481 | 812 | int ret, sock; |
d42f20df | 813 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
44d3bd01 | 814 | |
d80a6244 | 815 | /* Delete ust app sessions info */ |
852d0037 DG |
816 | sock = app->sock; |
817 | app->sock = -1; | |
d80a6244 | 818 | |
8b366481 | 819 | /* Wipe sessions */ |
d42f20df DG |
820 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
821 | teardown_node) { | |
822 | /* Free every object in the session and the session. */ | |
36b588ed | 823 | rcu_read_lock(); |
d0b96690 | 824 | delete_ust_app_session(sock, ua_sess, app); |
36b588ed | 825 | rcu_read_unlock(); |
d80a6244 | 826 | } |
36b588ed | 827 | |
0b2dc8df MD |
828 | ht_cleanup_push(app->sessions); |
829 | ht_cleanup_push(app->ust_objd); | |
d80a6244 | 830 | |
6414a713 | 831 | /* |
852d0037 DG |
832 | * Wait until we have deleted the application from the sock hash table |
833 | * before closing this socket, otherwise an application could re-use the | |
834 | * socket ID and race with the teardown, using the same hash table entry. | |
835 | * | |
836 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
837 | * all RCU readers that could run concurrently with unregister app, | |
838 | * therefore we _need_ to only close that socket after a grace period. So | |
839 | * it should stay in this RCU callback. | |
840 | * | |
841 | * This close() is a very important step of the synchronization model so | |
842 | * every modification to this function must be carefully reviewed. | |
6414a713 | 843 | */ |
799e2c4f MD |
844 | ret = close(sock); |
845 | if (ret) { | |
846 | PERROR("close"); | |
847 | } | |
4063050c | 848 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 849 | |
852d0037 | 850 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 851 | free(app); |
099e26bd DG |
852 | } |
853 | ||
854 | /* | |
f6a9efaa | 855 | * URCU intermediate call to delete an UST app. |
099e26bd | 856 | */ |
8b366481 DG |
857 | static |
858 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 859 | { |
bec39940 DG |
860 | struct lttng_ht_node_ulong *node = |
861 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa | 862 | struct ust_app *app = |
852d0037 | 863 | caa_container_of(node, struct ust_app, pid_n); |
f6a9efaa | 864 | |
852d0037 | 865 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 866 | delete_ust_app(app); |
099e26bd DG |
867 | } |
868 | ||
ffe60014 DG |
869 | /* |
870 | * Delete the session from the application ht and delete the data structure by | |
871 | * freeing every object inside and releasing them. | |
872 | */ | |
d0b96690 | 873 | static void destroy_app_session(struct ust_app *app, |
ffe60014 DG |
874 | struct ust_app_session *ua_sess) |
875 | { | |
876 | int ret; | |
877 | struct lttng_ht_iter iter; | |
878 | ||
879 | assert(app); | |
880 | assert(ua_sess); | |
881 | ||
882 | iter.iter.node = &ua_sess->node.node; | |
883 | ret = lttng_ht_del(app->sessions, &iter); | |
884 | if (ret) { | |
885 | /* Already scheduled for teardown. */ | |
886 | goto end; | |
887 | } | |
888 | ||
889 | /* Once deleted, free the data structure. */ | |
d0b96690 | 890 | delete_ust_app_session(app->sock, ua_sess, app); |
ffe60014 DG |
891 | |
892 | end: | |
893 | return; | |
894 | } | |
895 | ||
8b366481 DG |
896 | /* |
897 | * Alloc new UST app session. | |
898 | */ | |
899 | static | |
d0b96690 | 900 | struct ust_app_session *alloc_ust_app_session(struct ust_app *app) |
8b366481 DG |
901 | { |
902 | struct ust_app_session *ua_sess; | |
903 | ||
904 | /* Init most of the default value by allocating and zeroing */ | |
905 | ua_sess = zmalloc(sizeof(struct ust_app_session)); | |
906 | if (ua_sess == NULL) { | |
907 | PERROR("malloc"); | |
ffe60014 | 908 | goto error_free; |
8b366481 DG |
909 | } |
910 | ||
911 | ua_sess->handle = -1; | |
bec39940 | 912 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
ad7a9107 | 913 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
84ad93e8 | 914 | pthread_mutex_init(&ua_sess->lock, NULL); |
ad7a9107 | 915 | |
8b366481 DG |
916 | return ua_sess; |
917 | ||
ffe60014 | 918 | error_free: |
8b366481 DG |
919 | return NULL; |
920 | } | |
921 | ||
922 | /* | |
923 | * Alloc new UST app channel. | |
924 | */ | |
925 | static | |
926 | struct ust_app_channel *alloc_ust_app_channel(char *name, | |
d0b96690 | 927 | struct ust_app_session *ua_sess, |
ffe60014 | 928 | struct lttng_ust_channel_attr *attr) |
8b366481 DG |
929 | { |
930 | struct ust_app_channel *ua_chan; | |
931 | ||
932 | /* Init most of the default value by allocating and zeroing */ | |
933 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); | |
934 | if (ua_chan == NULL) { | |
935 | PERROR("malloc"); | |
936 | goto error; | |
937 | } | |
938 | ||
939 | /* Setup channel name */ | |
940 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
941 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
942 | ||
943 | ua_chan->enabled = 1; | |
944 | ua_chan->handle = -1; | |
45893984 | 945 | ua_chan->session = ua_sess; |
ffe60014 | 946 | ua_chan->key = get_next_channel_key(); |
bec39940 DG |
947 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
948 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
949 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
950 | |
951 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
31746f93 | 952 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
8b366481 DG |
953 | |
954 | /* Copy attributes */ | |
955 | if (attr) { | |
ffe60014 | 956 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
2fe6e7f5 DG |
957 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
958 | ua_chan->attr.num_subbuf = attr->num_subbuf; | |
959 | ua_chan->attr.overwrite = attr->overwrite; | |
960 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; | |
961 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; | |
962 | ua_chan->attr.output = attr->output; | |
8b366481 | 963 | } |
ffe60014 DG |
964 | /* By default, the channel is a per cpu channel. */ |
965 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8b366481 DG |
966 | |
967 | DBG3("UST app channel %s allocated", ua_chan->name); | |
968 | ||
969 | return ua_chan; | |
970 | ||
971 | error: | |
972 | return NULL; | |
973 | } | |
974 | ||
37f1c236 DG |
975 | /* |
976 | * Allocate and initialize a UST app stream. | |
977 | * | |
978 | * Return newly allocated stream pointer or NULL on error. | |
979 | */ | |
ffe60014 | 980 | struct ust_app_stream *ust_app_alloc_stream(void) |
37f1c236 DG |
981 | { |
982 | struct ust_app_stream *stream = NULL; | |
983 | ||
984 | stream = zmalloc(sizeof(*stream)); | |
985 | if (stream == NULL) { | |
986 | PERROR("zmalloc ust app stream"); | |
987 | goto error; | |
988 | } | |
989 | ||
990 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
991 | stream->handle = -1; | |
992 | ||
993 | error: | |
994 | return stream; | |
995 | } | |
996 | ||
8b366481 DG |
997 | /* |
998 | * Alloc new UST app event. | |
999 | */ | |
1000 | static | |
1001 | struct ust_app_event *alloc_ust_app_event(char *name, | |
1002 | struct lttng_ust_event *attr) | |
1003 | { | |
1004 | struct ust_app_event *ua_event; | |
1005 | ||
1006 | /* Init most of the default value by allocating and zeroing */ | |
1007 | ua_event = zmalloc(sizeof(struct ust_app_event)); | |
1008 | if (ua_event == NULL) { | |
1009 | PERROR("malloc"); | |
1010 | goto error; | |
1011 | } | |
1012 | ||
1013 | ua_event->enabled = 1; | |
1014 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
1015 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 | 1016 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
8b366481 DG |
1017 | |
1018 | /* Copy attributes */ | |
1019 | if (attr) { | |
1020 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
1021 | } | |
1022 | ||
1023 | DBG3("UST app event %s allocated", ua_event->name); | |
1024 | ||
1025 | return ua_event; | |
1026 | ||
1027 | error: | |
1028 | return NULL; | |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * Alloc new UST app context. | |
1033 | */ | |
1034 | static | |
1035 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx) | |
1036 | { | |
1037 | struct ust_app_ctx *ua_ctx; | |
1038 | ||
1039 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); | |
1040 | if (ua_ctx == NULL) { | |
1041 | goto error; | |
1042 | } | |
1043 | ||
31746f93 DG |
1044 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
1045 | ||
8b366481 DG |
1046 | if (uctx) { |
1047 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
1048 | } | |
1049 | ||
1050 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
1051 | ||
1052 | error: | |
1053 | return ua_ctx; | |
1054 | } | |
1055 | ||
025faf73 DG |
1056 | /* |
1057 | * Allocate a filter and copy the given original filter. | |
1058 | * | |
1059 | * Return allocated filter or NULL on error. | |
1060 | */ | |
0a3fca4d JG |
1061 | static struct lttng_filter_bytecode *copy_filter_bytecode( |
1062 | struct lttng_filter_bytecode *orig_f) | |
025faf73 | 1063 | { |
0a3fca4d | 1064 | struct lttng_filter_bytecode *filter = NULL; |
025faf73 DG |
1065 | |
1066 | /* Copy filter bytecode */ | |
1067 | filter = zmalloc(sizeof(*filter) + orig_f->len); | |
1068 | if (!filter) { | |
0a3fca4d | 1069 | PERROR("zmalloc alloc filter bytecode"); |
025faf73 DG |
1070 | goto error; |
1071 | } | |
1072 | ||
1073 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1074 | ||
1075 | error: | |
1076 | return filter; | |
1077 | } | |
1078 | ||
0a3fca4d JG |
1079 | /* |
1080 | * Create a liblttng-ust filter bytecode from given bytecode. | |
1081 | * | |
1082 | * Return allocated filter or NULL on error. | |
1083 | */ | |
1084 | static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode( | |
1085 | struct lttng_filter_bytecode *orig_f) | |
1086 | { | |
1087 | struct lttng_ust_filter_bytecode *filter = NULL; | |
1088 | ||
1089 | /* Copy filter bytecode */ | |
1090 | filter = zmalloc(sizeof(*filter) + orig_f->len); | |
1091 | if (!filter) { | |
1092 | PERROR("zmalloc alloc ust filter bytecode"); | |
1093 | goto error; | |
1094 | } | |
1095 | ||
1096 | assert(sizeof(struct lttng_filter_bytecode) == | |
1097 | sizeof(struct lttng_ust_filter_bytecode)); | |
1098 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1099 | error: | |
1100 | return filter; | |
1101 | } | |
1102 | ||
099e26bd | 1103 | /* |
421cb601 DG |
1104 | * Find an ust_app using the sock and return it. RCU read side lock must be |
1105 | * held before calling this helper function. | |
099e26bd | 1106 | */ |
f20baf8e | 1107 | struct ust_app *ust_app_find_by_sock(int sock) |
099e26bd | 1108 | { |
bec39940 | 1109 | struct lttng_ht_node_ulong *node; |
bec39940 | 1110 | struct lttng_ht_iter iter; |
f6a9efaa | 1111 | |
852d0037 | 1112 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1113 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
1114 | if (node == NULL) { |
1115 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
1116 | goto error; |
1117 | } | |
852d0037 DG |
1118 | |
1119 | return caa_container_of(node, struct ust_app, sock_n); | |
f6a9efaa DG |
1120 | |
1121 | error: | |
1122 | return NULL; | |
099e26bd DG |
1123 | } |
1124 | ||
d0b96690 DG |
1125 | /* |
1126 | * Find an ust_app using the notify sock and return it. RCU read side lock must | |
1127 | * be held before calling this helper function. | |
1128 | */ | |
1129 | static struct ust_app *find_app_by_notify_sock(int sock) | |
1130 | { | |
1131 | struct lttng_ht_node_ulong *node; | |
1132 | struct lttng_ht_iter iter; | |
1133 | ||
1134 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), | |
1135 | &iter); | |
1136 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1137 | if (node == NULL) { | |
1138 | DBG2("UST app find by notify sock %d not found", sock); | |
1139 | goto error; | |
1140 | } | |
1141 | ||
1142 | return caa_container_of(node, struct ust_app, notify_sock_n); | |
1143 | ||
1144 | error: | |
1145 | return NULL; | |
1146 | } | |
1147 | ||
025faf73 DG |
1148 | /* |
1149 | * Lookup for an ust app event based on event name, filter bytecode and the | |
1150 | * event loglevel. | |
1151 | * | |
1152 | * Return an ust_app_event object or NULL on error. | |
1153 | */ | |
18eace3b | 1154 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
79a065a0 PP |
1155 | char *name, struct lttng_filter_bytecode *filter, |
1156 | int loglevel_value, | |
39c5a3a7 | 1157 | const struct lttng_event_exclusion *exclusion) |
18eace3b DG |
1158 | { |
1159 | struct lttng_ht_iter iter; | |
1160 | struct lttng_ht_node_str *node; | |
1161 | struct ust_app_event *event = NULL; | |
1162 | struct ust_app_ht_key key; | |
18eace3b DG |
1163 | |
1164 | assert(name); | |
1165 | assert(ht); | |
1166 | ||
1167 | /* Setup key for event lookup. */ | |
1168 | key.name = name; | |
1169 | key.filter = filter; | |
79a065a0 | 1170 | key.loglevel_type = loglevel_value; |
39c5a3a7 | 1171 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
0a3fca4d | 1172 | key.exclusion = exclusion; |
18eace3b | 1173 | |
025faf73 DG |
1174 | /* Lookup using the event name as hash and a custom match fct. */ |
1175 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), | |
1176 | ht_match_ust_app_event, &key, &iter.iter); | |
18eace3b DG |
1177 | node = lttng_ht_iter_get_node_str(&iter); |
1178 | if (node == NULL) { | |
1179 | goto end; | |
1180 | } | |
1181 | ||
1182 | event = caa_container_of(node, struct ust_app_event, node); | |
1183 | ||
1184 | end: | |
18eace3b DG |
1185 | return event; |
1186 | } | |
1187 | ||
55cc08a6 DG |
1188 | /* |
1189 | * Create the channel context on the tracer. | |
d0b96690 DG |
1190 | * |
1191 | * Called with UST app session lock held. | |
55cc08a6 DG |
1192 | */ |
1193 | static | |
1194 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
1195 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
1196 | { | |
1197 | int ret; | |
1198 | ||
840cb59c | 1199 | health_code_update(); |
86acf0da | 1200 | |
39f0f159 | 1201 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1202 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 | 1203 | ua_chan->obj, &ua_ctx->obj); |
39f0f159 | 1204 | pthread_mutex_unlock(&app->sock_lock); |
55cc08a6 | 1205 | if (ret < 0) { |
ffe60014 DG |
1206 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1207 | ERR("UST app create channel context failed for app (pid: %d) " | |
1208 | "with ret %d", app->pid, ret); | |
1209 | } else { | |
3757b385 DG |
1210 | /* |
1211 | * This is normal behavior, an application can die during the | |
1212 | * creation process. Don't report an error so the execution can | |
1213 | * continue normally. | |
1214 | */ | |
1215 | ret = 0; | |
ffe60014 DG |
1216 | DBG3("UST app disable event failed. Application is dead."); |
1217 | } | |
55cc08a6 DG |
1218 | goto error; |
1219 | } | |
1220 | ||
1221 | ua_ctx->handle = ua_ctx->obj->handle; | |
1222 | ||
d0b96690 DG |
1223 | DBG2("UST app context handle %d created successfully for channel %s", |
1224 | ua_ctx->handle, ua_chan->name); | |
55cc08a6 DG |
1225 | |
1226 | error: | |
840cb59c | 1227 | health_code_update(); |
55cc08a6 DG |
1228 | return ret; |
1229 | } | |
1230 | ||
53a80697 MD |
1231 | /* |
1232 | * Set the filter on the tracer. | |
1233 | */ | |
1234 | static | |
1235 | int set_ust_event_filter(struct ust_app_event *ua_event, | |
1236 | struct ust_app *app) | |
1237 | { | |
1238 | int ret; | |
0a3fca4d | 1239 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
53a80697 | 1240 | |
840cb59c | 1241 | health_code_update(); |
86acf0da | 1242 | |
53a80697 | 1243 | if (!ua_event->filter) { |
86acf0da DG |
1244 | ret = 0; |
1245 | goto error; | |
53a80697 MD |
1246 | } |
1247 | ||
0a3fca4d JG |
1248 | ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter); |
1249 | if (!ust_bytecode) { | |
1250 | ret = -LTTNG_ERR_NOMEM; | |
1251 | goto error; | |
1252 | } | |
39f0f159 | 1253 | pthread_mutex_lock(&app->sock_lock); |
0a3fca4d | 1254 | ret = ustctl_set_filter(app->sock, ust_bytecode, |
53a80697 | 1255 | ua_event->obj); |
39f0f159 | 1256 | pthread_mutex_unlock(&app->sock_lock); |
53a80697 | 1257 | if (ret < 0) { |
ffe60014 DG |
1258 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1259 | ERR("UST app event %s filter failed for app (pid: %d) " | |
1260 | "with ret %d", ua_event->attr.name, app->pid, ret); | |
1261 | } else { | |
3757b385 DG |
1262 | /* |
1263 | * This is normal behavior, an application can die during the | |
1264 | * creation process. Don't report an error so the execution can | |
1265 | * continue normally. | |
1266 | */ | |
1267 | ret = 0; | |
ffe60014 DG |
1268 | DBG3("UST app filter event failed. Application is dead."); |
1269 | } | |
53a80697 MD |
1270 | goto error; |
1271 | } | |
1272 | ||
1273 | DBG2("UST filter set successfully for event %s", ua_event->name); | |
1274 | ||
1275 | error: | |
840cb59c | 1276 | health_code_update(); |
0a3fca4d | 1277 | free(ust_bytecode); |
53a80697 MD |
1278 | return ret; |
1279 | } | |
1280 | ||
0a3fca4d JG |
1281 | static |
1282 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( | |
1283 | struct lttng_event_exclusion *exclusion) | |
1284 | { | |
1285 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; | |
1286 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + | |
1287 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; | |
1288 | ||
1289 | ust_exclusion = zmalloc(exclusion_alloc_size); | |
1290 | if (!ust_exclusion) { | |
1291 | PERROR("malloc"); | |
1292 | goto end; | |
1293 | } | |
1294 | ||
1295 | assert(sizeof(struct lttng_event_exclusion) == | |
1296 | sizeof(struct lttng_ust_event_exclusion)); | |
1297 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); | |
1298 | end: | |
1299 | return ust_exclusion; | |
1300 | } | |
1301 | ||
7cc9a73c JI |
1302 | /* |
1303 | * Set event exclusions on the tracer. | |
1304 | */ | |
1305 | static | |
1306 | int set_ust_event_exclusion(struct ust_app_event *ua_event, | |
1307 | struct ust_app *app) | |
1308 | { | |
1309 | int ret; | |
0a3fca4d | 1310 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; |
7cc9a73c JI |
1311 | |
1312 | health_code_update(); | |
1313 | ||
1314 | if (!ua_event->exclusion || !ua_event->exclusion->count) { | |
1315 | ret = 0; | |
1316 | goto error; | |
1317 | } | |
1318 | ||
0a3fca4d JG |
1319 | ust_exclusion = create_ust_exclusion_from_exclusion( |
1320 | ua_event->exclusion); | |
1321 | if (!ust_exclusion) { | |
1322 | ret = -LTTNG_ERR_NOMEM; | |
1323 | goto error; | |
1324 | } | |
39f0f159 | 1325 | pthread_mutex_lock(&app->sock_lock); |
0a3fca4d | 1326 | ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj); |
39f0f159 | 1327 | pthread_mutex_unlock(&app->sock_lock); |
7cc9a73c JI |
1328 | if (ret < 0) { |
1329 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
1330 | ERR("UST app event %s exclusions failed for app (pid: %d) " | |
1331 | "with ret %d", ua_event->attr.name, app->pid, ret); | |
1332 | } else { | |
1333 | /* | |
1334 | * This is normal behavior, an application can die during the | |
1335 | * creation process. Don't report an error so the execution can | |
1336 | * continue normally. | |
1337 | */ | |
1338 | ret = 0; | |
1339 | DBG3("UST app event exclusion failed. Application is dead."); | |
1340 | } | |
1341 | goto error; | |
1342 | } | |
1343 | ||
1344 | DBG2("UST exclusion set successfully for event %s", ua_event->name); | |
1345 | ||
1346 | error: | |
1347 | health_code_update(); | |
0a3fca4d | 1348 | free(ust_exclusion); |
7cc9a73c JI |
1349 | return ret; |
1350 | } | |
1351 | ||
9730260e DG |
1352 | /* |
1353 | * Disable the specified event on to UST tracer for the UST session. | |
1354 | */ | |
1355 | static int disable_ust_event(struct ust_app *app, | |
1356 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
1357 | { | |
1358 | int ret; | |
1359 | ||
840cb59c | 1360 | health_code_update(); |
86acf0da | 1361 | |
39f0f159 | 1362 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1363 | ret = ustctl_disable(app->sock, ua_event->obj); |
39f0f159 | 1364 | pthread_mutex_unlock(&app->sock_lock); |
9730260e | 1365 | if (ret < 0) { |
ffe60014 DG |
1366 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1367 | ERR("UST app event %s disable failed for app (pid: %d) " | |
1368 | "and session handle %d with ret %d", | |
1369 | ua_event->attr.name, app->pid, ua_sess->handle, ret); | |
1370 | } else { | |
3757b385 DG |
1371 | /* |
1372 | * This is normal behavior, an application can die during the | |
1373 | * creation process. Don't report an error so the execution can | |
1374 | * continue normally. | |
1375 | */ | |
1376 | ret = 0; | |
ffe60014 DG |
1377 | DBG3("UST app disable event failed. Application is dead."); |
1378 | } | |
9730260e DG |
1379 | goto error; |
1380 | } | |
1381 | ||
1382 | DBG2("UST app event %s disabled successfully for app (pid: %d)", | |
852d0037 | 1383 | ua_event->attr.name, app->pid); |
9730260e DG |
1384 | |
1385 | error: | |
840cb59c | 1386 | health_code_update(); |
9730260e DG |
1387 | return ret; |
1388 | } | |
1389 | ||
78f0bacd DG |
1390 | /* |
1391 | * Disable the specified channel on to UST tracer for the UST session. | |
1392 | */ | |
1393 | static int disable_ust_channel(struct ust_app *app, | |
1394 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1395 | { | |
1396 | int ret; | |
1397 | ||
840cb59c | 1398 | health_code_update(); |
86acf0da | 1399 | |
39f0f159 | 1400 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1401 | ret = ustctl_disable(app->sock, ua_chan->obj); |
39f0f159 | 1402 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1403 | if (ret < 0) { |
ffe60014 DG |
1404 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1405 | ERR("UST app channel %s disable failed for app (pid: %d) " | |
1406 | "and session handle %d with ret %d", | |
1407 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1408 | } else { | |
3757b385 DG |
1409 | /* |
1410 | * This is normal behavior, an application can die during the | |
1411 | * creation process. Don't report an error so the execution can | |
1412 | * continue normally. | |
1413 | */ | |
1414 | ret = 0; | |
ffe60014 DG |
1415 | DBG3("UST app disable channel failed. Application is dead."); |
1416 | } | |
78f0bacd DG |
1417 | goto error; |
1418 | } | |
1419 | ||
78f0bacd | 1420 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
852d0037 | 1421 | ua_chan->name, app->pid); |
78f0bacd DG |
1422 | |
1423 | error: | |
840cb59c | 1424 | health_code_update(); |
78f0bacd DG |
1425 | return ret; |
1426 | } | |
1427 | ||
1428 | /* | |
1429 | * Enable the specified channel on to UST tracer for the UST session. | |
1430 | */ | |
1431 | static int enable_ust_channel(struct ust_app *app, | |
1432 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1433 | { | |
1434 | int ret; | |
1435 | ||
840cb59c | 1436 | health_code_update(); |
86acf0da | 1437 | |
39f0f159 | 1438 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1439 | ret = ustctl_enable(app->sock, ua_chan->obj); |
39f0f159 | 1440 | pthread_mutex_unlock(&app->sock_lock); |
78f0bacd | 1441 | if (ret < 0) { |
ffe60014 DG |
1442 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1443 | ERR("UST app channel %s enable failed for app (pid: %d) " | |
1444 | "and session handle %d with ret %d", | |
1445 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1446 | } else { | |
3757b385 DG |
1447 | /* |
1448 | * This is normal behavior, an application can die during the | |
1449 | * creation process. Don't report an error so the execution can | |
1450 | * continue normally. | |
1451 | */ | |
1452 | ret = 0; | |
ffe60014 DG |
1453 | DBG3("UST app enable channel failed. Application is dead."); |
1454 | } | |
78f0bacd DG |
1455 | goto error; |
1456 | } | |
1457 | ||
1458 | ua_chan->enabled = 1; | |
1459 | ||
1460 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", | |
852d0037 | 1461 | ua_chan->name, app->pid); |
78f0bacd DG |
1462 | |
1463 | error: | |
840cb59c | 1464 | health_code_update(); |
78f0bacd DG |
1465 | return ret; |
1466 | } | |
1467 | ||
edb67388 DG |
1468 | /* |
1469 | * Enable the specified event on to UST tracer for the UST session. | |
1470 | */ | |
1471 | static int enable_ust_event(struct ust_app *app, | |
1472 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
1473 | { | |
1474 | int ret; | |
1475 | ||
840cb59c | 1476 | health_code_update(); |
86acf0da | 1477 | |
39f0f159 | 1478 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1479 | ret = ustctl_enable(app->sock, ua_event->obj); |
39f0f159 | 1480 | pthread_mutex_unlock(&app->sock_lock); |
edb67388 | 1481 | if (ret < 0) { |
ffe60014 DG |
1482 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1483 | ERR("UST app event %s enable failed for app (pid: %d) " | |
1484 | "and session handle %d with ret %d", | |
1485 | ua_event->attr.name, app->pid, ua_sess->handle, ret); | |
1486 | } else { | |
3757b385 DG |
1487 | /* |
1488 | * This is normal behavior, an application can die during the | |
1489 | * creation process. Don't report an error so the execution can | |
1490 | * continue normally. | |
1491 | */ | |
1492 | ret = 0; | |
ffe60014 DG |
1493 | DBG3("UST app enable event failed. Application is dead."); |
1494 | } | |
edb67388 DG |
1495 | goto error; |
1496 | } | |
1497 | ||
1498 | DBG2("UST app event %s enabled successfully for app (pid: %d)", | |
852d0037 | 1499 | ua_event->attr.name, app->pid); |
edb67388 DG |
1500 | |
1501 | error: | |
840cb59c | 1502 | health_code_update(); |
edb67388 DG |
1503 | return ret; |
1504 | } | |
1505 | ||
099e26bd | 1506 | /* |
7972aab2 | 1507 | * Send channel and stream buffer to application. |
4f3ab6ee | 1508 | * |
ffe60014 | 1509 | * Return 0 on success. On error, a negative value is returned. |
4f3ab6ee | 1510 | */ |
7972aab2 DG |
1511 | static int send_channel_pid_to_ust(struct ust_app *app, |
1512 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
4f3ab6ee DG |
1513 | { |
1514 | int ret; | |
ffe60014 | 1515 | struct ust_app_stream *stream, *stmp; |
4f3ab6ee DG |
1516 | |
1517 | assert(app); | |
ffe60014 | 1518 | assert(ua_sess); |
4f3ab6ee | 1519 | assert(ua_chan); |
4f3ab6ee | 1520 | |
840cb59c | 1521 | health_code_update(); |
4f3ab6ee | 1522 | |
7972aab2 DG |
1523 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
1524 | app->sock); | |
86acf0da | 1525 | |
ffe60014 DG |
1526 | /* Send channel to the application. */ |
1527 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
320b3a68 MD |
1528 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1529 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1530 | goto error; | |
1531 | } else if (ret < 0) { | |
b551a063 DG |
1532 | goto error; |
1533 | } | |
1534 | ||
d88aee68 DG |
1535 | health_code_update(); |
1536 | ||
ffe60014 DG |
1537 | /* Send all streams to application. */ |
1538 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
1539 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); | |
320b3a68 MD |
1540 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1541 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1542 | goto error; | |
1543 | } else if (ret < 0) { | |
ffe60014 DG |
1544 | goto error; |
1545 | } | |
1546 | /* We don't need the stream anymore once sent to the tracer. */ | |
1547 | cds_list_del(&stream->list); | |
39f0f159 | 1548 | delete_ust_app_stream(-1, stream, app); |
ffe60014 | 1549 | } |
ffe60014 DG |
1550 | /* Flag the channel that it is sent to the application. */ |
1551 | ua_chan->is_sent = 1; | |
ffe60014 | 1552 | |
b551a063 | 1553 | error: |
840cb59c | 1554 | health_code_update(); |
b551a063 DG |
1555 | return ret; |
1556 | } | |
1557 | ||
91d76f53 | 1558 | /* |
5b4a0ec0 | 1559 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
1560 | * |
1561 | * Should be called with session mutex held. | |
91d76f53 | 1562 | */ |
edb67388 DG |
1563 | static |
1564 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
1565 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) | |
91d76f53 | 1566 | { |
5b4a0ec0 | 1567 | int ret = 0; |
284d8f55 | 1568 | |
840cb59c | 1569 | health_code_update(); |
86acf0da | 1570 | |
5b4a0ec0 | 1571 | /* Create UST event on tracer */ |
39f0f159 | 1572 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 1573 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 | 1574 | &ua_event->obj); |
39f0f159 | 1575 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 | 1576 | if (ret < 0) { |
ffe60014 DG |
1577 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1578 | ERR("Error ustctl create event %s for app pid: %d with ret %d", | |
1579 | ua_event->attr.name, app->pid, ret); | |
1580 | } else { | |
3757b385 DG |
1581 | /* |
1582 | * This is normal behavior, an application can die during the | |
1583 | * creation process. Don't report an error so the execution can | |
1584 | * continue normally. | |
1585 | */ | |
1586 | ret = 0; | |
ffe60014 DG |
1587 | DBG3("UST app create event failed. Application is dead."); |
1588 | } | |
5b4a0ec0 | 1589 | goto error; |
91d76f53 | 1590 | } |
f6a9efaa | 1591 | |
5b4a0ec0 | 1592 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 1593 | |
5b4a0ec0 | 1594 | DBG2("UST app event %s created successfully for pid:%d", |
852d0037 | 1595 | ua_event->attr.name, app->pid); |
f6a9efaa | 1596 | |
840cb59c | 1597 | health_code_update(); |
86acf0da | 1598 | |
025faf73 DG |
1599 | /* Set filter if one is present. */ |
1600 | if (ua_event->filter) { | |
1601 | ret = set_ust_event_filter(ua_event, app); | |
1602 | if (ret < 0) { | |
1603 | goto error; | |
1604 | } | |
1605 | } | |
1606 | ||
7cc9a73c JI |
1607 | /* Set exclusions for the event */ |
1608 | if (ua_event->exclusion) { | |
1609 | ret = set_ust_event_exclusion(ua_event, app); | |
1610 | if (ret < 0) { | |
1611 | goto error; | |
1612 | } | |
1613 | } | |
1614 | ||
8535a6d9 | 1615 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
1616 | if (ua_event->enabled) { |
1617 | /* | |
1618 | * We now need to explicitly enable the event, since it | |
1619 | * is now disabled at creation. | |
1620 | */ | |
1621 | ret = enable_ust_event(app, ua_sess, ua_event); | |
1622 | if (ret < 0) { | |
1623 | /* | |
1624 | * If we hit an EPERM, something is wrong with our enable call. If | |
1625 | * we get an EEXIST, there is a problem on the tracer side since we | |
1626 | * just created it. | |
1627 | */ | |
1628 | switch (ret) { | |
1629 | case -LTTNG_UST_ERR_PERM: | |
1630 | /* Code flow problem */ | |
1631 | assert(0); | |
1632 | case -LTTNG_UST_ERR_EXIST: | |
1633 | /* It's OK for our use case. */ | |
1634 | ret = 0; | |
1635 | break; | |
1636 | default: | |
1637 | break; | |
1638 | } | |
1639 | goto error; | |
1640 | } | |
8535a6d9 DG |
1641 | } |
1642 | ||
5b4a0ec0 | 1643 | error: |
840cb59c | 1644 | health_code_update(); |
5b4a0ec0 | 1645 | return ret; |
91d76f53 | 1646 | } |
48842b30 | 1647 | |
5b4a0ec0 DG |
1648 | /* |
1649 | * Copy data between an UST app event and a LTT event. | |
1650 | */ | |
421cb601 | 1651 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
1652 | struct ltt_ust_event *uevent) |
1653 | { | |
b4ffad32 JI |
1654 | size_t exclusion_alloc_size; |
1655 | ||
48842b30 DG |
1656 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
1657 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
1658 | ||
fc34caaa DG |
1659 | ua_event->enabled = uevent->enabled; |
1660 | ||
5b4a0ec0 DG |
1661 | /* Copy event attributes */ |
1662 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
1663 | ||
53a80697 MD |
1664 | /* Copy filter bytecode */ |
1665 | if (uevent->filter) { | |
0a3fca4d | 1666 | ua_event->filter = copy_filter_bytecode(uevent->filter); |
025faf73 | 1667 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 1668 | } |
b4ffad32 JI |
1669 | |
1670 | /* Copy exclusion data */ | |
1671 | if (uevent->exclusion) { | |
0a3fca4d | 1672 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
b4ffad32 JI |
1673 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
1674 | ua_event->exclusion = zmalloc(exclusion_alloc_size); | |
5f8df26c JI |
1675 | if (ua_event->exclusion == NULL) { |
1676 | PERROR("malloc"); | |
1677 | } else { | |
1678 | memcpy(ua_event->exclusion, uevent->exclusion, | |
1679 | exclusion_alloc_size); | |
b4ffad32 JI |
1680 | } |
1681 | } | |
48842b30 DG |
1682 | } |
1683 | ||
5b4a0ec0 DG |
1684 | /* |
1685 | * Copy data between an UST app channel and a LTT channel. | |
1686 | */ | |
421cb601 | 1687 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
1688 | struct ltt_ust_channel *uchan) |
1689 | { | |
bec39940 | 1690 | struct lttng_ht_iter iter; |
48842b30 | 1691 | struct ltt_ust_event *uevent; |
55cc08a6 | 1692 | struct ltt_ust_context *uctx; |
48842b30 | 1693 | struct ust_app_event *ua_event; |
55cc08a6 | 1694 | struct ust_app_ctx *ua_ctx; |
48842b30 | 1695 | |
fc34caaa | 1696 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
1697 | |
1698 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
1699 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 1700 | |
1624d5b7 JD |
1701 | ua_chan->tracefile_size = uchan->tracefile_size; |
1702 | ua_chan->tracefile_count = uchan->tracefile_count; | |
1703 | ||
ffe60014 DG |
1704 | /* Copy event attributes since the layout is different. */ |
1705 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
1706 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
1707 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
1708 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
1709 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
1710 | ua_chan->attr.output = uchan->attr.output; | |
1711 | /* | |
1712 | * Note that the attribute channel type is not set since the channel on the | |
1713 | * tracing registry side does not have this information. | |
1714 | */ | |
48842b30 | 1715 | |
fc34caaa | 1716 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 1717 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 1718 | |
31746f93 | 1719 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { |
55cc08a6 DG |
1720 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
1721 | if (ua_ctx == NULL) { | |
1722 | continue; | |
1723 | } | |
bec39940 DG |
1724 | lttng_ht_node_init_ulong(&ua_ctx->node, |
1725 | (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 1726 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 1727 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
55cc08a6 | 1728 | } |
48842b30 | 1729 | |
421cb601 | 1730 | /* Copy all events from ltt ust channel to ust app channel */ |
bec39940 | 1731 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
18eace3b | 1732 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
39c5a3a7 | 1733 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 1734 | if (ua_event == NULL) { |
421cb601 | 1735 | DBG2("UST event %s not found on shadow copy channel", |
48842b30 | 1736 | uevent->attr.name); |
284d8f55 | 1737 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
48842b30 | 1738 | if (ua_event == NULL) { |
5b4a0ec0 | 1739 | continue; |
48842b30 | 1740 | } |
421cb601 | 1741 | shadow_copy_event(ua_event, uevent); |
d0b96690 | 1742 | add_unique_ust_app_event(ua_chan, ua_event); |
48842b30 | 1743 | } |
48842b30 DG |
1744 | } |
1745 | ||
fc34caaa | 1746 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
1747 | } |
1748 | ||
5b4a0ec0 DG |
1749 | /* |
1750 | * Copy data between a UST app session and a regular LTT session. | |
1751 | */ | |
421cb601 | 1752 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 1753 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 1754 | { |
bec39940 DG |
1755 | struct lttng_ht_node_str *ua_chan_node; |
1756 | struct lttng_ht_iter iter; | |
48842b30 DG |
1757 | struct ltt_ust_channel *uchan; |
1758 | struct ust_app_channel *ua_chan; | |
477d7741 MD |
1759 | time_t rawtime; |
1760 | struct tm *timeinfo; | |
1761 | char datetime[16]; | |
1762 | int ret; | |
d7ba1388 | 1763 | char tmp_shm_path[PATH_MAX]; |
477d7741 MD |
1764 | |
1765 | /* Get date and time for unique app path */ | |
1766 | time(&rawtime); | |
1767 | timeinfo = localtime(&rawtime); | |
1768 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
48842b30 | 1769 | |
421cb601 | 1770 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 1771 | |
7972aab2 DG |
1772 | ua_sess->tracing_id = usess->id; |
1773 | ua_sess->id = get_next_session_id(); | |
1774 | ua_sess->uid = app->uid; | |
1775 | ua_sess->gid = app->gid; | |
1776 | ua_sess->euid = usess->uid; | |
1777 | ua_sess->egid = usess->gid; | |
1778 | ua_sess->buffer_type = usess->buffer_type; | |
1779 | ua_sess->bits_per_long = app->bits_per_long; | |
95a90d01 | 1780 | |
7972aab2 | 1781 | /* There is only one consumer object per session possible. */ |
95a90d01 | 1782 | consumer_output_get(usess->consumer); |
7972aab2 | 1783 | ua_sess->consumer = usess->consumer; |
95a90d01 | 1784 | |
2bba9e53 | 1785 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 1786 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
1787 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
1788 | &usess->metadata_attr); | |
7972aab2 DG |
1789 | |
1790 | switch (ua_sess->buffer_type) { | |
1791 | case LTTNG_BUFFER_PER_PID: | |
1792 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 1793 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
1794 | datetime); |
1795 | break; | |
1796 | case LTTNG_BUFFER_PER_UID: | |
1797 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
1798 | DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long); | |
1799 | break; | |
1800 | default: | |
1801 | assert(0); | |
1802 | goto error; | |
1803 | } | |
477d7741 MD |
1804 | if (ret < 0) { |
1805 | PERROR("asprintf UST shadow copy session"); | |
477d7741 | 1806 | assert(0); |
7972aab2 | 1807 | goto error; |
477d7741 MD |
1808 | } |
1809 | ||
3d071855 MD |
1810 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
1811 | sizeof(ua_sess->root_shm_path)); | |
1812 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
1813 | strncpy(ua_sess->shm_path, usess->shm_path, |
1814 | sizeof(ua_sess->shm_path)); | |
1815 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
1816 | if (ua_sess->shm_path[0]) { | |
1817 | switch (ua_sess->buffer_type) { | |
1818 | case LTTNG_BUFFER_PER_PID: | |
1819 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
1820 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", | |
1821 | app->name, app->pid, datetime); | |
1822 | break; | |
1823 | case LTTNG_BUFFER_PER_UID: | |
1824 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
1825 | DEFAULT_UST_TRACE_UID_PATH, | |
1826 | app->uid, app->bits_per_long); | |
1827 | break; | |
1828 | default: | |
1829 | assert(0); | |
1830 | goto error; | |
1831 | } | |
1832 | if (ret < 0) { | |
1833 | PERROR("sprintf UST shadow copy session"); | |
1834 | assert(0); | |
1835 | goto error; | |
1836 | } | |
1837 | strncat(ua_sess->shm_path, tmp_shm_path, | |
1838 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
1839 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
1840 | } | |
1841 | ||
48842b30 | 1842 | /* Iterate over all channels in global domain. */ |
bec39940 DG |
1843 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
1844 | uchan, node.node) { | |
1845 | struct lttng_ht_iter uiter; | |
ba767faf | 1846 | |
bec39940 DG |
1847 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1848 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
5b4a0ec0 | 1849 | if (ua_chan_node != NULL) { |
fc34caaa | 1850 | /* Session exist. Contiuing. */ |
5b4a0ec0 DG |
1851 | continue; |
1852 | } | |
421cb601 | 1853 | |
5b4a0ec0 DG |
1854 | DBG2("Channel %s not found on shadow session copy, creating it", |
1855 | uchan->name); | |
d0b96690 | 1856 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
5b4a0ec0 | 1857 | if (ua_chan == NULL) { |
fc34caaa | 1858 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
5b4a0ec0 | 1859 | continue; |
48842b30 | 1860 | } |
5b4a0ec0 | 1861 | shadow_copy_channel(ua_chan, uchan); |
ffe60014 DG |
1862 | /* |
1863 | * The concept of metadata channel does not exist on the tracing | |
1864 | * registry side of the session daemon so this can only be a per CPU | |
1865 | * channel and not metadata. | |
1866 | */ | |
1867 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
1868 | ||
bec39940 | 1869 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
48842b30 | 1870 | } |
95a90d01 | 1871 | return; |
7972aab2 DG |
1872 | |
1873 | error: | |
95a90d01 | 1874 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
1875 | } |
1876 | ||
78f0bacd DG |
1877 | /* |
1878 | * Lookup sesison wrapper. | |
1879 | */ | |
84cd17c6 MD |
1880 | static |
1881 | void __lookup_session_by_app(struct ltt_ust_session *usess, | |
bec39940 | 1882 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
1883 | { |
1884 | /* Get right UST app session from app */ | |
d9bf3ca4 | 1885 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
1886 | } |
1887 | ||
421cb601 DG |
1888 | /* |
1889 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 1890 | * id. |
421cb601 | 1891 | */ |
48842b30 DG |
1892 | static struct ust_app_session *lookup_session_by_app( |
1893 | struct ltt_ust_session *usess, struct ust_app *app) | |
1894 | { | |
bec39940 | 1895 | struct lttng_ht_iter iter; |
d9bf3ca4 | 1896 | struct lttng_ht_node_u64 *node; |
48842b30 | 1897 | |
84cd17c6 | 1898 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 1899 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
1900 | if (node == NULL) { |
1901 | goto error; | |
1902 | } | |
1903 | ||
1904 | return caa_container_of(node, struct ust_app_session, node); | |
1905 | ||
1906 | error: | |
1907 | return NULL; | |
1908 | } | |
1909 | ||
7972aab2 DG |
1910 | /* |
1911 | * Setup buffer registry per PID for the given session and application. If none | |
1912 | * is found, a new one is created, added to the global registry and | |
1913 | * initialized. If regp is valid, it's set with the newly created object. | |
1914 | * | |
1915 | * Return 0 on success or else a negative value. | |
1916 | */ | |
1917 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
1918 | struct ust_app *app, struct buffer_reg_pid **regp) | |
1919 | { | |
1920 | int ret = 0; | |
1921 | struct buffer_reg_pid *reg_pid; | |
1922 | ||
1923 | assert(ua_sess); | |
1924 | assert(app); | |
1925 | ||
1926 | rcu_read_lock(); | |
1927 | ||
1928 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
1929 | if (!reg_pid) { | |
1930 | /* | |
1931 | * This is the create channel path meaning that if there is NO | |
1932 | * registry available, we have to create one for this session. | |
1933 | */ | |
d7ba1388 | 1934 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 1935 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
1936 | if (ret < 0) { |
1937 | goto error; | |
1938 | } | |
7972aab2 DG |
1939 | } else { |
1940 | goto end; | |
1941 | } | |
1942 | ||
1943 | /* Initialize registry. */ | |
1944 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, | |
1945 | app->bits_per_long, app->uint8_t_alignment, | |
1946 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
1947 | app->uint64_t_alignment, app->long_alignment, |
1948 | app->byte_order, app->version.major, | |
3d071855 MD |
1949 | app->version.minor, reg_pid->root_shm_path, |
1950 | reg_pid->shm_path, | |
d7ba1388 | 1951 | ua_sess->euid, ua_sess->egid); |
7972aab2 | 1952 | if (ret < 0) { |
286c991a MD |
1953 | /* |
1954 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
1955 | * destroy the buffer registry, because it is always expected | |
1956 | * that if the buffer registry can be found, its ust registry is | |
1957 | * non-NULL. | |
1958 | */ | |
1959 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
1960 | goto error; |
1961 | } | |
1962 | ||
286c991a MD |
1963 | buffer_reg_pid_add(reg_pid); |
1964 | ||
7972aab2 DG |
1965 | DBG3("UST app buffer registry per PID created successfully"); |
1966 | ||
1967 | end: | |
1968 | if (regp) { | |
1969 | *regp = reg_pid; | |
1970 | } | |
1971 | error: | |
1972 | rcu_read_unlock(); | |
1973 | return ret; | |
1974 | } | |
1975 | ||
1976 | /* | |
1977 | * Setup buffer registry per UID for the given session and application. If none | |
1978 | * is found, a new one is created, added to the global registry and | |
1979 | * initialized. If regp is valid, it's set with the newly created object. | |
1980 | * | |
1981 | * Return 0 on success or else a negative value. | |
1982 | */ | |
1983 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 1984 | struct ust_app_session *ua_sess, |
7972aab2 DG |
1985 | struct ust_app *app, struct buffer_reg_uid **regp) |
1986 | { | |
1987 | int ret = 0; | |
1988 | struct buffer_reg_uid *reg_uid; | |
1989 | ||
1990 | assert(usess); | |
1991 | assert(app); | |
1992 | ||
1993 | rcu_read_lock(); | |
1994 | ||
1995 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
1996 | if (!reg_uid) { | |
1997 | /* | |
1998 | * This is the create channel path meaning that if there is NO | |
1999 | * registry available, we have to create one for this session. | |
2000 | */ | |
2001 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, | |
3d071855 MD |
2002 | LTTNG_DOMAIN_UST, ®_uid, |
2003 | ua_sess->root_shm_path, ua_sess->shm_path); | |
7972aab2 DG |
2004 | if (ret < 0) { |
2005 | goto error; | |
2006 | } | |
7972aab2 DG |
2007 | } else { |
2008 | goto end; | |
2009 | } | |
2010 | ||
2011 | /* Initialize registry. */ | |
af6142cf | 2012 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
7972aab2 DG |
2013 | app->bits_per_long, app->uint8_t_alignment, |
2014 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
2015 | app->uint64_t_alignment, app->long_alignment, |
2016 | app->byte_order, app->version.major, | |
3d071855 MD |
2017 | app->version.minor, reg_uid->root_shm_path, |
2018 | reg_uid->shm_path, usess->uid, usess->gid); | |
7972aab2 | 2019 | if (ret < 0) { |
286c991a MD |
2020 | /* |
2021 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
2022 | * destroy the buffer registry, because it is always expected | |
2023 | * that if the buffer registry can be found, its ust registry is | |
2024 | * non-NULL. | |
2025 | */ | |
2026 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
2027 | goto error; |
2028 | } | |
2029 | /* Add node to teardown list of the session. */ | |
2030 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
2031 | ||
286c991a | 2032 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 2033 | |
286c991a | 2034 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
2035 | end: |
2036 | if (regp) { | |
2037 | *regp = reg_uid; | |
2038 | } | |
2039 | error: | |
2040 | rcu_read_unlock(); | |
2041 | return ret; | |
2042 | } | |
2043 | ||
421cb601 | 2044 | /* |
3d8ca23b | 2045 | * Create a session on the tracer side for the given app. |
421cb601 | 2046 | * |
3d8ca23b DG |
2047 | * On success, ua_sess_ptr is populated with the session pointer or else left |
2048 | * untouched. If the session was created, is_created is set to 1. On error, | |
2049 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
2050 | * be NULL. | |
2051 | * | |
2052 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
2053 | * -ENOTCONN which is the default code if the ustctl_create_session fails. | |
421cb601 | 2054 | */ |
3d8ca23b DG |
2055 | static int create_ust_app_session(struct ltt_ust_session *usess, |
2056 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, | |
2057 | int *is_created) | |
421cb601 | 2058 | { |
3d8ca23b | 2059 | int ret, created = 0; |
421cb601 DG |
2060 | struct ust_app_session *ua_sess; |
2061 | ||
3d8ca23b DG |
2062 | assert(usess); |
2063 | assert(app); | |
2064 | assert(ua_sess_ptr); | |
2065 | ||
840cb59c | 2066 | health_code_update(); |
86acf0da | 2067 | |
421cb601 DG |
2068 | ua_sess = lookup_session_by_app(usess, app); |
2069 | if (ua_sess == NULL) { | |
d9bf3ca4 | 2070 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 2071 | app->pid, usess->id); |
d0b96690 | 2072 | ua_sess = alloc_ust_app_session(app); |
421cb601 DG |
2073 | if (ua_sess == NULL) { |
2074 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
2075 | ret = -ENOMEM; |
2076 | goto error; | |
421cb601 | 2077 | } |
477d7741 | 2078 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 2079 | created = 1; |
421cb601 DG |
2080 | } |
2081 | ||
7972aab2 DG |
2082 | switch (usess->buffer_type) { |
2083 | case LTTNG_BUFFER_PER_PID: | |
2084 | /* Init local registry. */ | |
2085 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 2086 | if (ret < 0) { |
e64207cf | 2087 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2088 | goto error; |
2089 | } | |
2090 | break; | |
2091 | case LTTNG_BUFFER_PER_UID: | |
2092 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 2093 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 2094 | if (ret < 0) { |
e64207cf | 2095 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2096 | goto error; |
2097 | } | |
2098 | break; | |
2099 | default: | |
2100 | assert(0); | |
2101 | ret = -EINVAL; | |
2102 | goto error; | |
2103 | } | |
2104 | ||
2105 | health_code_update(); | |
2106 | ||
2107 | if (ua_sess->handle == -1) { | |
39f0f159 | 2108 | pthread_mutex_lock(&app->sock_lock); |
7972aab2 | 2109 | ret = ustctl_create_session(app->sock); |
39f0f159 | 2110 | pthread_mutex_unlock(&app->sock_lock); |
7972aab2 DG |
2111 | if (ret < 0) { |
2112 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2113 | ERR("Creating session for app pid %d with ret %d", | |
ffe60014 DG |
2114 | app->pid, ret); |
2115 | } else { | |
2116 | DBG("UST app creating session failed. Application is dead"); | |
3757b385 DG |
2117 | /* |
2118 | * This is normal behavior, an application can die during the | |
2119 | * creation process. Don't report an error so the execution can | |
2120 | * continue normally. This will get flagged ENOTCONN and the | |
2121 | * caller will handle it. | |
2122 | */ | |
2123 | ret = 0; | |
ffe60014 | 2124 | } |
d0b96690 | 2125 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
2126 | if (ret != -ENOMEM) { |
2127 | /* | |
2128 | * Tracer is probably gone or got an internal error so let's | |
2129 | * behave like it will soon unregister or not usable. | |
2130 | */ | |
2131 | ret = -ENOTCONN; | |
2132 | } | |
2133 | goto error; | |
421cb601 DG |
2134 | } |
2135 | ||
7972aab2 DG |
2136 | ua_sess->handle = ret; |
2137 | ||
2138 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
2139 | lttng_ht_node_init_u64(&ua_sess->node, |
2140 | ua_sess->tracing_id); | |
2141 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
7972aab2 DG |
2142 | |
2143 | DBG2("UST app session created successfully with handle %d", ret); | |
2144 | } | |
2145 | ||
2146 | *ua_sess_ptr = ua_sess; | |
2147 | if (is_created) { | |
2148 | *is_created = created; | |
2149 | } | |
2150 | ||
2151 | /* Everything went well. */ | |
2152 | ret = 0; | |
2153 | ||
2154 | error: | |
2155 | health_code_update(); | |
2156 | return ret; | |
2157 | } | |
2158 | ||
6a6b2068 JG |
2159 | /* |
2160 | * Match function for a hash table lookup of ust_app_ctx. | |
2161 | * | |
2162 | * It matches an ust app context based on the context type and, in the case | |
2163 | * of perf counters, their name. | |
2164 | */ | |
2165 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
2166 | { | |
2167 | struct ust_app_ctx *ctx; | |
2168 | const struct lttng_ust_context *key; | |
2169 | ||
2170 | assert(node); | |
2171 | assert(_key); | |
2172 | ||
2173 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
2174 | key = _key; | |
2175 | ||
2176 | /* Context type */ | |
2177 | if (ctx->ctx.ctx != key->ctx) { | |
2178 | goto no_match; | |
2179 | } | |
2180 | ||
2181 | /* Check the name in the case of perf thread counters. */ | |
2182 | if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) { | |
2183 | if (strncmp(key->u.perf_counter.name, | |
2184 | ctx->ctx.u.perf_counter.name, | |
2185 | sizeof(key->u.perf_counter.name))) { | |
2186 | goto no_match; | |
2187 | } | |
2188 | } | |
2189 | ||
2190 | /* Match. */ | |
2191 | return 1; | |
2192 | ||
2193 | no_match: | |
2194 | return 0; | |
2195 | } | |
2196 | ||
2197 | /* | |
2198 | * Lookup for an ust app context from an lttng_ust_context. | |
2199 | * | |
be184a0f | 2200 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
2201 | * Return an ust_app_ctx object or NULL on error. |
2202 | */ | |
2203 | static | |
2204 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
2205 | struct lttng_ust_context *uctx) | |
2206 | { | |
2207 | struct lttng_ht_iter iter; | |
2208 | struct lttng_ht_node_ulong *node; | |
2209 | struct ust_app_ctx *app_ctx = NULL; | |
2210 | ||
2211 | assert(uctx); | |
2212 | assert(ht); | |
2213 | ||
2214 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
2215 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
2216 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
2217 | node = lttng_ht_iter_get_node_ulong(&iter); | |
2218 | if (!node) { | |
2219 | goto end; | |
2220 | } | |
2221 | ||
2222 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); | |
2223 | ||
2224 | end: | |
2225 | return app_ctx; | |
2226 | } | |
2227 | ||
7972aab2 DG |
2228 | /* |
2229 | * Create a context for the channel on the tracer. | |
2230 | * | |
2231 | * Called with UST app session lock held and a RCU read side lock. | |
2232 | */ | |
2233 | static | |
2234 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, | |
2235 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, | |
2236 | struct ust_app *app) | |
2237 | { | |
2238 | int ret = 0; | |
7972aab2 DG |
2239 | struct ust_app_ctx *ua_ctx; |
2240 | ||
2241 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
2242 | ||
6a6b2068 JG |
2243 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
2244 | if (ua_ctx) { | |
7972aab2 DG |
2245 | ret = -EEXIST; |
2246 | goto error; | |
2247 | } | |
2248 | ||
2249 | ua_ctx = alloc_ust_app_ctx(uctx); | |
2250 | if (ua_ctx == NULL) { | |
2251 | /* malloc failed */ | |
2252 | ret = -1; | |
2253 | goto error; | |
2254 | } | |
2255 | ||
2256 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 2257 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 2258 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
2259 | |
2260 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2261 | if (ret < 0) { | |
2262 | goto error; | |
2263 | } | |
2264 | ||
2265 | error: | |
2266 | return ret; | |
2267 | } | |
2268 | ||
2269 | /* | |
2270 | * Enable on the tracer side a ust app event for the session and channel. | |
2271 | * | |
2272 | * Called with UST app session lock held. | |
2273 | */ | |
2274 | static | |
2275 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
2276 | struct ust_app_event *ua_event, struct ust_app *app) | |
2277 | { | |
2278 | int ret; | |
2279 | ||
2280 | ret = enable_ust_event(app, ua_sess, ua_event); | |
2281 | if (ret < 0) { | |
2282 | goto error; | |
2283 | } | |
2284 | ||
2285 | ua_event->enabled = 1; | |
2286 | ||
2287 | error: | |
2288 | return ret; | |
2289 | } | |
2290 | ||
2291 | /* | |
2292 | * Disable on the tracer side a ust app event for the session and channel. | |
2293 | */ | |
2294 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
2295 | struct ust_app_event *ua_event, struct ust_app *app) | |
2296 | { | |
2297 | int ret; | |
2298 | ||
2299 | ret = disable_ust_event(app, ua_sess, ua_event); | |
2300 | if (ret < 0) { | |
2301 | goto error; | |
2302 | } | |
2303 | ||
2304 | ua_event->enabled = 0; | |
2305 | ||
2306 | error: | |
2307 | return ret; | |
2308 | } | |
2309 | ||
2310 | /* | |
2311 | * Lookup ust app channel for session and disable it on the tracer side. | |
2312 | */ | |
2313 | static | |
2314 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
2315 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
2316 | { | |
2317 | int ret; | |
2318 | ||
2319 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2320 | if (ret < 0) { | |
2321 | goto error; | |
2322 | } | |
2323 | ||
2324 | ua_chan->enabled = 0; | |
2325 | ||
2326 | error: | |
2327 | return ret; | |
2328 | } | |
2329 | ||
2330 | /* | |
2331 | * Lookup ust app channel for session and enable it on the tracer side. This | |
2332 | * MUST be called with a RCU read side lock acquired. | |
2333 | */ | |
2334 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
2335 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
2336 | { | |
2337 | int ret = 0; | |
2338 | struct lttng_ht_iter iter; | |
2339 | struct lttng_ht_node_str *ua_chan_node; | |
2340 | struct ust_app_channel *ua_chan; | |
2341 | ||
2342 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); | |
2343 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
2344 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 2345 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
2346 | uchan->name, ua_sess->tracing_id); |
2347 | goto error; | |
2348 | } | |
2349 | ||
2350 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2351 | ||
2352 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
2353 | if (ret < 0) { | |
2354 | goto error; | |
2355 | } | |
2356 | ||
2357 | error: | |
2358 | return ret; | |
2359 | } | |
2360 | ||
2361 | /* | |
2362 | * Ask the consumer to create a channel and get it if successful. | |
2363 | * | |
2364 | * Return 0 on success or else a negative value. | |
2365 | */ | |
2366 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
2367 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
2368 | int bitness, struct ust_registry_session *registry) | |
2369 | { | |
2370 | int ret; | |
2371 | unsigned int nb_fd = 0; | |
2372 | struct consumer_socket *socket; | |
2373 | ||
2374 | assert(usess); | |
2375 | assert(ua_sess); | |
2376 | assert(ua_chan); | |
2377 | assert(registry); | |
2378 | ||
2379 | rcu_read_lock(); | |
2380 | health_code_update(); | |
2381 | ||
2382 | /* Get the right consumer socket for the application. */ | |
2383 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
2384 | if (!socket) { | |
2385 | ret = -EINVAL; | |
2386 | goto error; | |
2387 | } | |
2388 | ||
2389 | health_code_update(); | |
2390 | ||
2391 | /* Need one fd for the channel. */ | |
2392 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2393 | if (ret < 0) { | |
2394 | ERR("Exhausted number of available FD upon create channel"); | |
2395 | goto error; | |
2396 | } | |
2397 | ||
2398 | /* | |
2399 | * Ask consumer to create channel. The consumer will return the number of | |
2400 | * stream we have to expect. | |
2401 | */ | |
2402 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
2403 | registry); | |
2404 | if (ret < 0) { | |
2405 | goto error_ask; | |
2406 | } | |
2407 | ||
2408 | /* | |
2409 | * Compute the number of fd needed before receiving them. It must be 2 per | |
2410 | * stream (2 being the default value here). | |
2411 | */ | |
2412 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
2413 | ||
2414 | /* Reserve the amount of file descriptor we need. */ | |
2415 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
2416 | if (ret < 0) { | |
2417 | ERR("Exhausted number of available FD upon create channel"); | |
2418 | goto error_fd_get_stream; | |
2419 | } | |
2420 | ||
2421 | health_code_update(); | |
2422 | ||
2423 | /* | |
2424 | * Now get the channel from the consumer. This call wil populate the stream | |
2425 | * list of that channel and set the ust objects. | |
2426 | */ | |
d9078d0c DG |
2427 | if (usess->consumer->enabled) { |
2428 | ret = ust_consumer_get_channel(socket, ua_chan); | |
2429 | if (ret < 0) { | |
2430 | goto error_destroy; | |
2431 | } | |
7972aab2 DG |
2432 | } |
2433 | ||
2434 | rcu_read_unlock(); | |
2435 | return 0; | |
2436 | ||
2437 | error_destroy: | |
2438 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
2439 | error_fd_get_stream: | |
2440 | /* | |
2441 | * Initiate a destroy channel on the consumer since we had an error | |
2442 | * handling it on our side. The return value is of no importance since we | |
2443 | * already have a ret value set by the previous error that we need to | |
2444 | * return. | |
2445 | */ | |
2446 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
2447 | error_ask: | |
2448 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2449 | error: | |
2450 | health_code_update(); | |
2451 | rcu_read_unlock(); | |
2452 | return ret; | |
2453 | } | |
2454 | ||
2455 | /* | |
2456 | * Duplicate the ust data object of the ust app stream and save it in the | |
2457 | * buffer registry stream. | |
2458 | * | |
2459 | * Return 0 on success or else a negative value. | |
2460 | */ | |
2461 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
2462 | struct ust_app_stream *stream) | |
2463 | { | |
2464 | int ret; | |
2465 | ||
2466 | assert(reg_stream); | |
2467 | assert(stream); | |
2468 | ||
2469 | /* Reserve the amount of file descriptor we need. */ | |
2470 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
2471 | if (ret < 0) { | |
2472 | ERR("Exhausted number of available FD upon duplicate stream"); | |
2473 | goto error; | |
2474 | } | |
2475 | ||
2476 | /* Duplicate object for stream once the original is in the registry. */ | |
2477 | ret = ustctl_duplicate_ust_object_data(&stream->obj, | |
2478 | reg_stream->obj.ust); | |
2479 | if (ret < 0) { | |
2480 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
2481 | reg_stream->obj.ust, stream->obj, ret); | |
2482 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
2483 | goto error; | |
2484 | } | |
2485 | stream->handle = stream->obj->handle; | |
2486 | ||
2487 | error: | |
2488 | return ret; | |
2489 | } | |
2490 | ||
2491 | /* | |
2492 | * Duplicate the ust data object of the ust app. channel and save it in the | |
2493 | * buffer registry channel. | |
2494 | * | |
2495 | * Return 0 on success or else a negative value. | |
2496 | */ | |
2497 | static int duplicate_channel_object(struct buffer_reg_channel *reg_chan, | |
2498 | struct ust_app_channel *ua_chan) | |
2499 | { | |
2500 | int ret; | |
2501 | ||
2502 | assert(reg_chan); | |
2503 | assert(ua_chan); | |
2504 | ||
2505 | /* Need two fds for the channel. */ | |
2506 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2507 | if (ret < 0) { | |
2508 | ERR("Exhausted number of available FD upon duplicate channel"); | |
2509 | goto error_fd_get; | |
2510 | } | |
2511 | ||
2512 | /* Duplicate object for stream once the original is in the registry. */ | |
2513 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust); | |
2514 | if (ret < 0) { | |
2515 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
2516 | reg_chan->obj.ust, ua_chan->obj, ret); | |
2517 | goto error; | |
2518 | } | |
2519 | ua_chan->handle = ua_chan->obj->handle; | |
2520 | ||
2521 | return 0; | |
2522 | ||
2523 | error: | |
2524 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2525 | error_fd_get: | |
2526 | return ret; | |
2527 | } | |
2528 | ||
2529 | /* | |
2530 | * For a given channel buffer registry, setup all streams of the given ust | |
2531 | * application channel. | |
2532 | * | |
2533 | * Return 0 on success or else a negative value. | |
2534 | */ | |
2535 | static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan, | |
39f0f159 MD |
2536 | struct ust_app_channel *ua_chan, |
2537 | struct ust_app *app) | |
7972aab2 DG |
2538 | { |
2539 | int ret = 0; | |
2540 | struct ust_app_stream *stream, *stmp; | |
2541 | ||
2542 | assert(reg_chan); | |
2543 | assert(ua_chan); | |
2544 | ||
2545 | DBG2("UST app setup buffer registry stream"); | |
2546 | ||
2547 | /* Send all streams to application. */ | |
2548 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
2549 | struct buffer_reg_stream *reg_stream; | |
2550 | ||
2551 | ret = buffer_reg_stream_create(®_stream); | |
2552 | if (ret < 0) { | |
2553 | goto error; | |
2554 | } | |
2555 | ||
2556 | /* | |
2557 | * Keep original pointer and nullify it in the stream so the delete | |
2558 | * stream call does not release the object. | |
2559 | */ | |
2560 | reg_stream->obj.ust = stream->obj; | |
2561 | stream->obj = NULL; | |
2562 | buffer_reg_stream_add(reg_stream, reg_chan); | |
421cb601 | 2563 | |
7972aab2 DG |
2564 | /* We don't need the streams anymore. */ |
2565 | cds_list_del(&stream->list); | |
39f0f159 | 2566 | delete_ust_app_stream(-1, stream, app); |
7972aab2 | 2567 | } |
421cb601 | 2568 | |
7972aab2 DG |
2569 | error: |
2570 | return ret; | |
2571 | } | |
2572 | ||
2573 | /* | |
2574 | * Create a buffer registry channel for the given session registry and | |
2575 | * application channel object. If regp pointer is valid, it's set with the | |
2576 | * created object. Important, the created object is NOT added to the session | |
2577 | * registry hash table. | |
2578 | * | |
2579 | * Return 0 on success else a negative value. | |
2580 | */ | |
2581 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
2582 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
2583 | { | |
2584 | int ret; | |
2585 | struct buffer_reg_channel *reg_chan = NULL; | |
2586 | ||
2587 | assert(reg_sess); | |
2588 | assert(ua_chan); | |
2589 | ||
2590 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
2591 | ||
2592 | /* Create buffer registry channel. */ | |
2593 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, ®_chan); | |
2594 | if (ret < 0) { | |
2595 | goto error_create; | |
421cb601 | 2596 | } |
7972aab2 DG |
2597 | assert(reg_chan); |
2598 | reg_chan->consumer_key = ua_chan->key; | |
8c924c7b | 2599 | reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
d07ceecd | 2600 | reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
421cb601 | 2601 | |
7972aab2 DG |
2602 | /* Create and add a channel registry to session. */ |
2603 | ret = ust_registry_channel_add(reg_sess->reg.ust, | |
2604 | ua_chan->tracing_channel_id); | |
2605 | if (ret < 0) { | |
2606 | goto error; | |
d88aee68 | 2607 | } |
7972aab2 | 2608 | buffer_reg_channel_add(reg_sess, reg_chan); |
d88aee68 | 2609 | |
7972aab2 DG |
2610 | if (regp) { |
2611 | *regp = reg_chan; | |
3d8ca23b | 2612 | } |
d88aee68 | 2613 | |
7972aab2 | 2614 | return 0; |
3d8ca23b DG |
2615 | |
2616 | error: | |
7972aab2 DG |
2617 | /* Safe because the registry channel object was not added to any HT. */ |
2618 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
2619 | error_create: | |
3d8ca23b | 2620 | return ret; |
421cb601 DG |
2621 | } |
2622 | ||
55cc08a6 | 2623 | /* |
7972aab2 DG |
2624 | * Setup buffer registry channel for the given session registry and application |
2625 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 2626 | * |
7972aab2 | 2627 | * Return 0 on success else a negative value. |
55cc08a6 | 2628 | */ |
7972aab2 | 2629 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
39f0f159 MD |
2630 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan, |
2631 | struct ust_app *app) | |
55cc08a6 | 2632 | { |
7972aab2 | 2633 | int ret; |
55cc08a6 | 2634 | |
7972aab2 DG |
2635 | assert(reg_sess); |
2636 | assert(reg_chan); | |
2637 | assert(ua_chan); | |
2638 | assert(ua_chan->obj); | |
55cc08a6 | 2639 | |
7972aab2 | 2640 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 2641 | |
7972aab2 | 2642 | /* Setup all streams for the registry. */ |
39f0f159 | 2643 | ret = setup_buffer_reg_streams(reg_chan, ua_chan, app); |
7972aab2 | 2644 | if (ret < 0) { |
55cc08a6 DG |
2645 | goto error; |
2646 | } | |
2647 | ||
7972aab2 DG |
2648 | reg_chan->obj.ust = ua_chan->obj; |
2649 | ua_chan->obj = NULL; | |
55cc08a6 | 2650 | |
7972aab2 | 2651 | return 0; |
55cc08a6 DG |
2652 | |
2653 | error: | |
7972aab2 DG |
2654 | buffer_reg_channel_remove(reg_sess, reg_chan); |
2655 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
2656 | return ret; |
2657 | } | |
2658 | ||
edb67388 | 2659 | /* |
7972aab2 | 2660 | * Send buffer registry channel to the application. |
d0b96690 | 2661 | * |
7972aab2 | 2662 | * Return 0 on success else a negative value. |
edb67388 | 2663 | */ |
7972aab2 DG |
2664 | static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan, |
2665 | struct ust_app *app, struct ust_app_session *ua_sess, | |
2666 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
2667 | { |
2668 | int ret; | |
7972aab2 | 2669 | struct buffer_reg_stream *reg_stream; |
edb67388 | 2670 | |
7972aab2 DG |
2671 | assert(reg_chan); |
2672 | assert(app); | |
2673 | assert(ua_sess); | |
2674 | assert(ua_chan); | |
2675 | ||
2676 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
2677 | ||
2678 | ret = duplicate_channel_object(reg_chan, ua_chan); | |
edb67388 DG |
2679 | if (ret < 0) { |
2680 | goto error; | |
2681 | } | |
2682 | ||
7972aab2 DG |
2683 | /* Send channel to the application. */ |
2684 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
320b3a68 MD |
2685 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2686 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2687 | goto error; | |
2688 | } else if (ret < 0) { | |
7972aab2 DG |
2689 | goto error; |
2690 | } | |
2691 | ||
2692 | health_code_update(); | |
2693 | ||
2694 | /* Send all streams to application. */ | |
2695 | pthread_mutex_lock(®_chan->stream_list_lock); | |
2696 | cds_list_for_each_entry(reg_stream, ®_chan->streams, lnode) { | |
2697 | struct ust_app_stream stream; | |
2698 | ||
2699 | ret = duplicate_stream_object(reg_stream, &stream); | |
2700 | if (ret < 0) { | |
2701 | goto error_stream_unlock; | |
2702 | } | |
2703 | ||
2704 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
2705 | if (ret < 0) { | |
39f0f159 | 2706 | (void) release_ust_app_stream(-1, &stream, app); |
320b3a68 MD |
2707 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2708 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2709 | goto error_stream_unlock; | |
2710 | } else if (ret < 0) { | |
2711 | goto error_stream_unlock; | |
2712 | } | |
7972aab2 DG |
2713 | goto error_stream_unlock; |
2714 | } | |
edb67388 | 2715 | |
7972aab2 DG |
2716 | /* |
2717 | * The return value is not important here. This function will output an | |
2718 | * error if needed. | |
2719 | */ | |
39f0f159 | 2720 | (void) release_ust_app_stream(-1, &stream, app); |
7972aab2 DG |
2721 | } |
2722 | ua_chan->is_sent = 1; | |
2723 | ||
2724 | error_stream_unlock: | |
2725 | pthread_mutex_unlock(®_chan->stream_list_lock); | |
edb67388 DG |
2726 | error: |
2727 | return ret; | |
2728 | } | |
2729 | ||
9730260e | 2730 | /* |
7972aab2 DG |
2731 | * Create and send to the application the created buffers with per UID buffers. |
2732 | * | |
2733 | * Return 0 on success else a negative value. | |
9730260e | 2734 | */ |
7972aab2 DG |
2735 | static int create_channel_per_uid(struct ust_app *app, |
2736 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2737 | struct ust_app_channel *ua_chan) | |
9730260e DG |
2738 | { |
2739 | int ret; | |
7972aab2 DG |
2740 | struct buffer_reg_uid *reg_uid; |
2741 | struct buffer_reg_channel *reg_chan; | |
9730260e | 2742 | |
7972aab2 DG |
2743 | assert(app); |
2744 | assert(usess); | |
2745 | assert(ua_sess); | |
2746 | assert(ua_chan); | |
2747 | ||
2748 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
2749 | ||
2750 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
2751 | /* | |
2752 | * The session creation handles the creation of this global registry | |
2753 | * object. If none can be find, there is a code flow problem or a | |
2754 | * teardown race. | |
2755 | */ | |
2756 | assert(reg_uid); | |
2757 | ||
2758 | reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, | |
2759 | reg_uid); | |
2760 | if (!reg_chan) { | |
2761 | /* Create the buffer registry channel object. */ | |
2762 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, ®_chan); | |
2763 | if (ret < 0) { | |
f14256d6 MD |
2764 | ERR("Error creating the UST channel \"%s\" registry instance", |
2765 | ua_chan->name); | |
7972aab2 DG |
2766 | goto error; |
2767 | } | |
2768 | assert(reg_chan); | |
2769 | ||
2770 | /* | |
2771 | * Create the buffers on the consumer side. This call populates the | |
2772 | * ust app channel object with all streams and data object. | |
2773 | */ | |
2774 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
2775 | app->bits_per_long, reg_uid->registry->reg.ust); | |
2776 | if (ret < 0) { | |
f14256d6 MD |
2777 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
2778 | ua_chan->name); | |
2779 | ||
07d2ae95 DG |
2780 | /* |
2781 | * Let's remove the previously created buffer registry channel so | |
2782 | * it's not visible anymore in the session registry. | |
2783 | */ | |
2784 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, | |
2785 | ua_chan->tracing_channel_id); | |
2786 | buffer_reg_channel_remove(reg_uid->registry, reg_chan); | |
2787 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
7972aab2 DG |
2788 | goto error; |
2789 | } | |
2790 | ||
2791 | /* | |
2792 | * Setup the streams and add it to the session registry. | |
2793 | */ | |
39f0f159 MD |
2794 | ret = setup_buffer_reg_channel(reg_uid->registry, |
2795 | ua_chan, reg_chan, app); | |
7972aab2 | 2796 | if (ret < 0) { |
f14256d6 MD |
2797 | ERR("Error setting up UST channel \"%s\"", |
2798 | ua_chan->name); | |
7972aab2 DG |
2799 | goto error; |
2800 | } | |
2801 | ||
2802 | } | |
2803 | ||
2804 | /* Send buffers to the application. */ | |
2805 | ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan); | |
9730260e | 2806 | if (ret < 0) { |
320b3a68 MD |
2807 | if (ret != -ENOTCONN) { |
2808 | ERR("Error sending channel to application"); | |
2809 | } | |
9730260e DG |
2810 | goto error; |
2811 | } | |
2812 | ||
9730260e DG |
2813 | error: |
2814 | return ret; | |
2815 | } | |
2816 | ||
78f0bacd | 2817 | /* |
7972aab2 DG |
2818 | * Create and send to the application the created buffers with per PID buffers. |
2819 | * | |
2820 | * Return 0 on success else a negative value. | |
78f0bacd | 2821 | */ |
7972aab2 DG |
2822 | static int create_channel_per_pid(struct ust_app *app, |
2823 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2824 | struct ust_app_channel *ua_chan) | |
78f0bacd | 2825 | { |
8535a6d9 | 2826 | int ret; |
7972aab2 | 2827 | struct ust_registry_session *registry; |
78f0bacd | 2828 | |
7972aab2 DG |
2829 | assert(app); |
2830 | assert(usess); | |
2831 | assert(ua_sess); | |
2832 | assert(ua_chan); | |
2833 | ||
2834 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
2835 | ||
2836 | rcu_read_lock(); | |
2837 | ||
2838 | registry = get_session_registry(ua_sess); | |
2839 | assert(registry); | |
2840 | ||
2841 | /* Create and add a new channel registry to session. */ | |
2842 | ret = ust_registry_channel_add(registry, ua_chan->key); | |
78f0bacd | 2843 | if (ret < 0) { |
f14256d6 MD |
2844 | ERR("Error creating the UST channel \"%s\" registry instance", |
2845 | ua_chan->name); | |
78f0bacd DG |
2846 | goto error; |
2847 | } | |
2848 | ||
7972aab2 DG |
2849 | /* Create and get channel on the consumer side. */ |
2850 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
2851 | app->bits_per_long, registry); | |
2852 | if (ret < 0) { | |
f14256d6 MD |
2853 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
2854 | ua_chan->name); | |
7972aab2 DG |
2855 | goto error; |
2856 | } | |
2857 | ||
2858 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
2859 | if (ret < 0) { | |
320b3a68 MD |
2860 | if (ret != -ENOTCONN) { |
2861 | ERR("Error sending channel to application"); | |
2862 | } | |
7972aab2 DG |
2863 | goto error; |
2864 | } | |
8535a6d9 | 2865 | |
78f0bacd | 2866 | error: |
7972aab2 | 2867 | rcu_read_unlock(); |
78f0bacd DG |
2868 | return ret; |
2869 | } | |
2870 | ||
2871 | /* | |
7972aab2 DG |
2872 | * From an already allocated ust app channel, create the channel buffers if |
2873 | * need and send it to the application. This MUST be called with a RCU read | |
2874 | * side lock acquired. | |
2875 | * | |
320b3a68 MD |
2876 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
2877 | * the application exited concurrently. | |
78f0bacd | 2878 | */ |
7972aab2 DG |
2879 | static int do_create_channel(struct ust_app *app, |
2880 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2881 | struct ust_app_channel *ua_chan) | |
78f0bacd | 2882 | { |
7972aab2 | 2883 | int ret; |
78f0bacd | 2884 | |
7972aab2 DG |
2885 | assert(app); |
2886 | assert(usess); | |
2887 | assert(ua_sess); | |
2888 | assert(ua_chan); | |
2889 | ||
2890 | /* Handle buffer type before sending the channel to the application. */ | |
2891 | switch (usess->buffer_type) { | |
2892 | case LTTNG_BUFFER_PER_UID: | |
2893 | { | |
2894 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
2895 | if (ret < 0) { | |
2896 | goto error; | |
2897 | } | |
2898 | break; | |
2899 | } | |
2900 | case LTTNG_BUFFER_PER_PID: | |
2901 | { | |
2902 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
2903 | if (ret < 0) { | |
2904 | goto error; | |
2905 | } | |
2906 | break; | |
2907 | } | |
2908 | default: | |
2909 | assert(0); | |
2910 | ret = -EINVAL; | |
78f0bacd DG |
2911 | goto error; |
2912 | } | |
2913 | ||
7972aab2 DG |
2914 | /* Initialize ust objd object using the received handle and add it. */ |
2915 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
2916 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 2917 | |
7972aab2 DG |
2918 | /* If channel is not enabled, disable it on the tracer */ |
2919 | if (!ua_chan->enabled) { | |
2920 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2921 | if (ret < 0) { | |
2922 | goto error; | |
2923 | } | |
78f0bacd DG |
2924 | } |
2925 | ||
2926 | error: | |
2927 | return ret; | |
2928 | } | |
2929 | ||
284d8f55 | 2930 | /* |
4d710ac2 DG |
2931 | * Create UST app channel and create it on the tracer. Set ua_chanp of the |
2932 | * newly created channel if not NULL. | |
d0b96690 | 2933 | * |
36b588ed | 2934 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 2935 | * |
320b3a68 MD |
2936 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
2937 | * the application exited concurrently. | |
284d8f55 | 2938 | */ |
4d710ac2 DG |
2939 | static int create_ust_app_channel(struct ust_app_session *ua_sess, |
2940 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
7972aab2 | 2941 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
4d710ac2 | 2942 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
2943 | { |
2944 | int ret = 0; | |
bec39940 DG |
2945 | struct lttng_ht_iter iter; |
2946 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
2947 | struct ust_app_channel *ua_chan; |
2948 | ||
2949 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2950 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2951 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 2952 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 2953 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 2954 | goto end; |
5b4a0ec0 DG |
2955 | } |
2956 | ||
d0b96690 | 2957 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
2958 | if (ua_chan == NULL) { |
2959 | /* Only malloc can fail here */ | |
4d710ac2 | 2960 | ret = -ENOMEM; |
094d1690 | 2961 | goto error_alloc; |
fc34caaa DG |
2962 | } |
2963 | shadow_copy_channel(ua_chan, uchan); | |
2964 | ||
ffe60014 DG |
2965 | /* Set channel type. */ |
2966 | ua_chan->attr.type = type; | |
2967 | ||
7972aab2 | 2968 | ret = do_create_channel(app, usess, ua_sess, ua_chan); |
5b4a0ec0 DG |
2969 | if (ret < 0) { |
2970 | goto error; | |
2971 | } | |
2972 | ||
fc34caaa | 2973 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
852d0037 | 2974 | app->pid); |
fc34caaa | 2975 | |
d0b96690 DG |
2976 | /* Only add the channel if successful on the tracer side. */ |
2977 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
2978 | ||
fc34caaa | 2979 | end: |
4d710ac2 DG |
2980 | if (ua_chanp) { |
2981 | *ua_chanp = ua_chan; | |
2982 | } | |
2983 | ||
2984 | /* Everything went well. */ | |
2985 | return 0; | |
5b4a0ec0 DG |
2986 | |
2987 | error: | |
d0b96690 | 2988 | delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app); |
094d1690 | 2989 | error_alloc: |
4d710ac2 | 2990 | return ret; |
5b4a0ec0 DG |
2991 | } |
2992 | ||
2993 | /* | |
2994 | * Create UST app event and create it on the tracer side. | |
d0b96690 DG |
2995 | * |
2996 | * Called with ust app session mutex held. | |
5b4a0ec0 | 2997 | */ |
edb67388 DG |
2998 | static |
2999 | int create_ust_app_event(struct ust_app_session *ua_sess, | |
3000 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, | |
3001 | struct ust_app *app) | |
284d8f55 | 3002 | { |
edb67388 | 3003 | int ret = 0; |
5b4a0ec0 | 3004 | struct ust_app_event *ua_event; |
284d8f55 | 3005 | |
5b4a0ec0 | 3006 | /* Get event node */ |
18eace3b | 3007 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
39c5a3a7 | 3008 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 3009 | if (ua_event != NULL) { |
fc34caaa | 3010 | ret = -EEXIST; |
edb67388 DG |
3011 | goto end; |
3012 | } | |
5b4a0ec0 | 3013 | |
edb67388 DG |
3014 | /* Does not exist so create one */ |
3015 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
3016 | if (ua_event == NULL) { | |
3017 | /* Only malloc can failed so something is really wrong */ | |
3018 | ret = -ENOMEM; | |
fc34caaa | 3019 | goto end; |
5b4a0ec0 | 3020 | } |
edb67388 | 3021 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 3022 | |
edb67388 | 3023 | /* Create it on the tracer side */ |
5b4a0ec0 | 3024 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 3025 | if (ret < 0) { |
fc34caaa | 3026 | /* Not found previously means that it does not exist on the tracer */ |
76f66f63 | 3027 | assert(ret != -LTTNG_UST_ERR_EXIST); |
284d8f55 DG |
3028 | goto error; |
3029 | } | |
3030 | ||
d0b96690 | 3031 | add_unique_ust_app_event(ua_chan, ua_event); |
284d8f55 | 3032 | |
fc34caaa | 3033 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
852d0037 | 3034 | app->pid); |
7f79d3a1 | 3035 | |
edb67388 | 3036 | end: |
fc34caaa DG |
3037 | return ret; |
3038 | ||
5b4a0ec0 | 3039 | error: |
fc34caaa | 3040 | /* Valid. Calling here is already in a read side lock */ |
39f0f159 | 3041 | delete_ust_app_event(-1, ua_event, app); |
edb67388 | 3042 | return ret; |
5b4a0ec0 DG |
3043 | } |
3044 | ||
3045 | /* | |
3046 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 3047 | * |
7972aab2 | 3048 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
3049 | */ |
3050 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 3051 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
3052 | { |
3053 | int ret = 0; | |
ffe60014 | 3054 | struct ust_app_channel *metadata; |
d88aee68 | 3055 | struct consumer_socket *socket; |
7972aab2 | 3056 | struct ust_registry_session *registry; |
5b4a0ec0 | 3057 | |
ffe60014 DG |
3058 | assert(ua_sess); |
3059 | assert(app); | |
d88aee68 | 3060 | assert(consumer); |
5b4a0ec0 | 3061 | |
7972aab2 DG |
3062 | registry = get_session_registry(ua_sess); |
3063 | assert(registry); | |
3064 | ||
ce34fcd0 MD |
3065 | pthread_mutex_lock(®istry->lock); |
3066 | ||
1b532a60 DG |
3067 | /* Metadata already exists for this registry or it was closed previously */ |
3068 | if (registry->metadata_key || registry->metadata_closed) { | |
7972aab2 DG |
3069 | ret = 0; |
3070 | goto error; | |
5b4a0ec0 DG |
3071 | } |
3072 | ||
ffe60014 | 3073 | /* Allocate UST metadata */ |
d0b96690 | 3074 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
3075 | if (!metadata) { |
3076 | /* malloc() failed */ | |
3077 | ret = -ENOMEM; | |
3078 | goto error; | |
3079 | } | |
5b4a0ec0 | 3080 | |
ad7a9107 | 3081 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 3082 | |
7972aab2 DG |
3083 | /* Need one fd for the channel. */ |
3084 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
3085 | if (ret < 0) { | |
3086 | ERR("Exhausted number of available FD upon create metadata"); | |
3087 | goto error; | |
3088 | } | |
3089 | ||
4dc3dfc5 DG |
3090 | /* Get the right consumer socket for the application. */ |
3091 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); | |
3092 | if (!socket) { | |
3093 | ret = -EINVAL; | |
3094 | goto error_consumer; | |
3095 | } | |
3096 | ||
331744e3 JD |
3097 | /* |
3098 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
3099 | * to the registry *before* we ask the consumer so we avoid the race of the | |
3100 | * consumer requesting the metadata and the ask_channel call on our side | |
3101 | * did not returned yet. | |
3102 | */ | |
3103 | registry->metadata_key = metadata->key; | |
3104 | ||
d88aee68 DG |
3105 | /* |
3106 | * Ask the metadata channel creation to the consumer. The metadata object | |
3107 | * will be created by the consumer and kept their. However, the stream is | |
3108 | * never added or monitored until we do a first push metadata to the | |
3109 | * consumer. | |
3110 | */ | |
7972aab2 DG |
3111 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
3112 | registry); | |
d88aee68 | 3113 | if (ret < 0) { |
f2a444f1 DG |
3114 | /* Nullify the metadata key so we don't try to close it later on. */ |
3115 | registry->metadata_key = 0; | |
d88aee68 DG |
3116 | goto error_consumer; |
3117 | } | |
3118 | ||
3119 | /* | |
3120 | * The setup command will make the metadata stream be sent to the relayd, | |
3121 | * if applicable, and the thread managing the metadatas. This is important | |
3122 | * because after this point, if an error occurs, the only way the stream | |
3123 | * can be deleted is to be monitored in the consumer. | |
3124 | */ | |
7972aab2 | 3125 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 3126 | if (ret < 0) { |
f2a444f1 DG |
3127 | /* Nullify the metadata key so we don't try to close it later on. */ |
3128 | registry->metadata_key = 0; | |
d88aee68 | 3129 | goto error_consumer; |
5b4a0ec0 DG |
3130 | } |
3131 | ||
7972aab2 DG |
3132 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
3133 | metadata->key, app->pid); | |
5b4a0ec0 | 3134 | |
d88aee68 | 3135 | error_consumer: |
b80f0b6c | 3136 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 3137 | delete_ust_app_channel(-1, metadata, app); |
5b4a0ec0 | 3138 | error: |
ce34fcd0 | 3139 | pthread_mutex_unlock(®istry->lock); |
ffe60014 | 3140 | return ret; |
5b4a0ec0 DG |
3141 | } |
3142 | ||
5b4a0ec0 | 3143 | /* |
d88aee68 DG |
3144 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
3145 | * acquired before calling this function. | |
5b4a0ec0 DG |
3146 | */ |
3147 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
3148 | { | |
d88aee68 | 3149 | struct ust_app *app = NULL; |
bec39940 DG |
3150 | struct lttng_ht_node_ulong *node; |
3151 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 3152 | |
bec39940 DG |
3153 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
3154 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
3155 | if (node == NULL) { |
3156 | DBG2("UST app no found with pid %d", pid); | |
3157 | goto error; | |
3158 | } | |
5b4a0ec0 DG |
3159 | |
3160 | DBG2("Found UST app by pid %d", pid); | |
3161 | ||
d88aee68 | 3162 | app = caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
3163 | |
3164 | error: | |
d88aee68 | 3165 | return app; |
5b4a0ec0 DG |
3166 | } |
3167 | ||
d88aee68 DG |
3168 | /* |
3169 | * Allocate and init an UST app object using the registration information and | |
3170 | * the command socket. This is called when the command socket connects to the | |
3171 | * session daemon. | |
3172 | * | |
3173 | * The object is returned on success or else NULL. | |
3174 | */ | |
d0b96690 | 3175 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 3176 | { |
d0b96690 DG |
3177 | struct ust_app *lta = NULL; |
3178 | ||
3179 | assert(msg); | |
3180 | assert(sock >= 0); | |
3181 | ||
3182 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 3183 | |
173af62f DG |
3184 | if ((msg->bits_per_long == 64 && |
3185 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
3186 | || (msg->bits_per_long == 32 && | |
3187 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 3188 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
3189 | "%d-bit long, but no consumerd for this size is available.\n", |
3190 | msg->name, msg->pid, msg->bits_per_long); | |
3191 | goto error; | |
3f2c5fcc | 3192 | } |
d0b96690 | 3193 | |
5b4a0ec0 DG |
3194 | lta = zmalloc(sizeof(struct ust_app)); |
3195 | if (lta == NULL) { | |
3196 | PERROR("malloc"); | |
d0b96690 | 3197 | goto error; |
5b4a0ec0 DG |
3198 | } |
3199 | ||
3200 | lta->ppid = msg->ppid; | |
3201 | lta->uid = msg->uid; | |
3202 | lta->gid = msg->gid; | |
d0b96690 | 3203 | |
7753dea8 | 3204 | lta->bits_per_long = msg->bits_per_long; |
d0b96690 DG |
3205 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
3206 | lta->uint16_t_alignment = msg->uint16_t_alignment; | |
3207 | lta->uint32_t_alignment = msg->uint32_t_alignment; | |
3208 | lta->uint64_t_alignment = msg->uint64_t_alignment; | |
3209 | lta->long_alignment = msg->long_alignment; | |
3210 | lta->byte_order = msg->byte_order; | |
3211 | ||
5b4a0ec0 DG |
3212 | lta->v_major = msg->major; |
3213 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 3214 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d0b96690 DG |
3215 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
3216 | lta->notify_sock = -1; | |
d88aee68 DG |
3217 | |
3218 | /* Copy name and make sure it's NULL terminated. */ | |
3219 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
3220 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
3221 | ||
3222 | /* | |
3223 | * Before this can be called, when receiving the registration information, | |
3224 | * the application compatibility is checked. So, at this point, the | |
3225 | * application can work with this session daemon. | |
3226 | */ | |
d0b96690 | 3227 | lta->compatible = 1; |
5b4a0ec0 | 3228 | |
852d0037 | 3229 | lta->pid = msg->pid; |
d0b96690 | 3230 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 3231 | lta->sock = sock; |
39f0f159 | 3232 | pthread_mutex_init(<a->sock_lock, NULL); |
d0b96690 | 3233 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 3234 | |
d42f20df | 3235 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
d0b96690 DG |
3236 | error: |
3237 | return lta; | |
3238 | } | |
3239 | ||
d88aee68 DG |
3240 | /* |
3241 | * For a given application object, add it to every hash table. | |
3242 | */ | |
d0b96690 DG |
3243 | void ust_app_add(struct ust_app *app) |
3244 | { | |
3245 | assert(app); | |
3246 | assert(app->notify_sock >= 0); | |
3247 | ||
5b4a0ec0 | 3248 | rcu_read_lock(); |
852d0037 DG |
3249 | |
3250 | /* | |
3251 | * On a re-registration, we want to kick out the previous registration of | |
3252 | * that pid | |
3253 | */ | |
d0b96690 | 3254 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
3255 | |
3256 | /* | |
3257 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
3258 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
3259 | * already in the table. | |
3260 | */ | |
d0b96690 | 3261 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 3262 | |
d0b96690 DG |
3263 | /* Add application to the notify socket hash table. */ |
3264 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
3265 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 3266 | |
d0b96690 | 3267 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
d88aee68 DG |
3268 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
3269 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, | |
3270 | app->v_minor); | |
5b4a0ec0 | 3271 | |
d0b96690 DG |
3272 | rcu_read_unlock(); |
3273 | } | |
3274 | ||
d88aee68 DG |
3275 | /* |
3276 | * Set the application version into the object. | |
3277 | * | |
3278 | * Return 0 on success else a negative value either an errno code or a | |
3279 | * LTTng-UST error code. | |
3280 | */ | |
d0b96690 DG |
3281 | int ust_app_version(struct ust_app *app) |
3282 | { | |
d88aee68 DG |
3283 | int ret; |
3284 | ||
d0b96690 | 3285 | assert(app); |
d88aee68 | 3286 | |
39f0f159 | 3287 | pthread_mutex_lock(&app->sock_lock); |
d88aee68 | 3288 | ret = ustctl_tracer_version(app->sock, &app->version); |
39f0f159 | 3289 | pthread_mutex_unlock(&app->sock_lock); |
d88aee68 DG |
3290 | if (ret < 0) { |
3291 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
5368d366 | 3292 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
d88aee68 | 3293 | } else { |
5368d366 | 3294 | DBG3("UST app %d version failed. Application is dead", app->sock); |
d88aee68 DG |
3295 | } |
3296 | } | |
3297 | ||
3298 | return ret; | |
5b4a0ec0 DG |
3299 | } |
3300 | ||
3301 | /* | |
3302 | * Unregister app by removing it from the global traceable app list and freeing | |
3303 | * the data struct. | |
3304 | * | |
3305 | * The socket is already closed at this point so no close to sock. | |
3306 | */ | |
3307 | void ust_app_unregister(int sock) | |
3308 | { | |
3309 | struct ust_app *lta; | |
bec39940 | 3310 | struct lttng_ht_node_ulong *node; |
c4b88406 | 3311 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 3312 | struct lttng_ht_iter iter; |
d42f20df | 3313 | struct ust_app_session *ua_sess; |
525b0740 | 3314 | int ret; |
5b4a0ec0 DG |
3315 | |
3316 | rcu_read_lock(); | |
886459c6 | 3317 | |
5b4a0ec0 | 3318 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
3319 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
3320 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
d0b96690 | 3321 | assert(node); |
284d8f55 | 3322 | |
852d0037 | 3323 | lta = caa_container_of(node, struct ust_app, sock_n); |
852d0037 DG |
3324 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
3325 | ||
d88aee68 | 3326 | /* |
ce34fcd0 MD |
3327 | * For per-PID buffers, perform "push metadata" and flush all |
3328 | * application streams before removing app from hash tables, | |
3329 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 3330 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 3331 | */ |
d42f20df DG |
3332 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
3333 | node.node) { | |
7972aab2 DG |
3334 | struct ust_registry_session *registry; |
3335 | ||
d42f20df DG |
3336 | ret = lttng_ht_del(lta->sessions, &iter); |
3337 | if (ret) { | |
3338 | /* The session was already removed so scheduled for teardown. */ | |
3339 | continue; | |
3340 | } | |
3341 | ||
ce34fcd0 MD |
3342 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
3343 | (void) ust_app_flush_app_session(lta, ua_sess); | |
3344 | } | |
c4b88406 | 3345 | |
d42f20df DG |
3346 | /* |
3347 | * Add session to list for teardown. This is safe since at this point we | |
3348 | * are the only one using this list. | |
3349 | */ | |
d88aee68 DG |
3350 | pthread_mutex_lock(&ua_sess->lock); |
3351 | ||
d40a0cb1 MD |
3352 | if (ua_sess->deleted) { |
3353 | pthread_mutex_unlock(&ua_sess->lock); | |
3354 | continue; | |
3355 | } | |
3356 | ||
d88aee68 DG |
3357 | /* |
3358 | * Normally, this is done in the delete session process which is | |
3359 | * executed in the call rcu below. However, upon registration we can't | |
3360 | * afford to wait for the grace period before pushing data or else the | |
3361 | * data pending feature can race between the unregistration and stop | |
3362 | * command where the data pending command is sent *before* the grace | |
3363 | * period ended. | |
3364 | * | |
3365 | * The close metadata below nullifies the metadata pointer in the | |
3366 | * session so the delete session will NOT push/close a second time. | |
3367 | */ | |
7972aab2 | 3368 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 3369 | if (registry) { |
7972aab2 DG |
3370 | /* Push metadata for application before freeing the application. */ |
3371 | (void) push_metadata(registry, ua_sess->consumer); | |
3372 | ||
3373 | /* | |
3374 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
3375 | * metadata only on destroy trace session in this case. Also, the |
3376 | * previous push metadata could have flag the metadata registry to | |
3377 | * close so don't send a close command if closed. | |
7972aab2 | 3378 | */ |
ce34fcd0 | 3379 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
3380 | /* And ask to close it for this session registry. */ |
3381 | (void) close_metadata(registry, ua_sess->consumer); | |
3382 | } | |
3383 | } | |
d42f20df | 3384 | cds_list_add(&ua_sess->teardown_node, <a->teardown_head); |
c4b88406 | 3385 | |
d88aee68 | 3386 | pthread_mutex_unlock(&ua_sess->lock); |
d42f20df DG |
3387 | } |
3388 | ||
c4b88406 MD |
3389 | /* Remove application from PID hash table */ |
3390 | ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter); | |
3391 | assert(!ret); | |
3392 | ||
3393 | /* | |
3394 | * Remove application from notify hash table. The thread handling the | |
3395 | * notify socket could have deleted the node so ignore on error because | |
3396 | * either way it's valid. The close of that socket is handled by the other | |
3397 | * thread. | |
3398 | */ | |
3399 | iter.iter.node = <a->notify_sock_n.node; | |
3400 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
3401 | ||
3402 | /* | |
3403 | * Ignore return value since the node might have been removed before by an | |
3404 | * add replace during app registration because the PID can be reassigned by | |
3405 | * the OS. | |
3406 | */ | |
3407 | iter.iter.node = <a->pid_n.node; | |
3408 | ret = lttng_ht_del(ust_app_ht, &iter); | |
3409 | if (ret) { | |
3410 | DBG3("Unregister app by PID %d failed. This can happen on pid reuse", | |
3411 | lta->pid); | |
3412 | } | |
3413 | ||
852d0037 DG |
3414 | /* Free memory */ |
3415 | call_rcu(<a->pid_n.head, delete_ust_app_rcu); | |
3416 | ||
5b4a0ec0 DG |
3417 | rcu_read_unlock(); |
3418 | return; | |
284d8f55 DG |
3419 | } |
3420 | ||
5b4a0ec0 DG |
3421 | /* |
3422 | * Fill events array with all events name of all registered apps. | |
3423 | */ | |
3424 | int ust_app_list_events(struct lttng_event **events) | |
421cb601 | 3425 | { |
5b4a0ec0 DG |
3426 | int ret, handle; |
3427 | size_t nbmem, count = 0; | |
bec39940 | 3428 | struct lttng_ht_iter iter; |
5b4a0ec0 | 3429 | struct ust_app *app; |
c617c0c6 | 3430 | struct lttng_event *tmp_event; |
421cb601 | 3431 | |
5b4a0ec0 | 3432 | nbmem = UST_APP_EVENT_LIST_SIZE; |
c617c0c6 MD |
3433 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event)); |
3434 | if (tmp_event == NULL) { | |
5b4a0ec0 DG |
3435 | PERROR("zmalloc ust app events"); |
3436 | ret = -ENOMEM; | |
421cb601 DG |
3437 | goto error; |
3438 | } | |
3439 | ||
5b4a0ec0 | 3440 | rcu_read_lock(); |
421cb601 | 3441 | |
852d0037 | 3442 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
90eaa0d2 | 3443 | struct lttng_ust_tracepoint_iter uiter; |
ac3bd9c0 | 3444 | |
840cb59c | 3445 | health_code_update(); |
86acf0da | 3446 | |
e0c7ec2b DG |
3447 | if (!app->compatible) { |
3448 | /* | |
3449 | * TODO: In time, we should notice the caller of this error by | |
3450 | * telling him that this is a version error. | |
3451 | */ | |
3452 | continue; | |
3453 | } | |
39f0f159 | 3454 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 3455 | handle = ustctl_tracepoint_list(app->sock); |
5b4a0ec0 | 3456 | if (handle < 0) { |
ffe60014 DG |
3457 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
3458 | ERR("UST app list events getting handle failed for app pid %d", | |
3459 | app->pid); | |
3460 | } | |
39f0f159 | 3461 | pthread_mutex_unlock(&app->sock_lock); |
5b4a0ec0 DG |
3462 | continue; |
3463 | } | |
421cb601 | 3464 | |
852d0037 | 3465 | while ((ret = ustctl_tracepoint_list_get(app->sock, handle, |
fb54cdbf | 3466 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
3467 | /* Handle ustctl error. */ |
3468 | if (ret < 0) { | |
39f0f159 MD |
3469 | int release_ret; |
3470 | ||
a2ba1ab0 | 3471 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
3472 | ERR("UST app tp list get failed for app %d with ret %d", |
3473 | app->sock, ret); | |
3474 | } else { | |
3475 | DBG3("UST app tp list get failed. Application is dead"); | |
3757b385 DG |
3476 | /* |
3477 | * This is normal behavior, an application can die during the | |
3478 | * creation process. Don't report an error so the execution can | |
3479 | * continue normally. Continue normal execution. | |
3480 | */ | |
3481 | break; | |
ffe60014 | 3482 | } |
98f595d4 | 3483 | free(tmp_event); |
39f0f159 | 3484 | release_ret = ustctl_release_handle(app->sock, handle); |
17a2c4ee JG |
3485 | if (release_ret < 0 && |
3486 | release_ret != -LTTNG_UST_ERR_EXITING && | |
3487 | release_ret != -EPIPE) { | |
39f0f159 MD |
3488 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
3489 | } | |
3490 | pthread_mutex_unlock(&app->sock_lock); | |
ffe60014 DG |
3491 | goto rcu_error; |
3492 | } | |
3493 | ||
840cb59c | 3494 | health_code_update(); |
815564d8 | 3495 | if (count >= nbmem) { |
d7b3776f | 3496 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
3497 | struct lttng_event *new_tmp_event; |
3498 | size_t new_nbmem; | |
3499 | ||
3500 | new_nbmem = nbmem << 1; | |
3501 | DBG2("Reallocating event list from %zu to %zu entries", | |
3502 | nbmem, new_nbmem); | |
3503 | new_tmp_event = realloc(tmp_event, | |
3504 | new_nbmem * sizeof(struct lttng_event)); | |
3505 | if (new_tmp_event == NULL) { | |
39f0f159 MD |
3506 | int release_ret; |
3507 | ||
5b4a0ec0 | 3508 | PERROR("realloc ust app events"); |
c617c0c6 | 3509 | free(tmp_event); |
5b4a0ec0 | 3510 | ret = -ENOMEM; |
39f0f159 | 3511 | release_ret = ustctl_release_handle(app->sock, handle); |
17a2c4ee JG |
3512 | if (release_ret < 0 && |
3513 | release_ret != -LTTNG_UST_ERR_EXITING && | |
3514 | release_ret != -EPIPE) { | |
39f0f159 MD |
3515 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
3516 | } | |
3517 | pthread_mutex_unlock(&app->sock_lock); | |
5b4a0ec0 DG |
3518 | goto rcu_error; |
3519 | } | |
53efb85a MD |
3520 | /* Zero the new memory */ |
3521 | memset(new_tmp_event + nbmem, 0, | |
3522 | (new_nbmem - nbmem) * sizeof(struct lttng_event)); | |
3523 | nbmem = new_nbmem; | |
3524 | tmp_event = new_tmp_event; | |
5b4a0ec0 | 3525 | } |
c617c0c6 MD |
3526 | memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN); |
3527 | tmp_event[count].loglevel = uiter.loglevel; | |
3528 | tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT; | |
3529 | tmp_event[count].pid = app->pid; | |
3530 | tmp_event[count].enabled = -1; | |
5b4a0ec0 | 3531 | count++; |
421cb601 | 3532 | } |
39f0f159 MD |
3533 | ret = ustctl_release_handle(app->sock, handle); |
3534 | pthread_mutex_unlock(&app->sock_lock); | |
17a2c4ee | 3535 | if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
39f0f159 MD |
3536 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
3537 | } | |
421cb601 DG |
3538 | } |
3539 | ||
5b4a0ec0 | 3540 | ret = count; |
c617c0c6 | 3541 | *events = tmp_event; |
421cb601 | 3542 | |
5b4a0ec0 | 3543 | DBG2("UST app list events done (%zu events)", count); |
421cb601 | 3544 | |
5b4a0ec0 DG |
3545 | rcu_error: |
3546 | rcu_read_unlock(); | |
421cb601 | 3547 | error: |
840cb59c | 3548 | health_code_update(); |
5b4a0ec0 | 3549 | return ret; |
421cb601 DG |
3550 | } |
3551 | ||
f37d259d MD |
3552 | /* |
3553 | * Fill events array with all events name of all registered apps. | |
3554 | */ | |
3555 | int ust_app_list_event_fields(struct lttng_event_field **fields) | |
3556 | { | |
3557 | int ret, handle; | |
3558 | size_t nbmem, count = 0; | |
3559 | struct lttng_ht_iter iter; | |
3560 | struct ust_app *app; | |
c617c0c6 | 3561 | struct lttng_event_field *tmp_event; |
f37d259d MD |
3562 | |
3563 | nbmem = UST_APP_EVENT_LIST_SIZE; | |
c617c0c6 MD |
3564 | tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field)); |
3565 | if (tmp_event == NULL) { | |
f37d259d MD |
3566 | PERROR("zmalloc ust app event fields"); |
3567 | ret = -ENOMEM; | |
3568 | goto error; | |
3569 | } | |
3570 | ||
3571 | rcu_read_lock(); | |
3572 | ||
3573 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
3574 | struct lttng_ust_field_iter uiter; | |
3575 | ||
840cb59c | 3576 | health_code_update(); |
86acf0da | 3577 | |
f37d259d MD |
3578 | if (!app->compatible) { |
3579 | /* | |
3580 | * TODO: In time, we should notice the caller of this error by | |
3581 | * telling him that this is a version error. | |
3582 | */ | |
3583 | continue; | |
3584 | } | |
39f0f159 | 3585 | pthread_mutex_lock(&app->sock_lock); |
f37d259d MD |
3586 | handle = ustctl_tracepoint_field_list(app->sock); |
3587 | if (handle < 0) { | |
ffe60014 DG |
3588 | if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) { |
3589 | ERR("UST app list field getting handle failed for app pid %d", | |
3590 | app->pid); | |
3591 | } | |
39f0f159 | 3592 | pthread_mutex_unlock(&app->sock_lock); |
f37d259d MD |
3593 | continue; |
3594 | } | |
3595 | ||
3596 | while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle, | |
fb54cdbf | 3597 | &uiter)) != -LTTNG_UST_ERR_NOENT) { |
ffe60014 DG |
3598 | /* Handle ustctl error. */ |
3599 | if (ret < 0) { | |
39f0f159 MD |
3600 | int release_ret; |
3601 | ||
a2ba1ab0 | 3602 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { |
ffe60014 DG |
3603 | ERR("UST app tp list field failed for app %d with ret %d", |
3604 | app->sock, ret); | |
3605 | } else { | |
3606 | DBG3("UST app tp list field failed. Application is dead"); | |
3757b385 DG |
3607 | /* |
3608 | * This is normal behavior, an application can die during the | |
3609 | * creation process. Don't report an error so the execution can | |
98f595d4 | 3610 | * continue normally. Reset list and count for next app. |
3757b385 DG |
3611 | */ |
3612 | break; | |
ffe60014 | 3613 | } |
98f595d4 | 3614 | free(tmp_event); |
39f0f159 MD |
3615 | release_ret = ustctl_release_handle(app->sock, handle); |
3616 | pthread_mutex_unlock(&app->sock_lock); | |
17a2c4ee JG |
3617 | if (release_ret < 0 && |
3618 | release_ret != -LTTNG_UST_ERR_EXITING && | |
3619 | release_ret != -EPIPE) { | |
39f0f159 MD |
3620 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
3621 | } | |
ffe60014 DG |
3622 | goto rcu_error; |
3623 | } | |
3624 | ||
840cb59c | 3625 | health_code_update(); |
f37d259d | 3626 | if (count >= nbmem) { |
d7b3776f | 3627 | /* In case the realloc fails, we free the memory */ |
53efb85a MD |
3628 | struct lttng_event_field *new_tmp_event; |
3629 | size_t new_nbmem; | |
3630 | ||
3631 | new_nbmem = nbmem << 1; | |
3632 | DBG2("Reallocating event field list from %zu to %zu entries", | |
3633 | nbmem, new_nbmem); | |
3634 | new_tmp_event = realloc(tmp_event, | |
3635 | new_nbmem * sizeof(struct lttng_event_field)); | |
3636 | if (new_tmp_event == NULL) { | |
39f0f159 MD |
3637 | int release_ret; |
3638 | ||
f37d259d | 3639 | PERROR("realloc ust app event fields"); |
c617c0c6 | 3640 | free(tmp_event); |
f37d259d | 3641 | ret = -ENOMEM; |
39f0f159 MD |
3642 | release_ret = ustctl_release_handle(app->sock, handle); |
3643 | pthread_mutex_unlock(&app->sock_lock); | |
17a2c4ee JG |
3644 | if (release_ret && |
3645 | release_ret != -LTTNG_UST_ERR_EXITING && | |
3646 | release_ret != -EPIPE) { | |
39f0f159 MD |
3647 | ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret); |
3648 | } | |
f37d259d MD |
3649 | goto rcu_error; |
3650 | } | |
53efb85a MD |
3651 | /* Zero the new memory */ |
3652 | memset(new_tmp_event + nbmem, 0, | |
3653 | (new_nbmem - nbmem) * sizeof(struct lttng_event_field)); | |
3654 | nbmem = new_nbmem; | |
3655 | tmp_event = new_tmp_event; | |
f37d259d | 3656 | } |
f37d259d | 3657 | |
c617c0c6 | 3658 | memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN); |
2e84128e DG |
3659 | /* Mapping between these enums matches 1 to 1. */ |
3660 | tmp_event[count].type = (enum lttng_event_field_type) uiter.type; | |
c617c0c6 | 3661 | tmp_event[count].nowrite = uiter.nowrite; |
f37d259d | 3662 | |
c617c0c6 MD |
3663 | memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN); |
3664 | tmp_event[count].event.loglevel = uiter.loglevel; | |
2e84128e | 3665 | tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT; |
c617c0c6 MD |
3666 | tmp_event[count].event.pid = app->pid; |
3667 | tmp_event[count].event.enabled = -1; | |
f37d259d MD |
3668 | count++; |
3669 | } | |
39f0f159 MD |
3670 | ret = ustctl_release_handle(app->sock, handle); |
3671 | pthread_mutex_unlock(&app->sock_lock); | |
17a2c4ee JG |
3672 | if (ret < 0 && |
3673 | ret != -LTTNG_UST_ERR_EXITING && | |
3674 | ret != -EPIPE) { | |
39f0f159 MD |
3675 | ERR("Error releasing app handle for app %d with ret %d", app->sock, ret); |
3676 | } | |
f37d259d MD |
3677 | } |
3678 | ||
3679 | ret = count; | |
c617c0c6 | 3680 | *fields = tmp_event; |
f37d259d MD |
3681 | |
3682 | DBG2("UST app list event fields done (%zu events)", count); | |
3683 | ||
3684 | rcu_error: | |
3685 | rcu_read_unlock(); | |
3686 | error: | |
840cb59c | 3687 | health_code_update(); |
f37d259d MD |
3688 | return ret; |
3689 | } | |
3690 | ||
5b4a0ec0 DG |
3691 | /* |
3692 | * Free and clean all traceable apps of the global list. | |
36b588ed MD |
3693 | * |
3694 | * Should _NOT_ be called with RCU read-side lock held. | |
5b4a0ec0 DG |
3695 | */ |
3696 | void ust_app_clean_list(void) | |
421cb601 | 3697 | { |
5b4a0ec0 | 3698 | int ret; |
659ed79f | 3699 | struct ust_app *app; |
bec39940 | 3700 | struct lttng_ht_iter iter; |
421cb601 | 3701 | |
5b4a0ec0 | 3702 | DBG2("UST app cleaning registered apps hash table"); |
421cb601 | 3703 | |
5b4a0ec0 | 3704 | rcu_read_lock(); |
421cb601 | 3705 | |
f1b711c4 MD |
3706 | if (ust_app_ht) { |
3707 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
3708 | ret = lttng_ht_del(ust_app_ht, &iter); | |
3709 | assert(!ret); | |
3710 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
3711 | } | |
421cb601 DG |
3712 | } |
3713 | ||
852d0037 | 3714 | /* Cleanup socket hash table */ |
f1b711c4 MD |
3715 | if (ust_app_ht_by_sock) { |
3716 | cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app, | |
3717 | sock_n.node) { | |
3718 | ret = lttng_ht_del(ust_app_ht_by_sock, &iter); | |
3719 | assert(!ret); | |
3720 | } | |
bec39940 | 3721 | } |
852d0037 | 3722 | |
d88aee68 | 3723 | /* Cleanup notify socket hash table */ |
f1b711c4 MD |
3724 | if (ust_app_ht_by_notify_sock) { |
3725 | cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app, | |
3726 | notify_sock_n.node) { | |
3727 | ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
3728 | assert(!ret); | |
3729 | } | |
d88aee68 | 3730 | } |
36b588ed | 3731 | rcu_read_unlock(); |
d88aee68 | 3732 | |
bec39940 | 3733 | /* Destroy is done only when the ht is empty */ |
f1b711c4 MD |
3734 | if (ust_app_ht) { |
3735 | ht_cleanup_push(ust_app_ht); | |
3736 | } | |
3737 | if (ust_app_ht_by_sock) { | |
3738 | ht_cleanup_push(ust_app_ht_by_sock); | |
3739 | } | |
3740 | if (ust_app_ht_by_notify_sock) { | |
3741 | ht_cleanup_push(ust_app_ht_by_notify_sock); | |
3742 | } | |
5b4a0ec0 DG |
3743 | } |
3744 | ||
3745 | /* | |
3746 | * Init UST app hash table. | |
3747 | */ | |
57703f6e | 3748 | int ust_app_ht_alloc(void) |
5b4a0ec0 | 3749 | { |
bec39940 | 3750 | ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
3751 | if (!ust_app_ht) { |
3752 | return -1; | |
3753 | } | |
852d0037 | 3754 | ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
3755 | if (!ust_app_ht_by_sock) { |
3756 | return -1; | |
3757 | } | |
d0b96690 | 3758 | ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
57703f6e MD |
3759 | if (!ust_app_ht_by_notify_sock) { |
3760 | return -1; | |
3761 | } | |
3762 | return 0; | |
421cb601 DG |
3763 | } |
3764 | ||
78f0bacd DG |
3765 | /* |
3766 | * For a specific UST session, disable the channel for all registered apps. | |
3767 | */ | |
35a9059d | 3768 | int ust_app_disable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
3769 | struct ltt_ust_channel *uchan) |
3770 | { | |
3771 | int ret = 0; | |
bec39940 DG |
3772 | struct lttng_ht_iter iter; |
3773 | struct lttng_ht_node_str *ua_chan_node; | |
78f0bacd DG |
3774 | struct ust_app *app; |
3775 | struct ust_app_session *ua_sess; | |
8535a6d9 | 3776 | struct ust_app_channel *ua_chan; |
78f0bacd DG |
3777 | |
3778 | if (usess == NULL || uchan == NULL) { | |
3779 | ERR("Disabling UST global channel with NULL values"); | |
3780 | ret = -1; | |
3781 | goto error; | |
3782 | } | |
3783 | ||
d9bf3ca4 | 3784 | DBG2("UST app disabling channel %s from global domain for session id %" PRIu64, |
a991f516 | 3785 | uchan->name, usess->id); |
78f0bacd DG |
3786 | |
3787 | rcu_read_lock(); | |
3788 | ||
3789 | /* For every registered applications */ | |
852d0037 | 3790 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
bec39940 | 3791 | struct lttng_ht_iter uiter; |
e0c7ec2b DG |
3792 | if (!app->compatible) { |
3793 | /* | |
3794 | * TODO: In time, we should notice the caller of this error by | |
3795 | * telling him that this is a version error. | |
3796 | */ | |
3797 | continue; | |
3798 | } | |
78f0bacd DG |
3799 | ua_sess = lookup_session_by_app(usess, app); |
3800 | if (ua_sess == NULL) { | |
3801 | continue; | |
3802 | } | |
3803 | ||
8535a6d9 | 3804 | /* Get channel */ |
bec39940 DG |
3805 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
3806 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
8535a6d9 DG |
3807 | /* If the session if found for the app, the channel must be there */ |
3808 | assert(ua_chan_node); | |
3809 | ||
3810 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
3811 | /* The channel must not be already disabled */ | |
3812 | assert(ua_chan->enabled == 1); | |
3813 | ||
3814 | /* Disable channel onto application */ | |
3815 | ret = disable_ust_app_channel(ua_sess, ua_chan, app); | |
78f0bacd DG |
3816 | if (ret < 0) { |
3817 | /* XXX: We might want to report this error at some point... */ | |
3818 | continue; | |
3819 | } | |
3820 | } | |
3821 | ||
3822 | rcu_read_unlock(); | |
3823 | ||
3824 | error: | |
3825 | return ret; | |
3826 | } | |
3827 | ||
3828 | /* | |
3829 | * For a specific UST session, enable the channel for all registered apps. | |
3830 | */ | |
35a9059d | 3831 | int ust_app_enable_channel_glb(struct ltt_ust_session *usess, |
78f0bacd DG |
3832 | struct ltt_ust_channel *uchan) |
3833 | { | |
3834 | int ret = 0; | |
bec39940 | 3835 | struct lttng_ht_iter iter; |
78f0bacd DG |
3836 | struct ust_app *app; |
3837 | struct ust_app_session *ua_sess; | |
3838 | ||
3839 | if (usess == NULL || uchan == NULL) { | |
3840 | ERR("Adding UST global channel to NULL values"); | |
3841 | ret = -1; | |
3842 | goto error; | |
3843 | } | |
3844 | ||
d9bf3ca4 | 3845 | DBG2("UST app enabling channel %s to global domain for session id %" PRIu64, |
a991f516 | 3846 | uchan->name, usess->id); |
78f0bacd DG |
3847 | |
3848 | rcu_read_lock(); | |
3849 | ||
3850 | /* For every registered applications */ | |
852d0037 | 3851 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
3852 | if (!app->compatible) { |
3853 | /* | |
3854 | * TODO: In time, we should notice the caller of this error by | |
3855 | * telling him that this is a version error. | |
3856 | */ | |
3857 | continue; | |
3858 | } | |
78f0bacd DG |
3859 | ua_sess = lookup_session_by_app(usess, app); |
3860 | if (ua_sess == NULL) { | |
3861 | continue; | |
3862 | } | |
3863 | ||
3864 | /* Enable channel onto application */ | |
3865 | ret = enable_ust_app_channel(ua_sess, uchan, app); | |
3866 | if (ret < 0) { | |
3867 | /* XXX: We might want to report this error at some point... */ | |
3868 | continue; | |
3869 | } | |
3870 | } | |
3871 | ||
3872 | rcu_read_unlock(); | |
3873 | ||
3874 | error: | |
3875 | return ret; | |
3876 | } | |
3877 | ||
b0a40d28 DG |
3878 | /* |
3879 | * Disable an event in a channel and for a specific session. | |
3880 | */ | |
35a9059d DG |
3881 | int ust_app_disable_event_glb(struct ltt_ust_session *usess, |
3882 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) | |
b0a40d28 DG |
3883 | { |
3884 | int ret = 0; | |
bec39940 | 3885 | struct lttng_ht_iter iter, uiter; |
46010fd6 | 3886 | struct lttng_ht_node_str *ua_chan_node; |
b0a40d28 DG |
3887 | struct ust_app *app; |
3888 | struct ust_app_session *ua_sess; | |
3889 | struct ust_app_channel *ua_chan; | |
3890 | struct ust_app_event *ua_event; | |
3891 | ||
3892 | DBG("UST app disabling event %s for all apps in channel " | |
d9bf3ca4 MD |
3893 | "%s for session id %" PRIu64, |
3894 | uevent->attr.name, uchan->name, usess->id); | |
b0a40d28 DG |
3895 | |
3896 | rcu_read_lock(); | |
3897 | ||
3898 | /* For all registered applications */ | |
852d0037 | 3899 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
3900 | if (!app->compatible) { |
3901 | /* | |
3902 | * TODO: In time, we should notice the caller of this error by | |
3903 | * telling him that this is a version error. | |
3904 | */ | |
3905 | continue; | |
3906 | } | |
b0a40d28 DG |
3907 | ua_sess = lookup_session_by_app(usess, app); |
3908 | if (ua_sess == NULL) { | |
3909 | /* Next app */ | |
3910 | continue; | |
3911 | } | |
3912 | ||
3913 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
3914 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
3915 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
b0a40d28 | 3916 | if (ua_chan_node == NULL) { |
d9bf3ca4 | 3917 | DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d." |
852d0037 | 3918 | "Skipping", uchan->name, usess->id, app->pid); |
b0a40d28 DG |
3919 | continue; |
3920 | } | |
3921 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
3922 | ||
46010fd6 JR |
3923 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
3924 | uevent->filter, uevent->attr.loglevel, | |
3925 | uevent->exclusion); | |
3926 | if (ua_event == NULL) { | |
b0a40d28 | 3927 | DBG2("Event %s not found in channel %s for app pid %d." |
852d0037 | 3928 | "Skipping", uevent->attr.name, uchan->name, app->pid); |
b0a40d28 DG |
3929 | continue; |
3930 | } | |
b0a40d28 | 3931 | |
7f79d3a1 | 3932 | ret = disable_ust_app_event(ua_sess, ua_event, app); |
b0a40d28 DG |
3933 | if (ret < 0) { |
3934 | /* XXX: Report error someday... */ | |
3935 | continue; | |
3936 | } | |
3937 | } | |
3938 | ||
3939 | rcu_read_unlock(); | |
3940 | ||
3941 | return ret; | |
3942 | } | |
3943 | ||
421cb601 | 3944 | /* |
5b4a0ec0 | 3945 | * For a specific UST session, create the channel for all registered apps. |
421cb601 | 3946 | */ |
35a9059d | 3947 | int ust_app_create_channel_glb(struct ltt_ust_session *usess, |
48842b30 DG |
3948 | struct ltt_ust_channel *uchan) |
3949 | { | |
3d8ca23b | 3950 | int ret = 0, created; |
bec39940 | 3951 | struct lttng_ht_iter iter; |
48842b30 | 3952 | struct ust_app *app; |
3d8ca23b | 3953 | struct ust_app_session *ua_sess = NULL; |
48842b30 | 3954 | |
fc34caaa DG |
3955 | /* Very wrong code flow */ |
3956 | assert(usess); | |
3957 | assert(uchan); | |
421cb601 | 3958 | |
d9bf3ca4 | 3959 | DBG2("UST app adding channel %s to UST domain for session id %" PRIu64, |
a991f516 | 3960 | uchan->name, usess->id); |
48842b30 DG |
3961 | |
3962 | rcu_read_lock(); | |
421cb601 | 3963 | |
5b4a0ec0 | 3964 | /* For every registered applications */ |
852d0037 | 3965 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
3966 | if (!app->compatible) { |
3967 | /* | |
3968 | * TODO: In time, we should notice the caller of this error by | |
3969 | * telling him that this is a version error. | |
3970 | */ | |
3971 | continue; | |
3972 | } | |
a9ad0c8f MD |
3973 | if (!trace_ust_pid_tracker_lookup(usess, app->pid)) { |
3974 | /* Skip. */ | |
3975 | continue; | |
3976 | } | |
3977 | ||
edb67388 DG |
3978 | /* |
3979 | * Create session on the tracer side and add it to app session HT. Note | |
3980 | * that if session exist, it will simply return a pointer to the ust | |
3981 | * app session. | |
3982 | */ | |
3d8ca23b DG |
3983 | ret = create_ust_app_session(usess, app, &ua_sess, &created); |
3984 | if (ret < 0) { | |
3985 | switch (ret) { | |
3986 | case -ENOTCONN: | |
3987 | /* | |
3988 | * The application's socket is not valid. Either a bad socket | |
3989 | * or a timeout on it. We can't inform the caller that for a | |
3990 | * specific app, the session failed so lets continue here. | |
3991 | */ | |
320b3a68 | 3992 | ret = 0; /* Not an error. */ |
3d8ca23b DG |
3993 | continue; |
3994 | case -ENOMEM: | |
3995 | default: | |
3996 | goto error_rcu_unlock; | |
3997 | } | |
48842b30 | 3998 | } |
3d8ca23b | 3999 | assert(ua_sess); |
48842b30 | 4000 | |
d0b96690 | 4001 | pthread_mutex_lock(&ua_sess->lock); |
d40a0cb1 MD |
4002 | |
4003 | if (ua_sess->deleted) { | |
4004 | pthread_mutex_unlock(&ua_sess->lock); | |
4005 | continue; | |
4006 | } | |
4007 | ||
d65d2de8 DG |
4008 | if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, |
4009 | sizeof(uchan->name))) { | |
ad7a9107 DG |
4010 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr); |
4011 | ret = 0; | |
d65d2de8 DG |
4012 | } else { |
4013 | /* Create channel onto application. We don't need the chan ref. */ | |
4014 | ret = create_ust_app_channel(ua_sess, uchan, app, | |
4015 | LTTNG_UST_CHAN_PER_CPU, usess, NULL); | |
4016 | } | |
d0b96690 | 4017 | pthread_mutex_unlock(&ua_sess->lock); |
3d8ca23b | 4018 | if (ret < 0) { |
3d8ca23b DG |
4019 | /* Cleanup the created session if it's the case. */ |
4020 | if (created) { | |
d0b96690 | 4021 | destroy_app_session(app, ua_sess); |
3d8ca23b | 4022 | } |
320b3a68 MD |
4023 | switch (ret) { |
4024 | case -ENOTCONN: | |
4025 | /* | |
4026 | * The application's socket is not valid. Either a bad socket | |
4027 | * or a timeout on it. We can't inform the caller that for a | |
4028 | * specific app, the session failed so lets continue here. | |
4029 | */ | |
4030 | ret = 0; /* Not an error. */ | |
4031 | continue; | |
4032 | case -ENOMEM: | |
4033 | default: | |
4034 | goto error_rcu_unlock; | |
4035 | } | |
48842b30 | 4036 | } |
48842b30 | 4037 | } |
5b4a0ec0 | 4038 | |
95e047ff | 4039 | error_rcu_unlock: |
48842b30 | 4040 | rcu_read_unlock(); |
3c14c33f | 4041 | return ret; |
48842b30 DG |
4042 | } |
4043 | ||
5b4a0ec0 | 4044 | /* |
edb67388 | 4045 | * Enable event for a specific session and channel on the tracer. |
5b4a0ec0 | 4046 | */ |
35a9059d | 4047 | int ust_app_enable_event_glb(struct ltt_ust_session *usess, |
48842b30 DG |
4048 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4049 | { | |
4050 | int ret = 0; | |
bec39940 | 4051 | struct lttng_ht_iter iter, uiter; |
18eace3b | 4052 | struct lttng_ht_node_str *ua_chan_node; |
48842b30 DG |
4053 | struct ust_app *app; |
4054 | struct ust_app_session *ua_sess; | |
4055 | struct ust_app_channel *ua_chan; | |
4056 | struct ust_app_event *ua_event; | |
48842b30 | 4057 | |
d9bf3ca4 | 4058 | DBG("UST app enabling event %s for all apps for session id %" PRIu64, |
a991f516 | 4059 | uevent->attr.name, usess->id); |
48842b30 | 4060 | |
edb67388 DG |
4061 | /* |
4062 | * NOTE: At this point, this function is called only if the session and | |
4063 | * channel passed are already created for all apps. and enabled on the | |
4064 | * tracer also. | |
4065 | */ | |
4066 | ||
48842b30 | 4067 | rcu_read_lock(); |
421cb601 DG |
4068 | |
4069 | /* For all registered applications */ | |
852d0037 | 4070 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4071 | if (!app->compatible) { |
4072 | /* | |
4073 | * TODO: In time, we should notice the caller of this error by | |
4074 | * telling him that this is a version error. | |
4075 | */ | |
4076 | continue; | |
4077 | } | |
edb67388 | 4078 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4079 | if (!ua_sess) { |
4080 | /* The application has problem or is probably dead. */ | |
4081 | continue; | |
4082 | } | |
ba767faf | 4083 | |
d0b96690 DG |
4084 | pthread_mutex_lock(&ua_sess->lock); |
4085 | ||
d40a0cb1 MD |
4086 | if (ua_sess->deleted) { |
4087 | pthread_mutex_unlock(&ua_sess->lock); | |
4088 | continue; | |
4089 | } | |
4090 | ||
edb67388 | 4091 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4092 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4093 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
320b3a68 MD |
4094 | /* |
4095 | * It is possible that the channel cannot be found is | |
4096 | * the channel/event creation occurs concurrently with | |
4097 | * an application exit. | |
4098 | */ | |
4099 | if (!ua_chan_node) { | |
4100 | pthread_mutex_unlock(&ua_sess->lock); | |
4101 | continue; | |
4102 | } | |
edb67388 DG |
4103 | |
4104 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4105 | ||
18eace3b DG |
4106 | /* Get event node */ |
4107 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, | |
39c5a3a7 | 4108 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 4109 | if (ua_event == NULL) { |
7f79d3a1 | 4110 | DBG3("UST app enable event %s not found for app PID %d." |
852d0037 | 4111 | "Skipping app", uevent->attr.name, app->pid); |
d0b96690 | 4112 | goto next_app; |
35a9059d | 4113 | } |
35a9059d DG |
4114 | |
4115 | ret = enable_ust_app_event(ua_sess, ua_event, app); | |
4116 | if (ret < 0) { | |
d0b96690 | 4117 | pthread_mutex_unlock(&ua_sess->lock); |
7f79d3a1 | 4118 | goto error; |
48842b30 | 4119 | } |
d0b96690 DG |
4120 | next_app: |
4121 | pthread_mutex_unlock(&ua_sess->lock); | |
edb67388 DG |
4122 | } |
4123 | ||
7f79d3a1 | 4124 | error: |
edb67388 | 4125 | rcu_read_unlock(); |
edb67388 DG |
4126 | return ret; |
4127 | } | |
4128 | ||
4129 | /* | |
4130 | * For a specific existing UST session and UST channel, creates the event for | |
4131 | * all registered apps. | |
4132 | */ | |
35a9059d | 4133 | int ust_app_create_event_glb(struct ltt_ust_session *usess, |
edb67388 DG |
4134 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent) |
4135 | { | |
4136 | int ret = 0; | |
bec39940 DG |
4137 | struct lttng_ht_iter iter, uiter; |
4138 | struct lttng_ht_node_str *ua_chan_node; | |
edb67388 DG |
4139 | struct ust_app *app; |
4140 | struct ust_app_session *ua_sess; | |
4141 | struct ust_app_channel *ua_chan; | |
4142 | ||
d9bf3ca4 | 4143 | DBG("UST app creating event %s for all apps for session id %" PRIu64, |
a991f516 | 4144 | uevent->attr.name, usess->id); |
edb67388 | 4145 | |
edb67388 DG |
4146 | rcu_read_lock(); |
4147 | ||
4148 | /* For all registered applications */ | |
852d0037 | 4149 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4150 | if (!app->compatible) { |
4151 | /* | |
4152 | * TODO: In time, we should notice the caller of this error by | |
4153 | * telling him that this is a version error. | |
4154 | */ | |
4155 | continue; | |
4156 | } | |
edb67388 | 4157 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4158 | if (!ua_sess) { |
4159 | /* The application has problem or is probably dead. */ | |
4160 | continue; | |
4161 | } | |
48842b30 | 4162 | |
d0b96690 | 4163 | pthread_mutex_lock(&ua_sess->lock); |
d40a0cb1 MD |
4164 | |
4165 | if (ua_sess->deleted) { | |
4166 | pthread_mutex_unlock(&ua_sess->lock); | |
4167 | continue; | |
4168 | } | |
4169 | ||
48842b30 | 4170 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4171 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4172 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
edb67388 DG |
4173 | /* If the channel is not found, there is a code flow error */ |
4174 | assert(ua_chan_node); | |
4175 | ||
48842b30 DG |
4176 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
4177 | ||
edb67388 | 4178 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
d0b96690 | 4179 | pthread_mutex_unlock(&ua_sess->lock); |
edb67388 | 4180 | if (ret < 0) { |
49c336c1 | 4181 | if (ret != -LTTNG_UST_ERR_EXIST) { |
fc34caaa DG |
4182 | /* Possible value at this point: -ENOMEM. If so, we stop! */ |
4183 | break; | |
4184 | } | |
4185 | DBG2("UST app event %s already exist on app PID %d", | |
852d0037 | 4186 | uevent->attr.name, app->pid); |
5b4a0ec0 | 4187 | continue; |
48842b30 | 4188 | } |
48842b30 | 4189 | } |
5b4a0ec0 | 4190 | |
48842b30 DG |
4191 | rcu_read_unlock(); |
4192 | ||
4193 | return ret; | |
4194 | } | |
4195 | ||
5b4a0ec0 DG |
4196 | /* |
4197 | * Start tracing for a specific UST session and app. | |
4198 | */ | |
b34cbebf | 4199 | static |
421cb601 | 4200 | int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 DG |
4201 | { |
4202 | int ret = 0; | |
48842b30 | 4203 | struct ust_app_session *ua_sess; |
48842b30 | 4204 | |
852d0037 | 4205 | DBG("Starting tracing for ust app pid %d", app->pid); |
5cf5d0e7 | 4206 | |
509cbaf8 MD |
4207 | rcu_read_lock(); |
4208 | ||
e0c7ec2b DG |
4209 | if (!app->compatible) { |
4210 | goto end; | |
4211 | } | |
4212 | ||
421cb601 DG |
4213 | ua_sess = lookup_session_by_app(usess, app); |
4214 | if (ua_sess == NULL) { | |
d42f20df DG |
4215 | /* The session is in teardown process. Ignore and continue. */ |
4216 | goto end; | |
421cb601 | 4217 | } |
48842b30 | 4218 | |
d0b96690 DG |
4219 | pthread_mutex_lock(&ua_sess->lock); |
4220 | ||
d40a0cb1 MD |
4221 | if (ua_sess->deleted) { |
4222 | pthread_mutex_unlock(&ua_sess->lock); | |
4223 | goto end; | |
4224 | } | |
4225 | ||
aea829b3 DG |
4226 | /* Upon restart, we skip the setup, already done */ |
4227 | if (ua_sess->started) { | |
8be98f9a | 4228 | goto skip_setup; |
aea829b3 | 4229 | } |
8be98f9a | 4230 | |
a4b92340 DG |
4231 | /* Create directories if consumer is LOCAL and has a path defined. */ |
4232 | if (usess->consumer->type == CONSUMER_DST_LOCAL && | |
4233 | strlen(usess->consumer->dst.trace_path) > 0) { | |
4234 | ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, | |
7972aab2 | 4235 | S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid); |
a4b92340 | 4236 | if (ret < 0) { |
a68b8851 | 4237 | if (errno != EEXIST) { |
a4b92340 | 4238 | ERR("Trace directory creation error"); |
d0b96690 | 4239 | goto error_unlock; |
421cb601 | 4240 | } |
173af62f | 4241 | } |
7753dea8 | 4242 | } |
aea829b3 | 4243 | |
d65d2de8 DG |
4244 | /* |
4245 | * Create the metadata for the application. This returns gracefully if a | |
4246 | * metadata was already set for the session. | |
4247 | */ | |
ad7a9107 | 4248 | ret = create_ust_app_metadata(ua_sess, app, usess->consumer); |
421cb601 | 4249 | if (ret < 0) { |
d0b96690 | 4250 | goto error_unlock; |
421cb601 | 4251 | } |
48842b30 | 4252 | |
840cb59c | 4253 | health_code_update(); |
86acf0da | 4254 | |
8be98f9a | 4255 | skip_setup: |
421cb601 | 4256 | /* This start the UST tracing */ |
39f0f159 | 4257 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4258 | ret = ustctl_start_session(app->sock, ua_sess->handle); |
39f0f159 | 4259 | pthread_mutex_unlock(&app->sock_lock); |
421cb601 | 4260 | if (ret < 0) { |
ffe60014 DG |
4261 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4262 | ERR("Error starting tracing for app pid: %d (ret: %d)", | |
4263 | app->pid, ret); | |
4264 | } else { | |
4265 | DBG("UST app start session failed. Application is dead."); | |
3757b385 DG |
4266 | /* |
4267 | * This is normal behavior, an application can die during the | |
4268 | * creation process. Don't report an error so the execution can | |
4269 | * continue normally. | |
4270 | */ | |
4271 | pthread_mutex_unlock(&ua_sess->lock); | |
4272 | goto end; | |
ffe60014 | 4273 | } |
d0b96690 | 4274 | goto error_unlock; |
421cb601 | 4275 | } |
5b4a0ec0 | 4276 | |
55c3953d DG |
4277 | /* Indicate that the session has been started once */ |
4278 | ua_sess->started = 1; | |
4279 | ||
d0b96690 DG |
4280 | pthread_mutex_unlock(&ua_sess->lock); |
4281 | ||
840cb59c | 4282 | health_code_update(); |
86acf0da | 4283 | |
421cb601 | 4284 | /* Quiescent wait after starting trace */ |
39f0f159 | 4285 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 4286 | ret = ustctl_wait_quiescent(app->sock); |
39f0f159 | 4287 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
4288 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4289 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
4290 | app->pid, ret); | |
4291 | } | |
48842b30 | 4292 | |
e0c7ec2b DG |
4293 | end: |
4294 | rcu_read_unlock(); | |
840cb59c | 4295 | health_code_update(); |
421cb601 | 4296 | return 0; |
48842b30 | 4297 | |
d0b96690 DG |
4298 | error_unlock: |
4299 | pthread_mutex_unlock(&ua_sess->lock); | |
509cbaf8 | 4300 | rcu_read_unlock(); |
840cb59c | 4301 | health_code_update(); |
421cb601 DG |
4302 | return -1; |
4303 | } | |
48842b30 | 4304 | |
8be98f9a MD |
4305 | /* |
4306 | * Stop tracing for a specific UST session and app. | |
4307 | */ | |
b34cbebf | 4308 | static |
8be98f9a MD |
4309 | int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app) |
4310 | { | |
4311 | int ret = 0; | |
4312 | struct ust_app_session *ua_sess; | |
7972aab2 | 4313 | struct ust_registry_session *registry; |
8be98f9a | 4314 | |
852d0037 | 4315 | DBG("Stopping tracing for ust app pid %d", app->pid); |
8be98f9a MD |
4316 | |
4317 | rcu_read_lock(); | |
4318 | ||
e0c7ec2b | 4319 | if (!app->compatible) { |
d88aee68 | 4320 | goto end_no_session; |
e0c7ec2b DG |
4321 | } |
4322 | ||
8be98f9a MD |
4323 | ua_sess = lookup_session_by_app(usess, app); |
4324 | if (ua_sess == NULL) { | |
d88aee68 | 4325 | goto end_no_session; |
8be98f9a MD |
4326 | } |
4327 | ||
d88aee68 DG |
4328 | pthread_mutex_lock(&ua_sess->lock); |
4329 | ||
d40a0cb1 MD |
4330 | if (ua_sess->deleted) { |
4331 | pthread_mutex_unlock(&ua_sess->lock); | |
4332 | goto end_no_session; | |
4333 | } | |
4334 | ||
9bc07046 DG |
4335 | /* |
4336 | * If started = 0, it means that stop trace has been called for a session | |
c45536e1 DG |
4337 | * that was never started. It's possible since we can have a fail start |
4338 | * from either the application manager thread or the command thread. Simply | |
4339 | * indicate that this is a stop error. | |
9bc07046 | 4340 | */ |
f9dfc3d9 | 4341 | if (!ua_sess->started) { |
c45536e1 DG |
4342 | goto error_rcu_unlock; |
4343 | } | |
7db205b5 | 4344 | |
840cb59c | 4345 | health_code_update(); |
86acf0da | 4346 | |
9d6c7d3f | 4347 | /* This inhibits UST tracing */ |
39f0f159 | 4348 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4349 | ret = ustctl_stop_session(app->sock, ua_sess->handle); |
39f0f159 | 4350 | pthread_mutex_unlock(&app->sock_lock); |
9d6c7d3f | 4351 | if (ret < 0) { |
ffe60014 DG |
4352 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4353 | ERR("Error stopping tracing for app pid: %d (ret: %d)", | |
4354 | app->pid, ret); | |
4355 | } else { | |
4356 | DBG("UST app stop session failed. Application is dead."); | |
3757b385 DG |
4357 | /* |
4358 | * This is normal behavior, an application can die during the | |
4359 | * creation process. Don't report an error so the execution can | |
4360 | * continue normally. | |
4361 | */ | |
4362 | goto end_unlock; | |
ffe60014 | 4363 | } |
9d6c7d3f DG |
4364 | goto error_rcu_unlock; |
4365 | } | |
4366 | ||
840cb59c | 4367 | health_code_update(); |
86acf0da | 4368 | |
9d6c7d3f | 4369 | /* Quiescent wait after stopping trace */ |
39f0f159 | 4370 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 4371 | ret = ustctl_wait_quiescent(app->sock); |
39f0f159 | 4372 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
4373 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4374 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
4375 | app->pid, ret); | |
4376 | } | |
9d6c7d3f | 4377 | |
840cb59c | 4378 | health_code_update(); |
86acf0da | 4379 | |
b34cbebf MD |
4380 | registry = get_session_registry(ua_sess); |
4381 | assert(registry); | |
1b532a60 | 4382 | |
ce34fcd0 MD |
4383 | /* Push metadata for application before freeing the application. */ |
4384 | (void) push_metadata(registry, ua_sess->consumer); | |
b34cbebf | 4385 | |
3757b385 | 4386 | end_unlock: |
b34cbebf MD |
4387 | pthread_mutex_unlock(&ua_sess->lock); |
4388 | end_no_session: | |
4389 | rcu_read_unlock(); | |
4390 | health_code_update(); | |
4391 | return 0; | |
4392 | ||
4393 | error_rcu_unlock: | |
4394 | pthread_mutex_unlock(&ua_sess->lock); | |
4395 | rcu_read_unlock(); | |
4396 | health_code_update(); | |
4397 | return -1; | |
4398 | } | |
4399 | ||
b34cbebf | 4400 | static |
c4b88406 MD |
4401 | int ust_app_flush_app_session(struct ust_app *app, |
4402 | struct ust_app_session *ua_sess) | |
b34cbebf | 4403 | { |
c4b88406 | 4404 | int ret, retval = 0; |
b34cbebf | 4405 | struct lttng_ht_iter iter; |
b34cbebf | 4406 | struct ust_app_channel *ua_chan; |
c4b88406 | 4407 | struct consumer_socket *socket; |
b34cbebf | 4408 | |
c4b88406 | 4409 | DBG("Flushing app session buffers for ust app pid %d", app->pid); |
b34cbebf MD |
4410 | |
4411 | rcu_read_lock(); | |
4412 | ||
4413 | if (!app->compatible) { | |
c4b88406 | 4414 | goto end_not_compatible; |
b34cbebf MD |
4415 | } |
4416 | ||
4417 | pthread_mutex_lock(&ua_sess->lock); | |
4418 | ||
d40a0cb1 MD |
4419 | if (ua_sess->deleted) { |
4420 | goto end_deleted; | |
4421 | } | |
4422 | ||
b34cbebf MD |
4423 | health_code_update(); |
4424 | ||
9d6c7d3f | 4425 | /* Flushing buffers */ |
c4b88406 MD |
4426 | socket = consumer_find_socket_by_bitness(app->bits_per_long, |
4427 | ua_sess->consumer); | |
ce34fcd0 MD |
4428 | |
4429 | /* Flush buffers and push metadata. */ | |
4430 | switch (ua_sess->buffer_type) { | |
4431 | case LTTNG_BUFFER_PER_PID: | |
4432 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, | |
4433 | node.node) { | |
4434 | health_code_update(); | |
4435 | assert(ua_chan->is_sent); | |
4436 | ret = consumer_flush_channel(socket, ua_chan->key); | |
4437 | if (ret) { | |
4438 | ERR("Error flushing consumer channel"); | |
4439 | retval = -1; | |
4440 | continue; | |
4441 | } | |
8be98f9a | 4442 | } |
ce34fcd0 MD |
4443 | break; |
4444 | case LTTNG_BUFFER_PER_UID: | |
4445 | default: | |
4446 | assert(0); | |
4447 | break; | |
8be98f9a | 4448 | } |
8be98f9a | 4449 | |
840cb59c | 4450 | health_code_update(); |
86acf0da | 4451 | |
d40a0cb1 | 4452 | end_deleted: |
d88aee68 | 4453 | pthread_mutex_unlock(&ua_sess->lock); |
ce34fcd0 | 4454 | |
c4b88406 MD |
4455 | end_not_compatible: |
4456 | rcu_read_unlock(); | |
4457 | health_code_update(); | |
4458 | return retval; | |
4459 | } | |
4460 | ||
4461 | /* | |
ce34fcd0 MD |
4462 | * Flush buffers for all applications for a specific UST session. |
4463 | * Called with UST session lock held. | |
c4b88406 MD |
4464 | */ |
4465 | static | |
ce34fcd0 | 4466 | int ust_app_flush_session(struct ltt_ust_session *usess) |
c4b88406 MD |
4467 | |
4468 | { | |
99b1411c | 4469 | int ret = 0; |
c4b88406 | 4470 | |
ce34fcd0 | 4471 | DBG("Flushing session buffers for all ust apps"); |
c4b88406 MD |
4472 | |
4473 | rcu_read_lock(); | |
4474 | ||
ce34fcd0 MD |
4475 | /* Flush buffers and push metadata. */ |
4476 | switch (usess->buffer_type) { | |
4477 | case LTTNG_BUFFER_PER_UID: | |
4478 | { | |
4479 | struct buffer_reg_uid *reg; | |
4480 | struct lttng_ht_iter iter; | |
4481 | ||
4482 | /* Flush all per UID buffers associated to that session. */ | |
4483 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
4484 | struct ust_registry_session *ust_session_reg; | |
4485 | struct buffer_reg_channel *reg_chan; | |
4486 | struct consumer_socket *socket; | |
4487 | ||
4488 | /* Get consumer socket to use to push the metadata.*/ | |
4489 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
4490 | usess->consumer); | |
4491 | if (!socket) { | |
4492 | /* Ignore request if no consumer is found for the session. */ | |
4493 | continue; | |
4494 | } | |
4495 | ||
4496 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
4497 | reg_chan, node.node) { | |
4498 | /* | |
4499 | * The following call will print error values so the return | |
4500 | * code is of little importance because whatever happens, we | |
4501 | * have to try them all. | |
4502 | */ | |
4503 | (void) consumer_flush_channel(socket, reg_chan->consumer_key); | |
4504 | } | |
4505 | ||
4506 | ust_session_reg = reg->registry->reg.ust; | |
4507 | /* Push metadata. */ | |
4508 | (void) push_metadata(ust_session_reg, usess->consumer); | |
4509 | } | |
ce34fcd0 MD |
4510 | break; |
4511 | } | |
4512 | case LTTNG_BUFFER_PER_PID: | |
4513 | { | |
4514 | struct ust_app_session *ua_sess; | |
4515 | struct lttng_ht_iter iter; | |
4516 | struct ust_app *app; | |
4517 | ||
4518 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4519 | ua_sess = lookup_session_by_app(usess, app); | |
4520 | if (ua_sess == NULL) { | |
4521 | continue; | |
4522 | } | |
4523 | (void) ust_app_flush_app_session(app, ua_sess); | |
4524 | } | |
4525 | break; | |
4526 | } | |
4527 | default: | |
99b1411c | 4528 | ret = -1; |
ce34fcd0 MD |
4529 | assert(0); |
4530 | break; | |
c4b88406 | 4531 | } |
c4b88406 | 4532 | |
7db205b5 | 4533 | rcu_read_unlock(); |
840cb59c | 4534 | health_code_update(); |
c4b88406 | 4535 | return ret; |
8be98f9a MD |
4536 | } |
4537 | ||
84cd17c6 MD |
4538 | /* |
4539 | * Destroy a specific UST session in apps. | |
4540 | */ | |
3353de95 | 4541 | static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app) |
84cd17c6 | 4542 | { |
ffe60014 | 4543 | int ret; |
84cd17c6 | 4544 | struct ust_app_session *ua_sess; |
bec39940 | 4545 | struct lttng_ht_iter iter; |
d9bf3ca4 | 4546 | struct lttng_ht_node_u64 *node; |
84cd17c6 | 4547 | |
852d0037 | 4548 | DBG("Destroy tracing for ust app pid %d", app->pid); |
84cd17c6 MD |
4549 | |
4550 | rcu_read_lock(); | |
4551 | ||
e0c7ec2b DG |
4552 | if (!app->compatible) { |
4553 | goto end; | |
4554 | } | |
4555 | ||
84cd17c6 | 4556 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 4557 | node = lttng_ht_iter_get_node_u64(&iter); |
84cd17c6 | 4558 | if (node == NULL) { |
d42f20df DG |
4559 | /* Session is being or is deleted. */ |
4560 | goto end; | |
84cd17c6 MD |
4561 | } |
4562 | ua_sess = caa_container_of(node, struct ust_app_session, node); | |
c4a1715b | 4563 | |
840cb59c | 4564 | health_code_update(); |
d0b96690 | 4565 | destroy_app_session(app, ua_sess); |
84cd17c6 | 4566 | |
840cb59c | 4567 | health_code_update(); |
7db205b5 | 4568 | |
84cd17c6 | 4569 | /* Quiescent wait after stopping trace */ |
39f0f159 | 4570 | pthread_mutex_lock(&app->sock_lock); |
ffe60014 | 4571 | ret = ustctl_wait_quiescent(app->sock); |
39f0f159 | 4572 | pthread_mutex_unlock(&app->sock_lock); |
ffe60014 DG |
4573 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
4574 | ERR("UST app wait quiescent failed for app pid %d ret %d", | |
4575 | app->pid, ret); | |
4576 | } | |
e0c7ec2b DG |
4577 | end: |
4578 | rcu_read_unlock(); | |
840cb59c | 4579 | health_code_update(); |
84cd17c6 | 4580 | return 0; |
84cd17c6 MD |
4581 | } |
4582 | ||
5b4a0ec0 DG |
4583 | /* |
4584 | * Start tracing for the UST session. | |
4585 | */ | |
421cb601 DG |
4586 | int ust_app_start_trace_all(struct ltt_ust_session *usess) |
4587 | { | |
4588 | int ret = 0; | |
bec39940 | 4589 | struct lttng_ht_iter iter; |
421cb601 | 4590 | struct ust_app *app; |
48842b30 | 4591 | |
421cb601 DG |
4592 | DBG("Starting all UST traces"); |
4593 | ||
4594 | rcu_read_lock(); | |
421cb601 | 4595 | |
852d0037 | 4596 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
421cb601 | 4597 | ret = ust_app_start_trace(usess, app); |
48842b30 | 4598 | if (ret < 0) { |
5b4a0ec0 DG |
4599 | /* Continue to next apps even on error */ |
4600 | continue; | |
48842b30 | 4601 | } |
48842b30 | 4602 | } |
5b4a0ec0 | 4603 | |
48842b30 DG |
4604 | rcu_read_unlock(); |
4605 | ||
4606 | return 0; | |
4607 | } | |
487cf67c | 4608 | |
8be98f9a MD |
4609 | /* |
4610 | * Start tracing for the UST session. | |
ce34fcd0 | 4611 | * Called with UST session lock held. |
8be98f9a MD |
4612 | */ |
4613 | int ust_app_stop_trace_all(struct ltt_ust_session *usess) | |
4614 | { | |
4615 | int ret = 0; | |
bec39940 | 4616 | struct lttng_ht_iter iter; |
8be98f9a MD |
4617 | struct ust_app *app; |
4618 | ||
4619 | DBG("Stopping all UST traces"); | |
4620 | ||
4621 | rcu_read_lock(); | |
4622 | ||
b34cbebf MD |
4623 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
4624 | ret = ust_app_stop_trace(usess, app); | |
4625 | if (ret < 0) { | |
4626 | /* Continue to next apps even on error */ | |
4627 | continue; | |
4628 | } | |
4629 | } | |
4630 | ||
ce34fcd0 | 4631 | (void) ust_app_flush_session(usess); |
8be98f9a MD |
4632 | |
4633 | rcu_read_unlock(); | |
4634 | ||
4635 | return 0; | |
4636 | } | |
4637 | ||
84cd17c6 MD |
4638 | /* |
4639 | * Destroy app UST session. | |
4640 | */ | |
4641 | int ust_app_destroy_trace_all(struct ltt_ust_session *usess) | |
4642 | { | |
4643 | int ret = 0; | |
bec39940 | 4644 | struct lttng_ht_iter iter; |
84cd17c6 MD |
4645 | struct ust_app *app; |
4646 | ||
4647 | DBG("Destroy all UST traces"); | |
4648 | ||
4649 | rcu_read_lock(); | |
4650 | ||
852d0037 | 4651 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
3353de95 | 4652 | ret = destroy_trace(usess, app); |
84cd17c6 MD |
4653 | if (ret < 0) { |
4654 | /* Continue to next apps even on error */ | |
4655 | continue; | |
4656 | } | |
4657 | } | |
4658 | ||
4659 | rcu_read_unlock(); | |
4660 | ||
4661 | return 0; | |
4662 | } | |
4663 | ||
a9ad0c8f MD |
4664 | static |
4665 | void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app) | |
487cf67c | 4666 | { |
55c54cce | 4667 | int ret = 0; |
31746f93 | 4668 | struct lttng_ht_iter iter, uiter; |
3d8ca23b | 4669 | struct ust_app_session *ua_sess = NULL; |
487cf67c DG |
4670 | struct ust_app_channel *ua_chan; |
4671 | struct ust_app_event *ua_event; | |
727d5404 | 4672 | struct ust_app_ctx *ua_ctx; |
a9ad0c8f | 4673 | int is_created = 0; |
1f3580c7 | 4674 | |
a9ad0c8f | 4675 | ret = create_ust_app_session(usess, app, &ua_sess, &is_created); |
3d8ca23b DG |
4676 | if (ret < 0) { |
4677 | /* Tracer is probably gone or ENOMEM. */ | |
487cf67c DG |
4678 | goto error; |
4679 | } | |
a9ad0c8f MD |
4680 | if (!is_created) { |
4681 | /* App session already created. */ | |
4682 | goto end; | |
4683 | } | |
3d8ca23b | 4684 | assert(ua_sess); |
487cf67c | 4685 | |
d0b96690 DG |
4686 | pthread_mutex_lock(&ua_sess->lock); |
4687 | ||
d40a0cb1 MD |
4688 | if (ua_sess->deleted) { |
4689 | pthread_mutex_unlock(&ua_sess->lock); | |
4690 | goto end; | |
4691 | } | |
4692 | ||
284d8f55 | 4693 | /* |
d0b96690 | 4694 | * We can iterate safely here over all UST app session since the create ust |
284d8f55 DG |
4695 | * app session above made a shadow copy of the UST global domain from the |
4696 | * ltt ust session. | |
4697 | */ | |
bec39940 DG |
4698 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
4699 | node.node) { | |
ad7a9107 | 4700 | ret = do_create_channel(app, usess, ua_sess, ua_chan); |
320b3a68 | 4701 | if (ret < 0 && ret != -ENOTCONN) { |
ad7a9107 | 4702 | /* |
320b3a68 MD |
4703 | * Stop everything. On error, the application |
4704 | * failed, no more file descriptor are available | |
4705 | * or ENOMEM so stopping here is the only thing | |
4706 | * we can do for now. The only exception is | |
4707 | * -ENOTCONN, which indicates that the application | |
4708 | * has exit. | |
ad7a9107 DG |
4709 | */ |
4710 | goto error_unlock; | |
487cf67c DG |
4711 | } |
4712 | ||
31746f93 DG |
4713 | /* |
4714 | * Add context using the list so they are enabled in the same order the | |
4715 | * user added them. | |
4716 | */ | |
4717 | cds_list_for_each_entry(ua_ctx, &ua_chan->ctx_list, list) { | |
727d5404 DG |
4718 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); |
4719 | if (ret < 0) { | |
d0b96690 | 4720 | goto error_unlock; |
727d5404 DG |
4721 | } |
4722 | } | |
4723 | ||
4724 | ||
284d8f55 | 4725 | /* For each events */ |
bec39940 DG |
4726 | cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event, |
4727 | node.node) { | |
284d8f55 DG |
4728 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
4729 | if (ret < 0) { | |
d0b96690 | 4730 | goto error_unlock; |
487cf67c | 4731 | } |
36dc12cc | 4732 | } |
487cf67c DG |
4733 | } |
4734 | ||
d0b96690 DG |
4735 | pthread_mutex_unlock(&ua_sess->lock); |
4736 | ||
14fb1ebe | 4737 | if (usess->active) { |
421cb601 | 4738 | ret = ust_app_start_trace(usess, app); |
36dc12cc | 4739 | if (ret < 0) { |
36dc12cc DG |
4740 | goto error; |
4741 | } | |
4742 | ||
852d0037 | 4743 | DBG2("UST trace started for app pid %d", app->pid); |
36dc12cc | 4744 | } |
a9ad0c8f | 4745 | end: |
ffe60014 | 4746 | /* Everything went well at this point. */ |
ffe60014 DG |
4747 | return; |
4748 | ||
d0b96690 DG |
4749 | error_unlock: |
4750 | pthread_mutex_unlock(&ua_sess->lock); | |
487cf67c | 4751 | error: |
ffe60014 | 4752 | if (ua_sess) { |
d0b96690 | 4753 | destroy_app_session(app, ua_sess); |
ffe60014 | 4754 | } |
487cf67c DG |
4755 | return; |
4756 | } | |
55cc08a6 | 4757 | |
a9ad0c8f MD |
4758 | static |
4759 | void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app) | |
4760 | { | |
4761 | struct ust_app_session *ua_sess; | |
4762 | ||
4763 | ua_sess = lookup_session_by_app(usess, app); | |
4764 | if (ua_sess == NULL) { | |
4765 | return; | |
4766 | } | |
4767 | destroy_app_session(app, ua_sess); | |
4768 | } | |
4769 | ||
4770 | /* | |
4771 | * Add channels/events from UST global domain to registered apps at sock. | |
4772 | * | |
4773 | * Called with session lock held. | |
4774 | * Called with RCU read-side lock held. | |
4775 | */ | |
4776 | void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app) | |
4777 | { | |
4778 | assert(usess); | |
4779 | ||
4780 | DBG2("UST app global update for app sock %d for session id %" PRIu64, | |
4781 | app->sock, usess->id); | |
4782 | ||
4783 | if (!app->compatible) { | |
4784 | return; | |
4785 | } | |
4786 | ||
4787 | if (trace_ust_pid_tracker_lookup(usess, app->pid)) { | |
4788 | ust_app_global_create(usess, app); | |
4789 | } else { | |
4790 | ust_app_global_destroy(usess, app); | |
4791 | } | |
4792 | } | |
4793 | ||
4794 | /* | |
4795 | * Called with session lock held. | |
4796 | */ | |
4797 | void ust_app_global_update_all(struct ltt_ust_session *usess) | |
4798 | { | |
4799 | struct lttng_ht_iter iter; | |
4800 | struct ust_app *app; | |
4801 | ||
4802 | rcu_read_lock(); | |
4803 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
4804 | ust_app_global_update(usess, app); | |
4805 | } | |
4806 | rcu_read_unlock(); | |
4807 | } | |
4808 | ||
55cc08a6 DG |
4809 | /* |
4810 | * Add context to a specific channel for global UST domain. | |
4811 | */ | |
4812 | int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess, | |
4813 | struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx) | |
4814 | { | |
4815 | int ret = 0; | |
bec39940 DG |
4816 | struct lttng_ht_node_str *ua_chan_node; |
4817 | struct lttng_ht_iter iter, uiter; | |
55cc08a6 DG |
4818 | struct ust_app_channel *ua_chan = NULL; |
4819 | struct ust_app_session *ua_sess; | |
4820 | struct ust_app *app; | |
4821 | ||
4822 | rcu_read_lock(); | |
4823 | ||
852d0037 | 4824 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
e0c7ec2b DG |
4825 | if (!app->compatible) { |
4826 | /* | |
4827 | * TODO: In time, we should notice the caller of this error by | |
4828 | * telling him that this is a version error. | |
4829 | */ | |
4830 | continue; | |
4831 | } | |
55cc08a6 DG |
4832 | ua_sess = lookup_session_by_app(usess, app); |
4833 | if (ua_sess == NULL) { | |
4834 | continue; | |
4835 | } | |
4836 | ||
d0b96690 | 4837 | pthread_mutex_lock(&ua_sess->lock); |
d40a0cb1 MD |
4838 | |
4839 | if (ua_sess->deleted) { | |
4840 | pthread_mutex_unlock(&ua_sess->lock); | |
4841 | continue; | |
4842 | } | |
4843 | ||
55cc08a6 | 4844 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4845 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
4846 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
55cc08a6 | 4847 | if (ua_chan_node == NULL) { |
d0b96690 | 4848 | goto next_app; |
55cc08a6 DG |
4849 | } |
4850 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, | |
4851 | node); | |
55cc08a6 DG |
4852 | ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app); |
4853 | if (ret < 0) { | |
d0b96690 | 4854 | goto next_app; |
55cc08a6 | 4855 | } |
d0b96690 DG |
4856 | next_app: |
4857 | pthread_mutex_unlock(&ua_sess->lock); | |
55cc08a6 DG |
4858 | } |
4859 | ||
55cc08a6 DG |
4860 | rcu_read_unlock(); |
4861 | return ret; | |
4862 | } | |
4863 | ||
76d45b40 DG |
4864 | /* |
4865 | * Enable event for a channel from a UST session for a specific PID. | |
4866 | */ | |
4867 | int ust_app_enable_event_pid(struct ltt_ust_session *usess, | |
4868 | struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid) | |
4869 | { | |
4870 | int ret = 0; | |
bec39940 | 4871 | struct lttng_ht_iter iter; |
18eace3b | 4872 | struct lttng_ht_node_str *ua_chan_node; |
76d45b40 DG |
4873 | struct ust_app *app; |
4874 | struct ust_app_session *ua_sess; | |
4875 | struct ust_app_channel *ua_chan; | |
4876 | struct ust_app_event *ua_event; | |
4877 | ||
4878 | DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid); | |
4879 | ||
4880 | rcu_read_lock(); | |
4881 | ||
4882 | app = ust_app_find_by_pid(pid); | |
4883 | if (app == NULL) { | |
4884 | ERR("UST app enable event per PID %d not found", pid); | |
4885 | ret = -1; | |
d0b96690 | 4886 | goto end; |
76d45b40 DG |
4887 | } |
4888 | ||
e0c7ec2b DG |
4889 | if (!app->compatible) { |
4890 | ret = 0; | |
d0b96690 | 4891 | goto end; |
e0c7ec2b DG |
4892 | } |
4893 | ||
76d45b40 | 4894 | ua_sess = lookup_session_by_app(usess, app); |
c4a1715b DG |
4895 | if (!ua_sess) { |
4896 | /* The application has problem or is probably dead. */ | |
d0b96690 DG |
4897 | ret = 0; |
4898 | goto end; | |
c4a1715b | 4899 | } |
76d45b40 | 4900 | |
d0b96690 | 4901 | pthread_mutex_lock(&ua_sess->lock); |
d40a0cb1 MD |
4902 | |
4903 | if (ua_sess->deleted) { | |
4904 | ret = 0; | |
4905 | goto end_unlock; | |
4906 | } | |
4907 | ||
76d45b40 | 4908 | /* Lookup channel in the ust app session */ |
bec39940 DG |
4909 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
4910 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
76d45b40 DG |
4911 | /* If the channel is not found, there is a code flow error */ |
4912 | assert(ua_chan_node); | |
4913 | ||
4914 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
4915 | ||
18eace3b | 4916 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
39c5a3a7 | 4917 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 4918 | if (ua_event == NULL) { |
76d45b40 DG |
4919 | ret = create_ust_app_event(ua_sess, ua_chan, uevent, app); |
4920 | if (ret < 0) { | |
d0b96690 | 4921 | goto end_unlock; |
76d45b40 DG |
4922 | } |
4923 | } else { | |
76d45b40 DG |
4924 | ret = enable_ust_app_event(ua_sess, ua_event, app); |
4925 | if (ret < 0) { | |
d0b96690 | 4926 | goto end_unlock; |
76d45b40 DG |
4927 | } |
4928 | } | |
4929 | ||
d0b96690 DG |
4930 | end_unlock: |
4931 | pthread_mutex_unlock(&ua_sess->lock); | |
4932 | end: | |
76d45b40 DG |
4933 | rcu_read_unlock(); |
4934 | return ret; | |
4935 | } | |
7f79d3a1 | 4936 | |
4466912f DG |
4937 | /* |
4938 | * Calibrate registered applications. | |
4939 | */ | |
4940 | int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate) | |
4941 | { | |
4942 | int ret = 0; | |
4943 | struct lttng_ht_iter iter; | |
4944 | struct ust_app *app; | |
4945 | ||
4946 | rcu_read_lock(); | |
4947 | ||
852d0037 | 4948 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { |
4466912f DG |
4949 | if (!app->compatible) { |
4950 | /* | |
4951 | * TODO: In time, we should notice the caller of this error by | |
4952 | * telling him that this is a version error. | |
4953 | */ | |
4954 | continue; | |
4955 | } | |
4956 | ||
840cb59c | 4957 | health_code_update(); |
86acf0da | 4958 | |
39f0f159 | 4959 | pthread_mutex_lock(&app->sock_lock); |
852d0037 | 4960 | ret = ustctl_calibrate(app->sock, calibrate); |
39f0f159 | 4961 | pthread_mutex_unlock(&app->sock_lock); |
4466912f DG |
4962 | if (ret < 0) { |
4963 | switch (ret) { | |
4964 | case -ENOSYS: | |
4965 | /* Means that it's not implemented on the tracer side. */ | |
4966 | ret = 0; | |
4967 | break; | |
4968 | default: | |
4466912f | 4969 | DBG2("Calibrate app PID %d returned with error %d", |
852d0037 | 4970 | app->pid, ret); |
4466912f DG |
4971 | break; |
4972 | } | |
4973 | } | |
4974 | } | |
4975 | ||
4976 | DBG("UST app global domain calibration finished"); | |
4977 | ||
4978 | rcu_read_unlock(); | |
4979 | ||
840cb59c | 4980 | health_code_update(); |
86acf0da | 4981 | |
4466912f DG |
4982 | return ret; |
4983 | } | |
d0b96690 DG |
4984 | |
4985 | /* | |
4986 | * Receive registration and populate the given msg structure. | |
4987 | * | |
4988 | * On success return 0 else a negative value returned by the ustctl call. | |
4989 | */ | |
4990 | int ust_app_recv_registration(int sock, struct ust_register_msg *msg) | |
4991 | { | |
4992 | int ret; | |
4993 | uint32_t pid, ppid, uid, gid; | |
4994 | ||
4995 | assert(msg); | |
4996 | ||
4997 | ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor, | |
4998 | &pid, &ppid, &uid, &gid, | |
4999 | &msg->bits_per_long, | |
5000 | &msg->uint8_t_alignment, | |
5001 | &msg->uint16_t_alignment, | |
5002 | &msg->uint32_t_alignment, | |
5003 | &msg->uint64_t_alignment, | |
5004 | &msg->long_alignment, | |
5005 | &msg->byte_order, | |
5006 | msg->name); | |
5007 | if (ret < 0) { | |
5008 | switch (-ret) { | |
5009 | case EPIPE: | |
5010 | case ECONNRESET: | |
5011 | case LTTNG_UST_ERR_EXITING: | |
5012 | DBG3("UST app recv reg message failed. Application died"); | |
5013 | break; | |
5014 | case LTTNG_UST_ERR_UNSUP_MAJOR: | |
5015 | ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d", | |
5016 | msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION, | |
5017 | LTTNG_UST_ABI_MINOR_VERSION); | |
5018 | break; | |
5019 | default: | |
5020 | ERR("UST app recv reg message failed with ret %d", ret); | |
5021 | break; | |
5022 | } | |
5023 | goto error; | |
5024 | } | |
5025 | msg->pid = (pid_t) pid; | |
5026 | msg->ppid = (pid_t) ppid; | |
5027 | msg->uid = (uid_t) uid; | |
5028 | msg->gid = (gid_t) gid; | |
5029 | ||
5030 | error: | |
5031 | return ret; | |
5032 | } | |
5033 | ||
d88aee68 DG |
5034 | /* |
5035 | * Return a ust app channel object using the application object and the channel | |
5036 | * object descriptor has a key. If not found, NULL is returned. A RCU read side | |
5037 | * lock MUST be acquired before calling this function. | |
5038 | */ | |
d0b96690 DG |
5039 | static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, |
5040 | int objd) | |
5041 | { | |
5042 | struct lttng_ht_node_ulong *node; | |
5043 | struct lttng_ht_iter iter; | |
5044 | struct ust_app_channel *ua_chan = NULL; | |
5045 | ||
5046 | assert(app); | |
5047 | ||
5048 | lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter); | |
5049 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5050 | if (node == NULL) { | |
5051 | DBG2("UST app channel find by objd %d not found", objd); | |
5052 | goto error; | |
5053 | } | |
5054 | ||
5055 | ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node); | |
5056 | ||
5057 | error: | |
5058 | return ua_chan; | |
5059 | } | |
5060 | ||
d88aee68 DG |
5061 | /* |
5062 | * Reply to a register channel notification from an application on the notify | |
5063 | * socket. The channel metadata is also created. | |
5064 | * | |
5065 | * The session UST registry lock is acquired in this function. | |
5066 | * | |
5067 | * On success 0 is returned else a negative value. | |
5068 | */ | |
d0b96690 DG |
5069 | static int reply_ust_register_channel(int sock, int sobjd, int cobjd, |
5070 | size_t nr_fields, struct ustctl_field *fields) | |
5071 | { | |
5072 | int ret, ret_code = 0; | |
5073 | uint32_t chan_id, reg_count; | |
7972aab2 | 5074 | uint64_t chan_reg_key; |
d0b96690 DG |
5075 | enum ustctl_channel_header type; |
5076 | struct ust_app *app; | |
5077 | struct ust_app_channel *ua_chan; | |
5078 | struct ust_app_session *ua_sess; | |
7972aab2 | 5079 | struct ust_registry_session *registry; |
45893984 | 5080 | struct ust_registry_channel *chan_reg; |
d0b96690 DG |
5081 | |
5082 | rcu_read_lock(); | |
5083 | ||
5084 | /* Lookup application. If not found, there is a code flow error. */ | |
5085 | app = find_app_by_notify_sock(sock); | |
d88aee68 DG |
5086 | if (!app) { |
5087 | DBG("Application socket %d is being teardown. Abort event notify", | |
5088 | sock); | |
5089 | ret = 0; | |
d5d629b5 | 5090 | free(fields); |
d88aee68 DG |
5091 | goto error_rcu_unlock; |
5092 | } | |
d0b96690 | 5093 | |
4950b860 | 5094 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 5095 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 MD |
5096 | if (!ua_chan) { |
5097 | DBG("Application channel is being teardown. Abort event notify"); | |
5098 | ret = 0; | |
d5d629b5 | 5099 | free(fields); |
4950b860 MD |
5100 | goto error_rcu_unlock; |
5101 | } | |
5102 | ||
d0b96690 DG |
5103 | assert(ua_chan->session); |
5104 | ua_sess = ua_chan->session; | |
d0b96690 | 5105 | |
7972aab2 DG |
5106 | /* Get right session registry depending on the session buffer type. */ |
5107 | registry = get_session_registry(ua_sess); | |
5108 | assert(registry); | |
45893984 | 5109 | |
7972aab2 DG |
5110 | /* Depending on the buffer type, a different channel key is used. */ |
5111 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
5112 | chan_reg_key = ua_chan->tracing_channel_id; | |
d0b96690 | 5113 | } else { |
7972aab2 | 5114 | chan_reg_key = ua_chan->key; |
d0b96690 DG |
5115 | } |
5116 | ||
7972aab2 DG |
5117 | pthread_mutex_lock(®istry->lock); |
5118 | ||
5119 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); | |
5120 | assert(chan_reg); | |
5121 | ||
5122 | if (!chan_reg->register_done) { | |
5123 | reg_count = ust_registry_get_event_count(chan_reg); | |
5124 | if (reg_count < 31) { | |
5125 | type = USTCTL_CHANNEL_HEADER_COMPACT; | |
5126 | } else { | |
5127 | type = USTCTL_CHANNEL_HEADER_LARGE; | |
5128 | } | |
5129 | ||
5130 | chan_reg->nr_ctx_fields = nr_fields; | |
5131 | chan_reg->ctx_fields = fields; | |
5132 | chan_reg->header_type = type; | |
d0b96690 | 5133 | } else { |
7972aab2 DG |
5134 | /* Get current already assigned values. */ |
5135 | type = chan_reg->header_type; | |
d5d629b5 DG |
5136 | free(fields); |
5137 | /* Set to NULL so the error path does not do a double free. */ | |
5138 | fields = NULL; | |
d0b96690 | 5139 | } |
7972aab2 DG |
5140 | /* Channel id is set during the object creation. */ |
5141 | chan_id = chan_reg->chan_id; | |
d0b96690 DG |
5142 | |
5143 | /* Append to metadata */ | |
7972aab2 DG |
5144 | if (!chan_reg->metadata_dumped) { |
5145 | ret_code = ust_metadata_channel_statedump(registry, chan_reg); | |
d0b96690 DG |
5146 | if (ret_code) { |
5147 | ERR("Error appending channel metadata (errno = %d)", ret_code); | |
5148 | goto reply; | |
5149 | } | |
5150 | } | |
5151 | ||
5152 | reply: | |
7972aab2 DG |
5153 | DBG3("UST app replying to register channel key %" PRIu64 |
5154 | " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type, | |
5155 | ret_code); | |
d0b96690 DG |
5156 | |
5157 | ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code); | |
5158 | if (ret < 0) { | |
5159 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
5160 | ERR("UST app reply channel failed with ret %d", ret); | |
5161 | } else { | |
5162 | DBG3("UST app reply channel failed. Application died"); | |
5163 | } | |
5164 | goto error; | |
5165 | } | |
5166 | ||
7972aab2 DG |
5167 | /* This channel registry registration is completed. */ |
5168 | chan_reg->register_done = 1; | |
5169 | ||
d0b96690 | 5170 | error: |
7972aab2 | 5171 | pthread_mutex_unlock(®istry->lock); |
d88aee68 | 5172 | error_rcu_unlock: |
d0b96690 | 5173 | rcu_read_unlock(); |
d5d629b5 DG |
5174 | if (ret) { |
5175 | free(fields); | |
5176 | } | |
d0b96690 DG |
5177 | return ret; |
5178 | } | |
5179 | ||
d88aee68 DG |
5180 | /* |
5181 | * Add event to the UST channel registry. When the event is added to the | |
5182 | * registry, the metadata is also created. Once done, this replies to the | |
5183 | * application with the appropriate error code. | |
5184 | * | |
5185 | * The session UST registry lock is acquired in the function. | |
5186 | * | |
5187 | * On success 0 is returned else a negative value. | |
5188 | */ | |
d0b96690 | 5189 | static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name, |
79a065a0 PP |
5190 | char *sig, size_t nr_fields, struct ustctl_field *fields, |
5191 | int loglevel_value, char *model_emf_uri) | |
d0b96690 DG |
5192 | { |
5193 | int ret, ret_code; | |
5194 | uint32_t event_id = 0; | |
7972aab2 | 5195 | uint64_t chan_reg_key; |
d0b96690 DG |
5196 | struct ust_app *app; |
5197 | struct ust_app_channel *ua_chan; | |
5198 | struct ust_app_session *ua_sess; | |
7972aab2 | 5199 | struct ust_registry_session *registry; |
d0b96690 DG |
5200 | |
5201 | rcu_read_lock(); | |
5202 | ||
5203 | /* Lookup application. If not found, there is a code flow error. */ | |
5204 | app = find_app_by_notify_sock(sock); | |
d88aee68 DG |
5205 | if (!app) { |
5206 | DBG("Application socket %d is being teardown. Abort event notify", | |
5207 | sock); | |
5208 | ret = 0; | |
d5d629b5 DG |
5209 | free(sig); |
5210 | free(fields); | |
5211 | free(model_emf_uri); | |
d88aee68 DG |
5212 | goto error_rcu_unlock; |
5213 | } | |
d0b96690 | 5214 | |
4950b860 | 5215 | /* Lookup channel by UST object descriptor. */ |
d0b96690 | 5216 | ua_chan = find_channel_by_objd(app, cobjd); |
4950b860 MD |
5217 | if (!ua_chan) { |
5218 | DBG("Application channel is being teardown. Abort event notify"); | |
5219 | ret = 0; | |
d5d629b5 DG |
5220 | free(sig); |
5221 | free(fields); | |
5222 | free(model_emf_uri); | |
4950b860 MD |
5223 | goto error_rcu_unlock; |
5224 | } | |
5225 | ||
d0b96690 DG |
5226 | assert(ua_chan->session); |
5227 | ua_sess = ua_chan->session; | |
5228 | ||
7972aab2 DG |
5229 | registry = get_session_registry(ua_sess); |
5230 | assert(registry); | |
5231 | ||
5232 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
5233 | chan_reg_key = ua_chan->tracing_channel_id; | |
5234 | } else { | |
5235 | chan_reg_key = ua_chan->key; | |
5236 | } | |
5237 | ||
5238 | pthread_mutex_lock(®istry->lock); | |
d0b96690 | 5239 | |
d5d629b5 DG |
5240 | /* |
5241 | * From this point on, this call acquires the ownership of the sig, fields | |
5242 | * and model_emf_uri meaning any free are done inside it if needed. These | |
5243 | * three variables MUST NOT be read/write after this. | |
5244 | */ | |
7972aab2 | 5245 | ret_code = ust_registry_create_event(registry, chan_reg_key, |
79a065a0 PP |
5246 | sobjd, cobjd, name, sig, nr_fields, fields, |
5247 | loglevel_value, model_emf_uri, ua_sess->buffer_type, | |
5248 | &event_id, app); | |
d0b96690 DG |
5249 | |
5250 | /* | |
5251 | * The return value is returned to ustctl so in case of an error, the | |
5252 | * application can be notified. In case of an error, it's important not to | |
5253 | * return a negative error or else the application will get closed. | |
5254 | */ | |
5255 | ret = ustctl_reply_register_event(sock, event_id, ret_code); | |
5256 | if (ret < 0) { | |
5257 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
5258 | ERR("UST app reply event failed with ret %d", ret); | |
5259 | } else { | |
5260 | DBG3("UST app reply event failed. Application died"); | |
5261 | } | |
5262 | /* | |
5263 | * No need to wipe the create event since the application socket will | |
5264 | * get close on error hence cleaning up everything by itself. | |
5265 | */ | |
5266 | goto error; | |
5267 | } | |
5268 | ||
7972aab2 DG |
5269 | DBG3("UST registry event %s with id %" PRId32 " added successfully", |
5270 | name, event_id); | |
d88aee68 | 5271 | |
d0b96690 | 5272 | error: |
7972aab2 | 5273 | pthread_mutex_unlock(®istry->lock); |
d88aee68 | 5274 | error_rcu_unlock: |
d0b96690 DG |
5275 | rcu_read_unlock(); |
5276 | return ret; | |
5277 | } | |
5278 | ||
d88aee68 DG |
5279 | /* |
5280 | * Handle application notification through the given notify socket. | |
5281 | * | |
5282 | * Return 0 on success or else a negative value. | |
5283 | */ | |
d0b96690 DG |
5284 | int ust_app_recv_notify(int sock) |
5285 | { | |
5286 | int ret; | |
5287 | enum ustctl_notify_cmd cmd; | |
5288 | ||
5289 | DBG3("UST app receiving notify from sock %d", sock); | |
5290 | ||
5291 | ret = ustctl_recv_notify(sock, &cmd); | |
5292 | if (ret < 0) { | |
5293 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
5294 | ERR("UST app recv notify failed with ret %d", ret); | |
5295 | } else { | |
5296 | DBG3("UST app recv notify failed. Application died"); | |
5297 | } | |
5298 | goto error; | |
5299 | } | |
5300 | ||
5301 | switch (cmd) { | |
5302 | case USTCTL_NOTIFY_CMD_EVENT: | |
5303 | { | |
79a065a0 | 5304 | int sobjd, cobjd, loglevel_value; |
d0b96690 DG |
5305 | char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri; |
5306 | size_t nr_fields; | |
5307 | struct ustctl_field *fields; | |
5308 | ||
5309 | DBG2("UST app ustctl register event received"); | |
5310 | ||
79a065a0 PP |
5311 | ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, |
5312 | &loglevel_value, &sig, &nr_fields, &fields, | |
5313 | &model_emf_uri); | |
d0b96690 DG |
5314 | if (ret < 0) { |
5315 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
5316 | ERR("UST app recv event failed with ret %d", ret); | |
5317 | } else { | |
5318 | DBG3("UST app recv event failed. Application died"); | |
5319 | } | |
5320 | goto error; | |
5321 | } | |
5322 | ||
d5d629b5 DG |
5323 | /* |
5324 | * Add event to the UST registry coming from the notify socket. This | |
5325 | * call will free if needed the sig, fields and model_emf_uri. This | |
5326 | * code path loses the ownsership of these variables and transfer them | |
5327 | * to the this function. | |
5328 | */ | |
d0b96690 | 5329 | ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields, |
79a065a0 | 5330 | fields, loglevel_value, model_emf_uri); |
d0b96690 DG |
5331 | if (ret < 0) { |
5332 | goto error; | |
5333 | } | |
5334 | ||
5335 | break; | |
5336 | } | |
5337 | case USTCTL_NOTIFY_CMD_CHANNEL: | |
5338 | { | |
5339 | int sobjd, cobjd; | |
5340 | size_t nr_fields; | |
5341 | struct ustctl_field *fields; | |
5342 | ||
5343 | DBG2("UST app ustctl register channel received"); | |
5344 | ||
5345 | ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields, | |
5346 | &fields); | |
5347 | if (ret < 0) { | |
5348 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
5349 | ERR("UST app recv channel failed with ret %d", ret); | |
5350 | } else { | |
5351 | DBG3("UST app recv channel failed. Application died"); | |
5352 | } | |
5353 | goto error; | |
5354 | } | |
5355 | ||
d5d629b5 DG |
5356 | /* |
5357 | * The fields ownership are transfered to this function call meaning | |
5358 | * that if needed it will be freed. After this, it's invalid to access | |
5359 | * fields or clean it up. | |
5360 | */ | |
d0b96690 DG |
5361 | ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields, |
5362 | fields); | |
5363 | if (ret < 0) { | |
5364 | goto error; | |
5365 | } | |
5366 | ||
5367 | break; | |
5368 | } | |
5369 | default: | |
5370 | /* Should NEVER happen. */ | |
5371 | assert(0); | |
5372 | } | |
5373 | ||
5374 | error: | |
5375 | return ret; | |
5376 | } | |
d88aee68 DG |
5377 | |
5378 | /* | |
5379 | * Once the notify socket hangs up, this is called. First, it tries to find the | |
5380 | * corresponding application. On failure, the call_rcu to close the socket is | |
5381 | * executed. If an application is found, it tries to delete it from the notify | |
5382 | * socket hash table. Whathever the result, it proceeds to the call_rcu. | |
5383 | * | |
5384 | * Note that an object needs to be allocated here so on ENOMEM failure, the | |
5385 | * call RCU is not done but the rest of the cleanup is. | |
5386 | */ | |
5387 | void ust_app_notify_sock_unregister(int sock) | |
5388 | { | |
5389 | int err_enomem = 0; | |
5390 | struct lttng_ht_iter iter; | |
5391 | struct ust_app *app; | |
5392 | struct ust_app_notify_sock_obj *obj; | |
5393 | ||
5394 | assert(sock >= 0); | |
5395 | ||
5396 | rcu_read_lock(); | |
5397 | ||
5398 | obj = zmalloc(sizeof(*obj)); | |
5399 | if (!obj) { | |
5400 | /* | |
5401 | * An ENOMEM is kind of uncool. If this strikes we continue the | |
5402 | * procedure but the call_rcu will not be called. In this case, we | |
5403 | * accept the fd leak rather than possibly creating an unsynchronized | |
5404 | * state between threads. | |
5405 | * | |
5406 | * TODO: The notify object should be created once the notify socket is | |
5407 | * registered and stored independantely from the ust app object. The | |
5408 | * tricky part is to synchronize the teardown of the application and | |
5409 | * this notify object. Let's keep that in mind so we can avoid this | |
5410 | * kind of shenanigans with ENOMEM in the teardown path. | |
5411 | */ | |
5412 | err_enomem = 1; | |
5413 | } else { | |
5414 | obj->fd = sock; | |
5415 | } | |
5416 | ||
5417 | DBG("UST app notify socket unregister %d", sock); | |
5418 | ||
5419 | /* | |
5420 | * Lookup application by notify socket. If this fails, this means that the | |
5421 | * hash table delete has already been done by the application | |
5422 | * unregistration process so we can safely close the notify socket in a | |
5423 | * call RCU. | |
5424 | */ | |
5425 | app = find_app_by_notify_sock(sock); | |
5426 | if (!app) { | |
5427 | goto close_socket; | |
5428 | } | |
5429 | ||
5430 | iter.iter.node = &app->notify_sock_n.node; | |
5431 | ||
5432 | /* | |
5433 | * Whatever happens here either we fail or succeed, in both cases we have | |
5434 | * to close the socket after a grace period to continue to the call RCU | |
5435 | * here. If the deletion is successful, the application is not visible | |
5436 | * anymore by other threads and is it fails it means that it was already | |
5437 | * deleted from the hash table so either way we just have to close the | |
5438 | * socket. | |
5439 | */ | |
5440 | (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter); | |
5441 | ||
5442 | close_socket: | |
5443 | rcu_read_unlock(); | |
5444 | ||
5445 | /* | |
5446 | * Close socket after a grace period to avoid for the socket to be reused | |
5447 | * before the application object is freed creating potential race between | |
5448 | * threads trying to add unique in the global hash table. | |
5449 | */ | |
5450 | if (!err_enomem) { | |
5451 | call_rcu(&obj->head, close_notify_sock_rcu); | |
5452 | } | |
5453 | } | |
f45e313d DG |
5454 | |
5455 | /* | |
5456 | * Destroy a ust app data structure and free its memory. | |
5457 | */ | |
5458 | void ust_app_destroy(struct ust_app *app) | |
5459 | { | |
5460 | if (!app) { | |
5461 | return; | |
5462 | } | |
5463 | ||
5464 | call_rcu(&app->pid_n.head, delete_ust_app_rcu); | |
5465 | } | |
6dc3064a DG |
5466 | |
5467 | /* | |
5468 | * Take a snapshot for a given UST session. The snapshot is sent to the given | |
5469 | * output. | |
5470 | * | |
5471 | * Return 0 on success or else a negative value. | |
5472 | */ | |
5473 | int ust_app_snapshot_record(struct ltt_ust_session *usess, | |
d07ceecd MD |
5474 | struct snapshot_output *output, int wait, |
5475 | uint64_t nb_packets_per_stream) | |
6dc3064a DG |
5476 | { |
5477 | int ret = 0; | |
7badf927 | 5478 | unsigned int snapshot_done = 0; |
6dc3064a DG |
5479 | struct lttng_ht_iter iter; |
5480 | struct ust_app *app; | |
af706bb7 | 5481 | char pathname[PATH_MAX]; |
6dc3064a DG |
5482 | |
5483 | assert(usess); | |
5484 | assert(output); | |
5485 | ||
5486 | rcu_read_lock(); | |
5487 | ||
8c924c7b MD |
5488 | switch (usess->buffer_type) { |
5489 | case LTTNG_BUFFER_PER_UID: | |
5490 | { | |
5491 | struct buffer_reg_uid *reg; | |
6dc3064a | 5492 | |
8c924c7b MD |
5493 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { |
5494 | struct buffer_reg_channel *reg_chan; | |
5495 | struct consumer_socket *socket; | |
6dc3064a | 5496 | |
8c924c7b MD |
5497 | /* Get consumer socket to use to push the metadata.*/ |
5498 | socket = consumer_find_socket_by_bitness(reg->bits_per_long, | |
5499 | usess->consumer); | |
5500 | if (!socket) { | |
5501 | ret = -EINVAL; | |
5502 | goto error; | |
5503 | } | |
6dc3064a | 5504 | |
8c924c7b MD |
5505 | memset(pathname, 0, sizeof(pathname)); |
5506 | ret = snprintf(pathname, sizeof(pathname), | |
5507 | DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH, | |
5508 | reg->uid, reg->bits_per_long); | |
5509 | if (ret < 0) { | |
5510 | PERROR("snprintf snapshot path"); | |
5511 | goto error; | |
5512 | } | |
5513 | ||
5514 | /* Add the UST default trace dir to path. */ | |
5515 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, | |
5516 | reg_chan, node.node) { | |
68808f4e DG |
5517 | ret = consumer_snapshot_channel(socket, reg_chan->consumer_key, |
5518 | output, 0, usess->uid, usess->gid, pathname, wait, | |
d07ceecd | 5519 | nb_packets_per_stream); |
8c924c7b MD |
5520 | if (ret < 0) { |
5521 | goto error; | |
5522 | } | |
5523 | } | |
68808f4e DG |
5524 | ret = consumer_snapshot_channel(socket, |
5525 | reg->registry->reg.ust->metadata_key, output, 1, | |
d07ceecd | 5526 | usess->uid, usess->gid, pathname, wait, 0); |
8c924c7b MD |
5527 | if (ret < 0) { |
5528 | goto error; | |
5529 | } | |
7badf927 | 5530 | snapshot_done = 1; |
af706bb7 | 5531 | } |
8c924c7b MD |
5532 | break; |
5533 | } | |
5534 | case LTTNG_BUFFER_PER_PID: | |
5535 | { | |
5536 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5537 | struct consumer_socket *socket; | |
5538 | struct lttng_ht_iter chan_iter; | |
5539 | struct ust_app_channel *ua_chan; | |
5540 | struct ust_app_session *ua_sess; | |
5541 | struct ust_registry_session *registry; | |
5542 | ||
5543 | ua_sess = lookup_session_by_app(usess, app); | |
5544 | if (!ua_sess) { | |
5545 | /* Session not associated with this app. */ | |
5546 | continue; | |
5547 | } | |
af706bb7 | 5548 | |
8c924c7b MD |
5549 | /* Get the right consumer socket for the application. */ |
5550 | socket = consumer_find_socket_by_bitness(app->bits_per_long, | |
5551 | output->consumer); | |
5552 | if (!socket) { | |
5c786ded | 5553 | ret = -EINVAL; |
5c786ded JD |
5554 | goto error; |
5555 | } | |
5556 | ||
8c924c7b MD |
5557 | /* Add the UST default trace dir to path. */ |
5558 | memset(pathname, 0, sizeof(pathname)); | |
5559 | ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s", | |
5560 | ua_sess->path); | |
6dc3064a | 5561 | if (ret < 0) { |
8c924c7b | 5562 | PERROR("snprintf snapshot path"); |
6dc3064a DG |
5563 | goto error; |
5564 | } | |
6dc3064a | 5565 | |
8c924c7b MD |
5566 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, |
5567 | ua_chan, node.node) { | |
68808f4e DG |
5568 | ret = consumer_snapshot_channel(socket, ua_chan->key, output, |
5569 | 0, ua_sess->euid, ua_sess->egid, pathname, wait, | |
d07ceecd | 5570 | nb_packets_per_stream); |
8c924c7b MD |
5571 | if (ret < 0) { |
5572 | goto error; | |
5573 | } | |
5574 | } | |
5575 | ||
5576 | registry = get_session_registry(ua_sess); | |
5577 | assert(registry); | |
5578 | ret = consumer_snapshot_channel(socket, registry->metadata_key, output, | |
d07ceecd | 5579 | 1, ua_sess->euid, ua_sess->egid, pathname, wait, 0); |
8c924c7b MD |
5580 | if (ret < 0) { |
5581 | goto error; | |
5582 | } | |
7badf927 | 5583 | snapshot_done = 1; |
8c924c7b MD |
5584 | } |
5585 | break; | |
5586 | } | |
5587 | default: | |
5588 | assert(0); | |
5589 | break; | |
6dc3064a DG |
5590 | } |
5591 | ||
7badf927 DG |
5592 | if (!snapshot_done) { |
5593 | /* | |
5594 | * If no snapshot was made and we are not in the error path, this means | |
5595 | * that there are no buffers thus no (prior) application to snapshot | |
5596 | * data from so we have simply NO data. | |
5597 | */ | |
5598 | ret = -ENODATA; | |
5599 | } | |
5600 | ||
6dc3064a DG |
5601 | error: |
5602 | rcu_read_unlock(); | |
5603 | return ret; | |
5604 | } | |
5c786ded JD |
5605 | |
5606 | /* | |
d07ceecd | 5607 | * Return the size taken by one more packet per stream. |
5c786ded | 5608 | */ |
d07ceecd MD |
5609 | uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess, |
5610 | uint64_t cur_nr_packets) | |
5c786ded | 5611 | { |
d07ceecd | 5612 | uint64_t tot_size = 0; |
5c786ded JD |
5613 | struct ust_app *app; |
5614 | struct lttng_ht_iter iter; | |
5615 | ||
5616 | assert(usess); | |
5617 | ||
5618 | switch (usess->buffer_type) { | |
5619 | case LTTNG_BUFFER_PER_UID: | |
5620 | { | |
5621 | struct buffer_reg_uid *reg; | |
5622 | ||
5623 | cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) { | |
5624 | struct buffer_reg_channel *reg_chan; | |
5625 | ||
b7064eaa | 5626 | rcu_read_lock(); |
5c786ded JD |
5627 | cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter, |
5628 | reg_chan, node.node) { | |
d07ceecd MD |
5629 | if (cur_nr_packets >= reg_chan->num_subbuf) { |
5630 | /* | |
5631 | * Don't take channel into account if we | |
5632 | * already grab all its packets. | |
5633 | */ | |
5634 | continue; | |
5635 | } | |
5636 | tot_size += reg_chan->subbuf_size * reg_chan->stream_count; | |
5c786ded | 5637 | } |
b7064eaa | 5638 | rcu_read_unlock(); |
5c786ded JD |
5639 | } |
5640 | break; | |
5641 | } | |
5642 | case LTTNG_BUFFER_PER_PID: | |
5643 | { | |
5644 | rcu_read_lock(); | |
5645 | cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) { | |
5646 | struct ust_app_channel *ua_chan; | |
5647 | struct ust_app_session *ua_sess; | |
5648 | struct lttng_ht_iter chan_iter; | |
5649 | ||
5650 | ua_sess = lookup_session_by_app(usess, app); | |
5651 | if (!ua_sess) { | |
5652 | /* Session not associated with this app. */ | |
5653 | continue; | |
5654 | } | |
5655 | ||
5656 | cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter, | |
5657 | ua_chan, node.node) { | |
d07ceecd MD |
5658 | if (cur_nr_packets >= ua_chan->attr.num_subbuf) { |
5659 | /* | |
5660 | * Don't take channel into account if we | |
5661 | * already grab all its packets. | |
5662 | */ | |
5663 | continue; | |
5664 | } | |
5665 | tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count; | |
5c786ded JD |
5666 | } |
5667 | } | |
5668 | rcu_read_unlock(); | |
5669 | break; | |
5670 | } | |
5671 | default: | |
5672 | assert(0); | |
5673 | break; | |
5674 | } | |
5675 | ||
d07ceecd | 5676 | return tot_size; |
5c786ded | 5677 | } |