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