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