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