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