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