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