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