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