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