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