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