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