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