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