Move ust channel registry inside session 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
18#define _GNU_SOURCE
19#include <errno.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
099e26bd 23#include <string.h>
aba8e916
DG
24#include <sys/stat.h>
25#include <sys/types.h>
099e26bd 26#include <unistd.h>
0df502fd 27#include <urcu/compiler.h>
fb54cdbf 28#include <lttng/ust-error.h>
bec39940 29
990570ed 30#include <common/common.h>
86acf0da 31#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 32
86acf0da
DG
33#include "fd-limit.h"
34#include "health.h"
56fff090 35#include "ust-app.h"
48842b30 36#include "ust-consumer.h"
d80a6244
DG
37#include "ust-ctl.h"
38
ffe60014
DG
39/* Next available channel key. */
40static unsigned long next_channel_key;
41
42/*
43 * Return the atomically incremented value of next_channel_key.
44 */
45static inline unsigned long get_next_channel_key(void)
46{
47 return uatomic_add_return(&next_channel_key, 1);
48}
49
50/*
51 * Return the consumer socket from the given consumer output with the right
52 * bitness. On error, returns NULL.
53 *
54 * The caller MUST acquire a rcu read side lock and keep it until the socket
55 * object reference is not needed anymore.
56 */
57static struct consumer_socket *find_consumer_socket_by_bitness(int bits,
58 struct consumer_output *consumer)
59{
60 int consumer_fd;
61 struct consumer_socket *socket = NULL;
62
63 switch (bits) {
64 case 64:
65 consumer_fd = uatomic_read(&ust_consumerd64_fd);
66 break;
67 case 32:
68 consumer_fd = uatomic_read(&ust_consumerd32_fd);
69 break;
70 default:
71 assert(0);
72 goto end;
73 }
74
75 socket = consumer_find_socket(consumer_fd, consumer);
d88aee68
DG
76 if (!socket) {
77 ERR("Consumer socket fd %d not found in consumer obj %p",
78 consumer_fd, consumer);
79 }
ffe60014
DG
80
81end:
82 return socket;
83}
84
025faf73
DG
85/*
86 * Match function for the hash table lookup.
87 *
88 * It matches an ust app event based on three attributes which are the event
89 * name, the filter bytecode and the loglevel.
90 */
18eace3b
DG
91static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
92{
93 struct ust_app_event *event;
94 const struct ust_app_ht_key *key;
95
96 assert(node);
97 assert(_key);
98
99 event = caa_container_of(node, struct ust_app_event, node.node);
100 key = _key;
101
102 /* Match the 3 elements of the key: name, filter and loglevel. */
103
104 /* Event name */
105 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
106 goto no_match;
107 }
108
109 /* Event loglevel. */
110 if (event->attr.loglevel != key->loglevel) {
025faf73
DG
111 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
112 && key->loglevel == 0 && event->attr.loglevel == -1) {
113 /*
114 * Match is accepted. This is because on event creation, the
115 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
116 * -1 are accepted for this loglevel type since 0 is the one set by
117 * the API when receiving an enable event.
118 */
119 } else {
120 goto no_match;
121 }
18eace3b
DG
122 }
123
124 /* One of the filters is NULL, fail. */
125 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
126 goto no_match;
127 }
128
025faf73
DG
129 if (key->filter && event->filter) {
130 /* Both filters exists, check length followed by the bytecode. */
131 if (event->filter->len != key->filter->len ||
132 memcmp(event->filter->data, key->filter->data,
133 event->filter->len) != 0) {
134 goto no_match;
135 }
18eace3b
DG
136 }
137
025faf73 138 /* Match. */
18eace3b
DG
139 return 1;
140
141no_match:
142 return 0;
18eace3b
DG
143}
144
025faf73
DG
145/*
146 * Unique add of an ust app event in the given ht. This uses the custom
147 * ht_match_ust_app_event match function and the event name as hash.
148 */
d0b96690 149static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
18eace3b
DG
150 struct ust_app_event *event)
151{
152 struct cds_lfht_node *node_ptr;
153 struct ust_app_ht_key key;
d0b96690 154 struct lttng_ht *ht;
18eace3b 155
d0b96690
DG
156 assert(ua_chan);
157 assert(ua_chan->events);
18eace3b
DG
158 assert(event);
159
d0b96690 160 ht = ua_chan->events;
18eace3b
DG
161 key.name = event->attr.name;
162 key.filter = event->filter;
163 key.loglevel = event->attr.loglevel;
164
165 node_ptr = cds_lfht_add_unique(ht->ht,
166 ht->hash_fct(event->node.key, lttng_ht_seed),
167 ht_match_ust_app_event, &key, &event->node.node);
168 assert(node_ptr == &event->node.node);
169}
170
d88aee68
DG
171/*
172 * Close the notify socket from the given RCU head object. This MUST be called
173 * through a call_rcu().
174 */
175static void close_notify_sock_rcu(struct rcu_head *head)
176{
177 int ret;
178 struct ust_app_notify_sock_obj *obj =
179 caa_container_of(head, struct ust_app_notify_sock_obj, head);
180
181 /* Must have a valid fd here. */
182 assert(obj->fd >= 0);
183
184 ret = close(obj->fd);
185 if (ret) {
186 ERR("close notify sock %d RCU", obj->fd);
187 }
188 lttng_fd_put(LTTNG_FD_APPS, 1);
189
190 free(obj);
191}
192
55cc08a6
DG
193/*
194 * Delete ust context safely. RCU read lock must be held before calling
195 * this function.
196 */
197static
198void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
199{
ffe60014
DG
200 int ret;
201
202 assert(ua_ctx);
203
55cc08a6 204 if (ua_ctx->obj) {
ffe60014 205 ret = ustctl_release_object(sock, ua_ctx->obj);
d0b96690
DG
206 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
207 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
208 sock, ua_ctx->obj->handle, ret);
ffe60014 209 }
55cc08a6
DG
210 free(ua_ctx->obj);
211 }
212 free(ua_ctx);
213}
214
d80a6244
DG
215/*
216 * Delete ust app event safely. RCU read lock must be held before calling
217 * this function.
218 */
8b366481
DG
219static
220void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
d80a6244 221{
ffe60014
DG
222 int ret;
223
224 assert(ua_event);
225
53a80697 226 free(ua_event->filter);
d80a6244 227
edb67388 228 if (ua_event->obj != NULL) {
ffe60014
DG
229 ret = ustctl_release_object(sock, ua_event->obj);
230 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
231 ERR("UST app sock %d release event obj failed with ret %d",
232 sock, ret);
233 }
edb67388
DG
234 free(ua_event->obj);
235 }
d80a6244
DG
236 free(ua_event);
237}
238
239/*
240 * Delete ust app stream safely. RCU read lock must be held before calling
241 * this function.
242 */
8b366481 243static
030a66fa 244void delete_ust_app_stream(int sock, struct ust_app_stream *stream)
d80a6244 245{
ffe60014
DG
246 int ret;
247
248 assert(stream);
249
8b366481 250 if (stream->obj) {
ffe60014
DG
251 ret = ustctl_release_object(sock, stream->obj);
252 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
253 ERR("UST app sock %d release stream obj failed with ret %d",
254 sock, ret);
255 }
4063050c 256 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
257 free(stream->obj);
258 }
84cd17c6 259 free(stream);
d80a6244
DG
260}
261
262/*
263 * Delete ust app channel safely. RCU read lock must be held before calling
264 * this function.
265 */
8b366481 266static
d0b96690
DG
267void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
268 struct ust_app *app)
d80a6244
DG
269{
270 int ret;
bec39940 271 struct lttng_ht_iter iter;
d80a6244 272 struct ust_app_event *ua_event;
55cc08a6 273 struct ust_app_ctx *ua_ctx;
030a66fa 274 struct ust_app_stream *stream, *stmp;
d80a6244 275
ffe60014
DG
276 assert(ua_chan);
277
278 DBG3("UST app deleting channel %s", ua_chan->name);
279
55cc08a6 280 /* Wipe stream */
d80a6244 281 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 282 cds_list_del(&stream->list);
d80a6244
DG
283 delete_ust_app_stream(sock, stream);
284 }
285
55cc08a6 286 /* Wipe context */
bec39940
DG
287 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
288 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6
DG
289 assert(!ret);
290 delete_ust_app_ctx(sock, ua_ctx);
291 }
bec39940 292 lttng_ht_destroy(ua_chan->ctx);
d80a6244 293
55cc08a6 294 /* Wipe events */
bec39940
DG
295 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
296 node.node) {
297 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 298 assert(!ret);
d80a6244
DG
299 delete_ust_app_event(sock, ua_event);
300 }
bec39940 301 lttng_ht_destroy(ua_chan->events);
edb67388 302
45893984
DG
303 /* Wipe and free registry from session registry. */
304 ust_registry_channel_del_free(ua_chan->session->registry, ua_chan->key);
d0b96690 305
edb67388 306 if (ua_chan->obj != NULL) {
d0b96690
DG
307 /* Remove channel from application UST object descriptor. */
308 iter.iter.node = &ua_chan->ust_objd_node.node;
309 lttng_ht_del(app->ust_objd, &iter);
ffe60014
DG
310 ret = ustctl_release_object(sock, ua_chan->obj);
311 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
312 ERR("UST app sock %d release channel obj failed with ret %d",
313 sock, ret);
314 }
4063050c 315 lttng_fd_put(LTTNG_FD_APPS, 2);
edb67388
DG
316 free(ua_chan->obj);
317 }
84cd17c6 318 free(ua_chan);
d80a6244
DG
319}
320
d88aee68
DG
321/*
322 * For a given application and session, push metadata to consumer. The session
323 * lock MUST be acquired here before calling this.
324 *
325 * Return 0 on success else a negative error.
326 */
327static int push_metadata(struct ust_app *app, struct ust_app_session *ua_sess)
328{
329 int ret;
330 char *metadata_str = NULL;
331 size_t len, offset;
332 struct consumer_socket *socket;
333
334 assert(app);
335 assert(ua_sess);
336
337 if (!ua_sess->consumer || !ua_sess->metadata) {
338 /* No consumer means no stream associated so just return gracefully. */
339 ret = 0;
340 goto end;
341 }
342
343 rcu_read_lock();
344
345 /* Get consumer socket to use to push the metadata.*/
346 socket = find_consumer_socket_by_bitness(app->bits_per_long,
347 ua_sess->consumer);
348 if (!socket) {
349 ret = -1;
350 goto error_rcu_unlock;
351 }
352
353 /*
354 * TODO: Currently, we hold the socket lock around sampling of the next
355 * metadata segment to ensure we send metadata over the consumer socket in
356 * the correct order. This makes the registry lock nest inside the socket
357 * lock.
358 *
359 * Please note that this is a temporary measure: we should move this lock
360 * back into ust_consumer_push_metadata() when the consumer gets the
361 * ability to reorder the metadata it receives.
362 */
363 pthread_mutex_lock(socket->lock);
45893984 364 pthread_mutex_lock(&ua_sess->registry->lock);
d88aee68 365
45893984
DG
366 offset = ua_sess->registry->metadata_len_sent;
367 len = ua_sess->registry->metadata_len -
368 ua_sess->registry->metadata_len_sent;
d88aee68
DG
369 if (len == 0) {
370 DBG3("No metadata to push for session id %d", ua_sess->id);
371 ret = 0;
372 goto error_reg_unlock;
373 }
374 assert(len > 0);
375
376 /* Allocate only what we have to send. */
377 metadata_str = zmalloc(len);
378 if (!metadata_str) {
379 PERROR("zmalloc ust app metadata string");
380 ret = -ENOMEM;
381 goto error_reg_unlock;
382 }
383 /* Copy what we haven't send out. */
45893984 384 memcpy(metadata_str, ua_sess->registry->metadata + offset, len);
d88aee68 385
45893984 386 pthread_mutex_unlock(&ua_sess->registry->lock);
d88aee68
DG
387
388 ret = ust_consumer_push_metadata(socket, ua_sess, metadata_str, len,
389 offset);
390 if (ret < 0) {
391 pthread_mutex_unlock(socket->lock);
392 goto error_rcu_unlock;
393 }
394
395 /* Update len sent of the registry. */
45893984
DG
396 pthread_mutex_lock(&ua_sess->registry->lock);
397 ua_sess->registry->metadata_len_sent += len;
398 pthread_mutex_unlock(&ua_sess->registry->lock);
d88aee68
DG
399 pthread_mutex_unlock(socket->lock);
400
401 rcu_read_unlock();
402 free(metadata_str);
403 return 0;
404
405error_reg_unlock:
45893984 406 pthread_mutex_unlock(&ua_sess->registry->lock);
d88aee68
DG
407 pthread_mutex_unlock(socket->lock);
408error_rcu_unlock:
409 rcu_read_unlock();
410 free(metadata_str);
411end:
412 return ret;
413}
414
415/*
416 * Send to the consumer a close metadata command for the given session. Once
417 * done, the metadata channel is deleted and the session metadata pointer is
418 * nullified. The session lock MUST be acquired here unless the application is
419 * in the destroy path.
420 *
421 * Return 0 on success else a negative value.
422 */
423static int close_metadata(struct ust_app *app, struct ust_app_session *ua_sess)
424{
425 int ret;
426 struct consumer_socket *socket;
427
428 assert(app);
429 assert(ua_sess);
430
431 /* Ignore if no metadata. Valid since it can be called on unregister. */
432 if (!ua_sess->metadata) {
433 ret = 0;
434 goto error;
435 }
436
437 rcu_read_lock();
438
439 /* Get consumer socket to use to push the metadata.*/
440 socket = find_consumer_socket_by_bitness(app->bits_per_long,
441 ua_sess->consumer);
442 if (!socket) {
443 ret = -1;
444 goto error_rcu_unlock;
445 }
446
447 ret = ust_consumer_close_metadata(socket, ua_sess->metadata);
448 if (ret < 0) {
449 goto error_rcu_unlock;
450 }
451
452error_rcu_unlock:
453 /* Destroy metadata on our side since we must not use it anymore. */
454 delete_ust_app_channel(-1, ua_sess->metadata, app);
455 ua_sess->metadata = NULL;
456
457 rcu_read_unlock();
458error:
459 return ret;
460}
461
d80a6244
DG
462/*
463 * Delete ust app session safely. RCU read lock must be held before calling
464 * this function.
465 */
8b366481 466static
d0b96690
DG
467void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
468 struct ust_app *app)
d80a6244
DG
469{
470 int ret;
bec39940 471 struct lttng_ht_iter iter;
d80a6244
DG
472 struct ust_app_channel *ua_chan;
473
d88aee68
DG
474 assert(ua_sess);
475
d80a6244 476 if (ua_sess->metadata) {
d88aee68
DG
477 /* Push metadata for application before freeing the application. */
478 (void) push_metadata(app, ua_sess);
479
480 /* And ask to close it for this session. */
481 (void) close_metadata(app, ua_sess);
d80a6244
DG
482 }
483
bec39940
DG
484 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
485 node.node) {
486 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 487 assert(!ret);
d0b96690 488 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 489 }
bec39940 490 lttng_ht_destroy(ua_sess->channels);
d80a6244 491
45893984
DG
492 ust_registry_session_destroy(ua_sess->registry);
493 free(ua_sess->registry);
d0b96690 494
aee6bafd 495 if (ua_sess->handle != -1) {
ffe60014
DG
496 ret = ustctl_release_handle(sock, ua_sess->handle);
497 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
498 ERR("UST app sock %d release session handle failed with ret %d",
499 sock, ret);
500 }
aee6bafd 501 }
8b366481 502 free(ua_sess);
d80a6244 503}
91d76f53
DG
504
505/*
284d8f55
DG
506 * Delete a traceable application structure from the global list. Never call
507 * this function outside of a call_rcu call.
91d76f53 508 */
8b366481
DG
509static
510void delete_ust_app(struct ust_app *app)
91d76f53 511{
8b366481 512 int ret, sock;
d42f20df 513 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 514
f6a9efaa 515 rcu_read_lock();
44d3bd01 516
d80a6244 517 /* Delete ust app sessions info */
852d0037
DG
518 sock = app->sock;
519 app->sock = -1;
d80a6244 520
d42f20df
DG
521 lttng_ht_destroy(app->sessions);
522
8b366481 523 /* Wipe sessions */
d42f20df
DG
524 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
525 teardown_node) {
526 /* Free every object in the session and the session. */
d0b96690 527 delete_ust_app_session(sock, ua_sess, app);
d80a6244 528 }
d80a6244 529
6414a713 530 /*
852d0037
DG
531 * Wait until we have deleted the application from the sock hash table
532 * before closing this socket, otherwise an application could re-use the
533 * socket ID and race with the teardown, using the same hash table entry.
534 *
535 * It's OK to leave the close in call_rcu. We want it to stay unique for
536 * all RCU readers that could run concurrently with unregister app,
537 * therefore we _need_ to only close that socket after a grace period. So
538 * it should stay in this RCU callback.
539 *
540 * This close() is a very important step of the synchronization model so
541 * every modification to this function must be carefully reviewed.
6414a713 542 */
799e2c4f
MD
543 ret = close(sock);
544 if (ret) {
545 PERROR("close");
546 }
4063050c 547 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 548
852d0037 549 DBG2("UST app pid %d deleted", app->pid);
284d8f55 550 free(app);
bec39940 551
f6a9efaa 552 rcu_read_unlock();
099e26bd
DG
553}
554
555/*
f6a9efaa 556 * URCU intermediate call to delete an UST app.
099e26bd 557 */
8b366481
DG
558static
559void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 560{
bec39940
DG
561 struct lttng_ht_node_ulong *node =
562 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 563 struct ust_app *app =
852d0037 564 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 565
852d0037 566 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 567 delete_ust_app(app);
099e26bd
DG
568}
569
ffe60014
DG
570/*
571 * Delete the session from the application ht and delete the data structure by
572 * freeing every object inside and releasing them.
573 */
d0b96690 574static void destroy_app_session(struct ust_app *app,
ffe60014
DG
575 struct ust_app_session *ua_sess)
576{
577 int ret;
578 struct lttng_ht_iter iter;
579
580 assert(app);
581 assert(ua_sess);
582
583 iter.iter.node = &ua_sess->node.node;
584 ret = lttng_ht_del(app->sessions, &iter);
585 if (ret) {
586 /* Already scheduled for teardown. */
587 goto end;
588 }
589
590 /* Once deleted, free the data structure. */
d0b96690 591 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
592
593end:
594 return;
595}
596
8b366481
DG
597/*
598 * Alloc new UST app session.
599 */
600static
d0b96690 601struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
8b366481
DG
602{
603 struct ust_app_session *ua_sess;
604
605 /* Init most of the default value by allocating and zeroing */
606 ua_sess = zmalloc(sizeof(struct ust_app_session));
607 if (ua_sess == NULL) {
608 PERROR("malloc");
ffe60014 609 goto error_free;
8b366481
DG
610 }
611
612 ua_sess->handle = -1;
bec39940 613 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
d0b96690
DG
614 pthread_mutex_init(&ua_sess->lock, NULL);
615 if (ust_registry_session_init(&ua_sess->registry, app,
616 app->bits_per_long,
617 app->uint8_t_alignment,
618 app->uint16_t_alignment,
619 app->uint32_t_alignment,
620 app->uint64_t_alignment,
621 app->long_alignment,
622 app->byte_order)) {
ffe60014
DG
623 goto error;
624 }
625
8b366481
DG
626 return ua_sess;
627
628error:
ffe60014
DG
629 free(ua_sess);
630error_free:
8b366481
DG
631 return NULL;
632}
633
634/*
635 * Alloc new UST app channel.
636 */
637static
638struct ust_app_channel *alloc_ust_app_channel(char *name,
d0b96690 639 struct ust_app_session *ua_sess,
ffe60014 640 struct lttng_ust_channel_attr *attr)
8b366481
DG
641{
642 struct ust_app_channel *ua_chan;
643
644 /* Init most of the default value by allocating and zeroing */
645 ua_chan = zmalloc(sizeof(struct ust_app_channel));
646 if (ua_chan == NULL) {
647 PERROR("malloc");
648 goto error;
649 }
650
651 /* Setup channel name */
652 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
653 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
654
655 ua_chan->enabled = 1;
656 ua_chan->handle = -1;
45893984 657 ua_chan->session = ua_sess;
ffe60014 658 ua_chan->key = get_next_channel_key();
bec39940
DG
659 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
660 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
661 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
662
663 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
664
45893984
DG
665 /* Add a channel registry to session. */
666 if (ust_registry_channel_add(ua_sess->registry, ua_chan->key) < 0) {
667 goto error;
668 }
d0b96690 669
8b366481
DG
670 /* Copy attributes */
671 if (attr) {
ffe60014 672 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
673 ua_chan->attr.subbuf_size = attr->subbuf_size;
674 ua_chan->attr.num_subbuf = attr->num_subbuf;
675 ua_chan->attr.overwrite = attr->overwrite;
676 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
677 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
678 ua_chan->attr.output = attr->output;
8b366481 679 }
ffe60014
DG
680 /* By default, the channel is a per cpu channel. */
681 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
682
683 DBG3("UST app channel %s allocated", ua_chan->name);
684
685 return ua_chan;
686
687error:
688 return NULL;
689}
690
37f1c236
DG
691/*
692 * Allocate and initialize a UST app stream.
693 *
694 * Return newly allocated stream pointer or NULL on error.
695 */
ffe60014 696struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
697{
698 struct ust_app_stream *stream = NULL;
699
700 stream = zmalloc(sizeof(*stream));
701 if (stream == NULL) {
702 PERROR("zmalloc ust app stream");
703 goto error;
704 }
705
706 /* Zero could be a valid value for a handle so flag it to -1. */
707 stream->handle = -1;
708
709error:
710 return stream;
711}
712
8b366481
DG
713/*
714 * Alloc new UST app event.
715 */
716static
717struct ust_app_event *alloc_ust_app_event(char *name,
718 struct lttng_ust_event *attr)
719{
720 struct ust_app_event *ua_event;
721
722 /* Init most of the default value by allocating and zeroing */
723 ua_event = zmalloc(sizeof(struct ust_app_event));
724 if (ua_event == NULL) {
725 PERROR("malloc");
726 goto error;
727 }
728
729 ua_event->enabled = 1;
730 strncpy(ua_event->name, name, sizeof(ua_event->name));
731 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 732 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
733
734 /* Copy attributes */
735 if (attr) {
736 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
737 }
738
739 DBG3("UST app event %s allocated", ua_event->name);
740
741 return ua_event;
742
743error:
744 return NULL;
745}
746
747/*
748 * Alloc new UST app context.
749 */
750static
751struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
752{
753 struct ust_app_ctx *ua_ctx;
754
755 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
756 if (ua_ctx == NULL) {
757 goto error;
758 }
759
760 if (uctx) {
761 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
762 }
763
764 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
765
766error:
767 return ua_ctx;
768}
769
025faf73
DG
770/*
771 * Allocate a filter and copy the given original filter.
772 *
773 * Return allocated filter or NULL on error.
774 */
775static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
776 struct lttng_ust_filter_bytecode *orig_f)
777{
778 struct lttng_ust_filter_bytecode *filter = NULL;
779
780 /* Copy filter bytecode */
781 filter = zmalloc(sizeof(*filter) + orig_f->len);
782 if (!filter) {
783 PERROR("zmalloc alloc ust app filter");
784 goto error;
785 }
786
787 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
788
789error:
790 return filter;
791}
792
099e26bd 793/*
421cb601
DG
794 * Find an ust_app using the sock and return it. RCU read side lock must be
795 * held before calling this helper function.
099e26bd 796 */
8b366481
DG
797static
798struct ust_app *find_app_by_sock(int sock)
099e26bd 799{
bec39940 800 struct lttng_ht_node_ulong *node;
bec39940 801 struct lttng_ht_iter iter;
f6a9efaa 802
852d0037 803 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 804 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
805 if (node == NULL) {
806 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
807 goto error;
808 }
852d0037
DG
809
810 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
811
812error:
813 return NULL;
099e26bd
DG
814}
815
d0b96690
DG
816/*
817 * Find an ust_app using the notify sock and return it. RCU read side lock must
818 * be held before calling this helper function.
819 */
820static struct ust_app *find_app_by_notify_sock(int sock)
821{
822 struct lttng_ht_node_ulong *node;
823 struct lttng_ht_iter iter;
824
825 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
826 &iter);
827 node = lttng_ht_iter_get_node_ulong(&iter);
828 if (node == NULL) {
829 DBG2("UST app find by notify sock %d not found", sock);
830 goto error;
831 }
832
833 return caa_container_of(node, struct ust_app, notify_sock_n);
834
835error:
836 return NULL;
837}
838
025faf73
DG
839/*
840 * Lookup for an ust app event based on event name, filter bytecode and the
841 * event loglevel.
842 *
843 * Return an ust_app_event object or NULL on error.
844 */
18eace3b
DG
845static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
846 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel)
847{
848 struct lttng_ht_iter iter;
849 struct lttng_ht_node_str *node;
850 struct ust_app_event *event = NULL;
851 struct ust_app_ht_key key;
18eace3b
DG
852
853 assert(name);
854 assert(ht);
855
856 /* Setup key for event lookup. */
857 key.name = name;
858 key.filter = filter;
859 key.loglevel = loglevel;
860
025faf73
DG
861 /* Lookup using the event name as hash and a custom match fct. */
862 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
863 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
864 node = lttng_ht_iter_get_node_str(&iter);
865 if (node == NULL) {
866 goto end;
867 }
868
869 event = caa_container_of(node, struct ust_app_event, node);
870
871end:
18eace3b
DG
872 return event;
873}
874
55cc08a6
DG
875/*
876 * Create the channel context on the tracer.
d0b96690
DG
877 *
878 * Called with UST app session lock held.
55cc08a6
DG
879 */
880static
881int create_ust_channel_context(struct ust_app_channel *ua_chan,
882 struct ust_app_ctx *ua_ctx, struct ust_app *app)
883{
884 int ret;
885
840cb59c 886 health_code_update();
86acf0da 887
852d0037 888 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6
DG
889 ua_chan->obj, &ua_ctx->obj);
890 if (ret < 0) {
ffe60014
DG
891 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
892 ERR("UST app create channel context failed for app (pid: %d) "
893 "with ret %d", app->pid, ret);
894 } else {
895 DBG3("UST app disable event failed. Application is dead.");
896 }
55cc08a6
DG
897 goto error;
898 }
899
900 ua_ctx->handle = ua_ctx->obj->handle;
901
d0b96690
DG
902 DBG2("UST app context handle %d created successfully for channel %s",
903 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
904
905error:
840cb59c 906 health_code_update();
55cc08a6
DG
907 return ret;
908}
909
53a80697
MD
910/*
911 * Set the filter on the tracer.
912 */
913static
914int set_ust_event_filter(struct ust_app_event *ua_event,
915 struct ust_app *app)
916{
917 int ret;
918
840cb59c 919 health_code_update();
86acf0da 920
53a80697 921 if (!ua_event->filter) {
86acf0da
DG
922 ret = 0;
923 goto error;
53a80697
MD
924 }
925
926 ret = ustctl_set_filter(app->sock, ua_event->filter,
927 ua_event->obj);
928 if (ret < 0) {
ffe60014
DG
929 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
930 ERR("UST app event %s filter failed for app (pid: %d) "
931 "with ret %d", ua_event->attr.name, app->pid, ret);
932 } else {
933 DBG3("UST app filter event failed. Application is dead.");
934 }
53a80697
MD
935 goto error;
936 }
937
938 DBG2("UST filter set successfully for event %s", ua_event->name);
939
940error:
840cb59c 941 health_code_update();
53a80697
MD
942 return ret;
943}
944
9730260e
DG
945/*
946 * Disable the specified event on to UST tracer for the UST session.
947 */
948static int disable_ust_event(struct ust_app *app,
949 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
950{
951 int ret;
952
840cb59c 953 health_code_update();
86acf0da 954
852d0037 955 ret = ustctl_disable(app->sock, ua_event->obj);
9730260e 956 if (ret < 0) {
ffe60014
DG
957 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
958 ERR("UST app event %s disable failed for app (pid: %d) "
959 "and session handle %d with ret %d",
960 ua_event->attr.name, app->pid, ua_sess->handle, ret);
961 } else {
962 DBG3("UST app disable event failed. Application is dead.");
963 }
9730260e
DG
964 goto error;
965 }
966
967 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 968 ua_event->attr.name, app->pid);
9730260e
DG
969
970error:
840cb59c 971 health_code_update();
9730260e
DG
972 return ret;
973}
974
78f0bacd
DG
975/*
976 * Disable the specified channel on to UST tracer for the UST session.
977 */
978static int disable_ust_channel(struct ust_app *app,
979 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
980{
981 int ret;
982
840cb59c 983 health_code_update();
86acf0da 984
852d0037 985 ret = ustctl_disable(app->sock, ua_chan->obj);
78f0bacd 986 if (ret < 0) {
ffe60014
DG
987 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
988 ERR("UST app channel %s disable failed for app (pid: %d) "
989 "and session handle %d with ret %d",
990 ua_chan->name, app->pid, ua_sess->handle, ret);
991 } else {
992 DBG3("UST app disable channel failed. Application is dead.");
993 }
78f0bacd
DG
994 goto error;
995 }
996
78f0bacd 997 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 998 ua_chan->name, app->pid);
78f0bacd
DG
999
1000error:
840cb59c 1001 health_code_update();
78f0bacd
DG
1002 return ret;
1003}
1004
1005/*
1006 * Enable the specified channel on to UST tracer for the UST session.
1007 */
1008static int enable_ust_channel(struct ust_app *app,
1009 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1010{
1011 int ret;
1012
840cb59c 1013 health_code_update();
86acf0da 1014
852d0037 1015 ret = ustctl_enable(app->sock, ua_chan->obj);
78f0bacd 1016 if (ret < 0) {
ffe60014
DG
1017 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1018 ERR("UST app channel %s enable failed for app (pid: %d) "
1019 "and session handle %d with ret %d",
1020 ua_chan->name, app->pid, ua_sess->handle, ret);
1021 } else {
1022 DBG3("UST app enable channel failed. Application is dead.");
1023 }
78f0bacd
DG
1024 goto error;
1025 }
1026
1027 ua_chan->enabled = 1;
1028
1029 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 1030 ua_chan->name, app->pid);
78f0bacd
DG
1031
1032error:
840cb59c 1033 health_code_update();
78f0bacd
DG
1034 return ret;
1035}
1036
edb67388
DG
1037/*
1038 * Enable the specified event on to UST tracer for the UST session.
1039 */
1040static int enable_ust_event(struct ust_app *app,
1041 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1042{
1043 int ret;
1044
840cb59c 1045 health_code_update();
86acf0da 1046
852d0037 1047 ret = ustctl_enable(app->sock, ua_event->obj);
edb67388 1048 if (ret < 0) {
ffe60014
DG
1049 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1050 ERR("UST app event %s enable failed for app (pid: %d) "
1051 "and session handle %d with ret %d",
1052 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1053 } else {
1054 DBG3("UST app enable event failed. Application is dead.");
1055 }
edb67388
DG
1056 goto error;
1057 }
1058
1059 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 1060 ua_event->attr.name, app->pid);
edb67388
DG
1061
1062error:
840cb59c 1063 health_code_update();
edb67388
DG
1064 return ret;
1065}
1066
099e26bd 1067/*
d88aee68
DG
1068 * Create the specified channel onto the UST tracer for a UST session. This
1069 * MUST be called with UST app session lock held.
4f3ab6ee 1070 *
ffe60014 1071 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1072 */
ffe60014
DG
1073static int create_ust_channel(struct ust_app *app,
1074 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
1075 struct consumer_output *consumer)
4f3ab6ee
DG
1076{
1077 int ret;
ffe60014
DG
1078 unsigned int nb_fd = 0;
1079 struct consumer_socket *socket;
1080 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1081
1082 assert(app);
ffe60014 1083 assert(ua_sess);
4f3ab6ee 1084 assert(ua_chan);
ffe60014 1085 assert(consumer);
4f3ab6ee 1086
d88aee68 1087 rcu_read_lock();
840cb59c 1088 health_code_update();
4f3ab6ee 1089
ffe60014
DG
1090 /* Get the right consumer socket for the application. */
1091 socket = find_consumer_socket_by_bitness(app->bits_per_long, consumer);
1092 if (!socket) {
4f3ab6ee
DG
1093 ret = -1;
1094 goto error;
1095 }
1096
ffe60014
DG
1097 health_code_update();
1098
7f133705 1099 /*
ffe60014
DG
1100 * Ask consumer to create channel. The consumer will return the number of
1101 * stream we have to expect.
7f133705 1102 */
ffe60014 1103 ret = ust_consumer_ask_channel(ua_sess, ua_chan, consumer, socket);
7f133705 1104 if (ret < 0) {
7f133705
DG
1105 goto error;
1106 }
1107
ffe60014
DG
1108 /*
1109 * Compute the number of fd needed before receiving them. It must be 2 per
d88aee68 1110 * stream (2 being the default value here).
ffe60014
DG
1111 */
1112 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
1113
1114 /* Reserve the amount of file descriptor we need. */
1115 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
4f3ab6ee 1116 if (ret < 0) {
ffe60014
DG
1117 ERR("Exhausted number of available FD upon create channel");
1118 goto error_fd_get;
4f3ab6ee
DG
1119 }
1120
840cb59c 1121 health_code_update();
4f3ab6ee 1122
ffe60014
DG
1123 /*
1124 * Now get the channel from the consumer. This call wil populate the stream
1125 * list of that channel and set the ust object.
1126 */
1127 ret = ust_consumer_get_channel(socket, ua_chan);
4063050c 1128 if (ret < 0) {
ffe60014 1129 goto error_destroy;
4063050c 1130 }
86acf0da 1131
ffe60014
DG
1132 /* Send channel to the application. */
1133 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
5b4a0ec0 1134 if (ret < 0) {
b551a063
DG
1135 goto error;
1136 }
1137
d88aee68
DG
1138 health_code_update();
1139
ffe60014
DG
1140 /* Send all streams to application. */
1141 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1142 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
1143 if (ret < 0) {
1144 goto error;
1145 }
1146 /* We don't need the stream anymore once sent to the tracer. */
1147 cds_list_del(&stream->list);
1148 delete_ust_app_stream(-1, stream);
1149 }
b551a063 1150
ffe60014
DG
1151 /* Flag the channel that it is sent to the application. */
1152 ua_chan->is_sent = 1;
d0b96690
DG
1153 /* Initialize ust objd object using the received handle and add it. */
1154 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
1155 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
b551a063 1156
840cb59c 1157 health_code_update();
86acf0da 1158
8535a6d9
DG
1159 /* If channel is not enabled, disable it on the tracer */
1160 if (!ua_chan->enabled) {
1161 ret = disable_ust_channel(app, ua_sess, ua_chan);
1162 if (ret < 0) {
1163 goto error;
1164 }
1165 }
1166
d88aee68 1167 rcu_read_unlock();
ffe60014
DG
1168 return 0;
1169
1170error_destroy:
1171 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
1172error_fd_get:
1173 /*
1174 * Initiate a destroy channel on the consumer since we had an error
1175 * handling it on our side. The return value is of no importance since we
1176 * already have a ret value set by the previous error that we need to
1177 * return.
1178 */
1179 (void) ust_consumer_destroy_channel(socket, ua_chan);
b551a063 1180error:
840cb59c 1181 health_code_update();
d88aee68 1182 rcu_read_unlock();
b551a063
DG
1183 return ret;
1184}
1185
91d76f53 1186/*
5b4a0ec0 1187 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1188 *
1189 * Should be called with session mutex held.
91d76f53 1190 */
edb67388
DG
1191static
1192int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1193 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1194{
5b4a0ec0 1195 int ret = 0;
284d8f55 1196
840cb59c 1197 health_code_update();
86acf0da 1198
5b4a0ec0 1199 /* Create UST event on tracer */
852d0037 1200 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0
DG
1201 &ua_event->obj);
1202 if (ret < 0) {
ffe60014
DG
1203 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1204 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1205 ua_event->attr.name, app->pid, ret);
1206 } else {
1207 DBG3("UST app create event failed. Application is dead.");
1208 }
5b4a0ec0 1209 goto error;
91d76f53 1210 }
f6a9efaa 1211
5b4a0ec0 1212 ua_event->handle = ua_event->obj->handle;
284d8f55 1213
5b4a0ec0 1214 DBG2("UST app event %s created successfully for pid:%d",
852d0037 1215 ua_event->attr.name, app->pid);
f6a9efaa 1216
840cb59c 1217 health_code_update();
86acf0da 1218
025faf73
DG
1219 /* Set filter if one is present. */
1220 if (ua_event->filter) {
1221 ret = set_ust_event_filter(ua_event, app);
1222 if (ret < 0) {
1223 goto error;
1224 }
1225 }
1226
8535a6d9 1227 /* If event not enabled, disable it on the tracer */
fc34caaa 1228 if (ua_event->enabled == 0) {
8535a6d9
DG
1229 ret = disable_ust_event(app, ua_sess, ua_event);
1230 if (ret < 0) {
fc34caaa
DG
1231 /*
1232 * If we hit an EPERM, something is wrong with our disable call. If
1233 * we get an EEXIST, there is a problem on the tracer side since we
1234 * just created it.
1235 */
1236 switch (ret) {
49c336c1 1237 case -LTTNG_UST_ERR_PERM:
fc34caaa
DG
1238 /* Code flow problem */
1239 assert(0);
49c336c1 1240 case -LTTNG_UST_ERR_EXIST:
fc34caaa
DG
1241 /* It's OK for our use case. */
1242 ret = 0;
1243 break;
1244 default:
1245 break;
1246 }
8535a6d9
DG
1247 goto error;
1248 }
1249 }
1250
5b4a0ec0 1251error:
840cb59c 1252 health_code_update();
5b4a0ec0 1253 return ret;
91d76f53 1254}
48842b30 1255
5b4a0ec0
DG
1256/*
1257 * Copy data between an UST app event and a LTT event.
1258 */
421cb601 1259static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1260 struct ltt_ust_event *uevent)
1261{
1262 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1263 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1264
fc34caaa
DG
1265 ua_event->enabled = uevent->enabled;
1266
5b4a0ec0
DG
1267 /* Copy event attributes */
1268 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1269
53a80697
MD
1270 /* Copy filter bytecode */
1271 if (uevent->filter) {
025faf73
DG
1272 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
1273 /* Filter might be NULL here in case of ENONEM. */
53a80697 1274 }
48842b30
DG
1275}
1276
5b4a0ec0
DG
1277/*
1278 * Copy data between an UST app channel and a LTT channel.
1279 */
421cb601 1280static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1281 struct ltt_ust_channel *uchan)
1282{
bec39940 1283 struct lttng_ht_iter iter;
48842b30 1284 struct ltt_ust_event *uevent;
55cc08a6 1285 struct ltt_ust_context *uctx;
48842b30 1286 struct ust_app_event *ua_event;
55cc08a6 1287 struct ust_app_ctx *ua_ctx;
48842b30 1288
fc34caaa 1289 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1290
1291 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1292 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014
DG
1293
1294 /* Copy event attributes since the layout is different. */
1295 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1296 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1297 ua_chan->attr.overwrite = uchan->attr.overwrite;
1298 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1299 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1300 ua_chan->attr.output = uchan->attr.output;
1301 /*
1302 * Note that the attribute channel type is not set since the channel on the
1303 * tracing registry side does not have this information.
1304 */
48842b30 1305
fc34caaa
DG
1306 ua_chan->enabled = uchan->enabled;
1307
bec39940 1308 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
1309 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1310 if (ua_ctx == NULL) {
1311 continue;
1312 }
bec39940
DG
1313 lttng_ht_node_init_ulong(&ua_ctx->node,
1314 (unsigned long) ua_ctx->ctx.ctx);
1315 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6 1316 }
48842b30 1317
421cb601 1318 /* Copy all events from ltt ust channel to ust app channel */
bec39940 1319 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
18eace3b
DG
1320 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1321 uevent->filter, uevent->attr.loglevel);
1322 if (ua_event == NULL) {
421cb601 1323 DBG2("UST event %s not found on shadow copy channel",
48842b30 1324 uevent->attr.name);
284d8f55 1325 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 1326 if (ua_event == NULL) {
5b4a0ec0 1327 continue;
48842b30 1328 }
421cb601 1329 shadow_copy_event(ua_event, uevent);
d0b96690 1330 add_unique_ust_app_event(ua_chan, ua_event);
48842b30 1331 }
48842b30
DG
1332 }
1333
fc34caaa 1334 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1335}
1336
5b4a0ec0
DG
1337/*
1338 * Copy data between a UST app session and a regular LTT session.
1339 */
421cb601 1340static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1341 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1342{
bec39940
DG
1343 struct lttng_ht_node_str *ua_chan_node;
1344 struct lttng_ht_iter iter;
48842b30
DG
1345 struct ltt_ust_channel *uchan;
1346 struct ust_app_channel *ua_chan;
477d7741
MD
1347 time_t rawtime;
1348 struct tm *timeinfo;
1349 char datetime[16];
1350 int ret;
1351
1352 /* Get date and time for unique app path */
1353 time(&rawtime);
1354 timeinfo = localtime(&rawtime);
1355 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1356
421cb601 1357 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1358
a991f516 1359 ua_sess->id = usess->id;
6df2e2c9
MD
1360 ua_sess->uid = usess->uid;
1361 ua_sess->gid = usess->gid;
48842b30 1362
00e2e675
DG
1363 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
1364 datetime);
477d7741
MD
1365 if (ret < 0) {
1366 PERROR("asprintf UST shadow copy session");
1367 /* TODO: We cannot return an error from here.. */
1368 assert(0);
1369 }
1370
48842b30
DG
1371 /* TODO: support all UST domain */
1372
1373 /* Iterate over all channels in global domain. */
bec39940
DG
1374 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1375 uchan, node.node) {
1376 struct lttng_ht_iter uiter;
ba767faf 1377
bec39940
DG
1378 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1379 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0 1380 if (ua_chan_node != NULL) {
fc34caaa 1381 /* Session exist. Contiuing. */
5b4a0ec0
DG
1382 continue;
1383 }
421cb601 1384
5b4a0ec0
DG
1385 DBG2("Channel %s not found on shadow session copy, creating it",
1386 uchan->name);
d0b96690 1387 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
5b4a0ec0 1388 if (ua_chan == NULL) {
fc34caaa 1389 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
5b4a0ec0 1390 continue;
48842b30 1391 }
5b4a0ec0 1392 shadow_copy_channel(ua_chan, uchan);
ffe60014
DG
1393 /*
1394 * The concept of metadata channel does not exist on the tracing
1395 * registry side of the session daemon so this can only be a per CPU
1396 * channel and not metadata.
1397 */
1398 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1399
bec39940 1400 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30
DG
1401 }
1402}
1403
78f0bacd
DG
1404/*
1405 * Lookup sesison wrapper.
1406 */
84cd17c6
MD
1407static
1408void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 1409 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1410{
1411 /* Get right UST app session from app */
2c348c10 1412 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
84cd17c6
MD
1413}
1414
421cb601
DG
1415/*
1416 * Return ust app session from the app session hashtable using the UST session
a991f516 1417 * id.
421cb601 1418 */
48842b30
DG
1419static struct ust_app_session *lookup_session_by_app(
1420 struct ltt_ust_session *usess, struct ust_app *app)
1421{
bec39940
DG
1422 struct lttng_ht_iter iter;
1423 struct lttng_ht_node_ulong *node;
48842b30 1424
84cd17c6 1425 __lookup_session_by_app(usess, app, &iter);
bec39940 1426 node = lttng_ht_iter_get_node_ulong(&iter);
48842b30
DG
1427 if (node == NULL) {
1428 goto error;
1429 }
1430
1431 return caa_container_of(node, struct ust_app_session, node);
1432
1433error:
1434 return NULL;
1435}
1436
421cb601 1437/*
3d8ca23b 1438 * Create a session on the tracer side for the given app.
421cb601 1439 *
3d8ca23b
DG
1440 * On success, ua_sess_ptr is populated with the session pointer or else left
1441 * untouched. If the session was created, is_created is set to 1. On error,
1442 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1443 * be NULL.
1444 *
1445 * Returns 0 on success or else a negative code which is either -ENOMEM or
1446 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 1447 */
3d8ca23b
DG
1448static int create_ust_app_session(struct ltt_ust_session *usess,
1449 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1450 int *is_created)
421cb601 1451{
3d8ca23b 1452 int ret, created = 0;
421cb601
DG
1453 struct ust_app_session *ua_sess;
1454
3d8ca23b
DG
1455 assert(usess);
1456 assert(app);
1457 assert(ua_sess_ptr);
1458
840cb59c 1459 health_code_update();
86acf0da 1460
421cb601
DG
1461 ua_sess = lookup_session_by_app(usess, app);
1462 if (ua_sess == NULL) {
a991f516 1463 DBG2("UST app pid: %d session id %d not found, creating it",
852d0037 1464 app->pid, usess->id);
d0b96690 1465 ua_sess = alloc_ust_app_session(app);
421cb601
DG
1466 if (ua_sess == NULL) {
1467 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
1468 ret = -ENOMEM;
1469 goto error;
421cb601 1470 }
477d7741 1471 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 1472 created = 1;
421cb601
DG
1473 }
1474
840cb59c 1475 health_code_update();
86acf0da 1476
421cb601 1477 if (ua_sess->handle == -1) {
852d0037 1478 ret = ustctl_create_session(app->sock);
421cb601 1479 if (ret < 0) {
ffe60014
DG
1480 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1481 ERR("Creating session for app pid %d with ret %d",
1482 app->pid, ret);
1483 } else {
1484 DBG("UST app creating session failed. Application is dead");
1485 }
d0b96690 1486 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
1487 if (ret != -ENOMEM) {
1488 /*
1489 * Tracer is probably gone or got an internal error so let's
1490 * behave like it will soon unregister or not usable.
1491 */
1492 ret = -ENOTCONN;
1493 }
1494 goto error;
421cb601
DG
1495 }
1496
421cb601
DG
1497 ua_sess->handle = ret;
1498
1499 /* Add ust app session to app's HT */
2c348c10 1500 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
bec39940 1501 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
421cb601
DG
1502
1503 DBG2("UST app session created successfully with handle %d", ret);
1504 }
1505
d88aee68
DG
1506 /*
1507 * Assign consumer if not already set. For one application, there is only
1508 * one possible consumer has of now.
1509 */
1510 if (!ua_sess->consumer) {
1511 ua_sess->consumer = usess->consumer;
1512 }
1513
3d8ca23b
DG
1514 *ua_sess_ptr = ua_sess;
1515 if (is_created) {
1516 *is_created = created;
1517 }
d88aee68 1518
3d8ca23b
DG
1519 /* Everything went well. */
1520 ret = 0;
1521
1522error:
840cb59c 1523 health_code_update();
3d8ca23b 1524 return ret;
421cb601
DG
1525}
1526
55cc08a6
DG
1527/*
1528 * Create a context for the channel on the tracer.
d0b96690
DG
1529 *
1530 * Called with UST app session lock held.
55cc08a6
DG
1531 */
1532static
1533int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1534 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1535 struct ust_app *app)
1536{
1537 int ret = 0;
bec39940
DG
1538 struct lttng_ht_iter iter;
1539 struct lttng_ht_node_ulong *node;
55cc08a6
DG
1540 struct ust_app_ctx *ua_ctx;
1541
1542 DBG2("UST app adding context to channel %s", ua_chan->name);
1543
bec39940
DG
1544 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1545 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
1546 if (node != NULL) {
1547 ret = -EEXIST;
1548 goto error;
1549 }
1550
1551 ua_ctx = alloc_ust_app_ctx(uctx);
1552 if (ua_ctx == NULL) {
1553 /* malloc failed */
1554 ret = -1;
1555 goto error;
1556 }
1557
bec39940
DG
1558 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1559 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6
DG
1560
1561 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1562 if (ret < 0) {
1563 goto error;
1564 }
1565
1566error:
1567 return ret;
1568}
1569
edb67388
DG
1570/*
1571 * Enable on the tracer side a ust app event for the session and channel.
d0b96690
DG
1572 *
1573 * Called with UST app session lock held.
edb67388
DG
1574 */
1575static
1576int enable_ust_app_event(struct ust_app_session *ua_sess,
35a9059d 1577 struct ust_app_event *ua_event, struct ust_app *app)
edb67388
DG
1578{
1579 int ret;
1580
1581 ret = enable_ust_event(app, ua_sess, ua_event);
1582 if (ret < 0) {
1583 goto error;
1584 }
1585
1586 ua_event->enabled = 1;
1587
1588error:
1589 return ret;
1590}
1591
9730260e
DG
1592/*
1593 * Disable on the tracer side a ust app event for the session and channel.
1594 */
1595static int disable_ust_app_event(struct ust_app_session *ua_sess,
7f79d3a1 1596 struct ust_app_event *ua_event, struct ust_app *app)
9730260e
DG
1597{
1598 int ret;
1599
1600 ret = disable_ust_event(app, ua_sess, ua_event);
1601 if (ret < 0) {
1602 goto error;
1603 }
1604
1605 ua_event->enabled = 0;
1606
1607error:
1608 return ret;
1609}
1610
78f0bacd
DG
1611/*
1612 * Lookup ust app channel for session and disable it on the tracer side.
1613 */
8535a6d9
DG
1614static
1615int disable_ust_app_channel(struct ust_app_session *ua_sess,
1616 struct ust_app_channel *ua_chan, struct ust_app *app)
78f0bacd 1617{
8535a6d9 1618 int ret;
78f0bacd
DG
1619
1620 ret = disable_ust_channel(app, ua_sess, ua_chan);
1621 if (ret < 0) {
1622 goto error;
1623 }
1624
8535a6d9
DG
1625 ua_chan->enabled = 0;
1626
78f0bacd
DG
1627error:
1628 return ret;
1629}
1630
1631/*
1632 * Lookup ust app channel for session and enable it on the tracer side.
1633 */
1634static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1635 struct ltt_ust_channel *uchan, struct ust_app *app)
1636{
1637 int ret = 0;
bec39940
DG
1638 struct lttng_ht_iter iter;
1639 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1640 struct ust_app_channel *ua_chan;
1641
bec39940
DG
1642 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1643 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
78f0bacd 1644 if (ua_chan_node == NULL) {
a991f516
MD
1645 DBG2("Unable to find channel %s in ust session id %u",
1646 uchan->name, ua_sess->id);
78f0bacd
DG
1647 goto error;
1648 }
1649
1650 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1651
1652 ret = enable_ust_channel(app, ua_sess, ua_chan);
1653 if (ret < 0) {
1654 goto error;
1655 }
1656
1657error:
1658 return ret;
1659}
1660
284d8f55 1661/*
4d710ac2
DG
1662 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1663 * newly created channel if not NULL.
d0b96690
DG
1664 *
1665 * Called with UST app session lock held.
284d8f55 1666 */
4d710ac2
DG
1667static int create_ust_app_channel(struct ust_app_session *ua_sess,
1668 struct ltt_ust_channel *uchan, struct ust_app *app,
ffe60014 1669 struct consumer_output *consumer, enum lttng_ust_chan_type type,
4d710ac2 1670 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
1671{
1672 int ret = 0;
bec39940
DG
1673 struct lttng_ht_iter iter;
1674 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
1675 struct ust_app_channel *ua_chan;
1676
1677 /* Lookup channel in the ust app session */
bec39940
DG
1678 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1679 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 1680 if (ua_chan_node != NULL) {
5b4a0ec0 1681 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 1682 goto end;
5b4a0ec0
DG
1683 }
1684
d0b96690 1685 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
1686 if (ua_chan == NULL) {
1687 /* Only malloc can fail here */
4d710ac2 1688 ret = -ENOMEM;
fc34caaa
DG
1689 goto error;
1690 }
1691 shadow_copy_channel(ua_chan, uchan);
1692
ffe60014
DG
1693 /* Set channel type. */
1694 ua_chan->attr.type = type;
1695
1696 ret = create_ust_channel(app, ua_sess, ua_chan, consumer);
5b4a0ec0
DG
1697 if (ret < 0) {
1698 goto error;
1699 }
1700
fc34caaa 1701 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
852d0037 1702 app->pid);
fc34caaa 1703
d0b96690
DG
1704 /* Only add the channel if successful on the tracer side. */
1705 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1706
fc34caaa 1707end:
4d710ac2
DG
1708 if (ua_chanp) {
1709 *ua_chanp = ua_chan;
1710 }
1711
1712 /* Everything went well. */
1713 return 0;
5b4a0ec0
DG
1714
1715error:
d0b96690 1716 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
4d710ac2 1717 return ret;
5b4a0ec0
DG
1718}
1719
1720/*
1721 * Create UST app event and create it on the tracer side.
d0b96690
DG
1722 *
1723 * Called with ust app session mutex held.
5b4a0ec0 1724 */
edb67388
DG
1725static
1726int create_ust_app_event(struct ust_app_session *ua_sess,
1727 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1728 struct ust_app *app)
284d8f55 1729{
edb67388 1730 int ret = 0;
5b4a0ec0 1731 struct ust_app_event *ua_event;
284d8f55 1732
5b4a0ec0 1733 /* Get event node */
18eace3b
DG
1734 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1735 uevent->filter, uevent->attr.loglevel);
1736 if (ua_event != NULL) {
fc34caaa 1737 ret = -EEXIST;
edb67388
DG
1738 goto end;
1739 }
5b4a0ec0 1740
edb67388
DG
1741 /* Does not exist so create one */
1742 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1743 if (ua_event == NULL) {
1744 /* Only malloc can failed so something is really wrong */
1745 ret = -ENOMEM;
fc34caaa 1746 goto end;
5b4a0ec0 1747 }
edb67388 1748 shadow_copy_event(ua_event, uevent);
5b4a0ec0 1749
edb67388 1750 /* Create it on the tracer side */
5b4a0ec0 1751 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 1752 if (ret < 0) {
fc34caaa 1753 /* Not found previously means that it does not exist on the tracer */
76f66f63 1754 assert(ret != -LTTNG_UST_ERR_EXIST);
284d8f55
DG
1755 goto error;
1756 }
1757
d0b96690 1758 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 1759
fc34caaa 1760 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 1761 app->pid);
7f79d3a1 1762
edb67388 1763end:
fc34caaa
DG
1764 return ret;
1765
5b4a0ec0 1766error:
fc34caaa
DG
1767 /* Valid. Calling here is already in a read side lock */
1768 delete_ust_app_event(-1, ua_event);
edb67388 1769 return ret;
5b4a0ec0
DG
1770}
1771
1772/*
1773 * Create UST metadata and open it on the tracer side.
d0b96690
DG
1774 *
1775 * Called with UST app session lock held.
5b4a0ec0
DG
1776 */
1777static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ffe60014 1778 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
1779{
1780 int ret = 0;
ffe60014 1781 struct ust_app_channel *metadata;
d88aee68 1782 struct consumer_socket *socket;
5b4a0ec0 1783
ffe60014
DG
1784 assert(ua_sess);
1785 assert(app);
d88aee68 1786 assert(consumer);
5b4a0ec0 1787
ffe60014
DG
1788 if (ua_sess->metadata) {
1789 /* Already exist. Return success. */
1790 goto end;
5b4a0ec0
DG
1791 }
1792
ffe60014 1793 /* Allocate UST metadata */
d0b96690 1794 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
1795 if (!metadata) {
1796 /* malloc() failed */
1797 ret = -ENOMEM;
1798 goto error;
1799 }
5b4a0ec0 1800
ffe60014
DG
1801 /* Set default attributes for metadata. */
1802 metadata->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1803 metadata->attr.subbuf_size = default_get_metadata_subbuf_size();
1804 metadata->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
6bb9e85f
MD
1805 metadata->attr.switch_timer_interval = DEFAULT_UST_CHANNEL_SWITCH_TIMER;
1806 metadata->attr.read_timer_interval = DEFAULT_UST_CHANNEL_READ_TIMER;
ffe60014
DG
1807 metadata->attr.output = LTTNG_UST_MMAP;
1808 metadata->attr.type = LTTNG_UST_CHAN_METADATA;
5b4a0ec0 1809
d88aee68
DG
1810 /* Get the right consumer socket for the application. */
1811 socket = find_consumer_socket_by_bitness(app->bits_per_long, consumer);
1812 if (!socket) {
1813 ret = -EINVAL;
1814 goto error_consumer;
1815 }
1816
1817 /*
1818 * Ask the metadata channel creation to the consumer. The metadata object
1819 * will be created by the consumer and kept their. However, the stream is
1820 * never added or monitored until we do a first push metadata to the
1821 * consumer.
1822 */
1823 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket);
1824 if (ret < 0) {
1825 goto error_consumer;
1826 }
1827
1828 /*
1829 * The setup command will make the metadata stream be sent to the relayd,
1830 * if applicable, and the thread managing the metadatas. This is important
1831 * because after this point, if an error occurs, the only way the stream
1832 * can be deleted is to be monitored in the consumer.
1833 */
1834 ret = ust_consumer_setup_metadata(socket, metadata);
ffe60014 1835 if (ret < 0) {
d88aee68 1836 goto error_consumer;
5b4a0ec0
DG
1837 }
1838
ffe60014
DG
1839 ua_sess->metadata = metadata;
1840
d88aee68 1841 DBG2("UST metadata created for app pid %d", app->pid);
5b4a0ec0 1842
ffe60014
DG
1843end:
1844 return 0;
d88aee68
DG
1845error_consumer:
1846 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 1847error:
ffe60014 1848 return ret;
5b4a0ec0
DG
1849}
1850
1851/*
1852 * Return pointer to traceable apps list.
1853 */
bec39940 1854struct lttng_ht *ust_app_get_ht(void)
5b4a0ec0
DG
1855{
1856 return ust_app_ht;
1857}
1858
1859/*
d88aee68
DG
1860 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
1861 * acquired before calling this function.
5b4a0ec0
DG
1862 */
1863struct ust_app *ust_app_find_by_pid(pid_t pid)
1864{
d88aee68 1865 struct ust_app *app = NULL;
bec39940
DG
1866 struct lttng_ht_node_ulong *node;
1867 struct lttng_ht_iter iter;
5b4a0ec0 1868
bec39940
DG
1869 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1870 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
1871 if (node == NULL) {
1872 DBG2("UST app no found with pid %d", pid);
1873 goto error;
1874 }
5b4a0ec0
DG
1875
1876 DBG2("Found UST app by pid %d", pid);
1877
d88aee68 1878 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
1879
1880error:
d88aee68 1881 return app;
5b4a0ec0
DG
1882}
1883
d88aee68
DG
1884/*
1885 * Allocate and init an UST app object using the registration information and
1886 * the command socket. This is called when the command socket connects to the
1887 * session daemon.
1888 *
1889 * The object is returned on success or else NULL.
1890 */
d0b96690 1891struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 1892{
d0b96690
DG
1893 struct ust_app *lta = NULL;
1894
1895 assert(msg);
1896 assert(sock >= 0);
1897
1898 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 1899
173af62f
DG
1900 if ((msg->bits_per_long == 64 &&
1901 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1902 || (msg->bits_per_long == 32 &&
1903 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 1904 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
1905 "%d-bit long, but no consumerd for this size is available.\n",
1906 msg->name, msg->pid, msg->bits_per_long);
1907 goto error;
3f2c5fcc 1908 }
d0b96690 1909
5b4a0ec0
DG
1910 lta = zmalloc(sizeof(struct ust_app));
1911 if (lta == NULL) {
1912 PERROR("malloc");
d0b96690 1913 goto error;
5b4a0ec0
DG
1914 }
1915
1916 lta->ppid = msg->ppid;
1917 lta->uid = msg->uid;
1918 lta->gid = msg->gid;
d0b96690 1919
7753dea8 1920 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
1921 lta->uint8_t_alignment = msg->uint8_t_alignment;
1922 lta->uint16_t_alignment = msg->uint16_t_alignment;
1923 lta->uint32_t_alignment = msg->uint32_t_alignment;
1924 lta->uint64_t_alignment = msg->uint64_t_alignment;
1925 lta->long_alignment = msg->long_alignment;
1926 lta->byte_order = msg->byte_order;
1927
5b4a0ec0
DG
1928 lta->v_major = msg->major;
1929 lta->v_minor = msg->minor;
bec39940 1930 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690
DG
1931 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1932 lta->notify_sock = -1;
d88aee68
DG
1933
1934 /* Copy name and make sure it's NULL terminated. */
1935 strncpy(lta->name, msg->name, sizeof(lta->name));
1936 lta->name[UST_APP_PROCNAME_LEN] = '\0';
1937
1938 /*
1939 * Before this can be called, when receiving the registration information,
1940 * the application compatibility is checked. So, at this point, the
1941 * application can work with this session daemon.
1942 */
d0b96690 1943 lta->compatible = 1;
5b4a0ec0 1944
852d0037 1945 lta->pid = msg->pid;
d0b96690 1946 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 1947 lta->sock = sock;
d0b96690 1948 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 1949
d42f20df
DG
1950 CDS_INIT_LIST_HEAD(&lta->teardown_head);
1951
d0b96690
DG
1952error:
1953 return lta;
1954}
1955
d88aee68
DG
1956/*
1957 * For a given application object, add it to every hash table.
1958 */
d0b96690
DG
1959void ust_app_add(struct ust_app *app)
1960{
1961 assert(app);
1962 assert(app->notify_sock >= 0);
1963
5b4a0ec0 1964 rcu_read_lock();
852d0037
DG
1965
1966 /*
1967 * On a re-registration, we want to kick out the previous registration of
1968 * that pid
1969 */
d0b96690 1970 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
1971
1972 /*
1973 * The socket _should_ be unique until _we_ call close. So, a add_unique
1974 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1975 * already in the table.
1976 */
d0b96690 1977 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 1978
d0b96690
DG
1979 /* Add application to the notify socket hash table. */
1980 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
1981 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 1982
d0b96690 1983 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
d88aee68
DG
1984 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
1985 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
1986 app->v_minor);
5b4a0ec0 1987
d0b96690
DG
1988 rcu_read_unlock();
1989}
1990
d88aee68
DG
1991/*
1992 * Set the application version into the object.
1993 *
1994 * Return 0 on success else a negative value either an errno code or a
1995 * LTTng-UST error code.
1996 */
d0b96690
DG
1997int ust_app_version(struct ust_app *app)
1998{
d88aee68
DG
1999 int ret;
2000
d0b96690 2001 assert(app);
d88aee68
DG
2002
2003 ret = ustctl_tracer_version(app->sock, &app->version);
2004 if (ret < 0) {
2005 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
2006 ERR("UST app %d verson failed with ret %d", app->sock, ret);
2007 } else {
2008 DBG3("UST app %d verion failed. Application is dead", app->sock);
2009 }
2010 }
2011
2012 return ret;
5b4a0ec0
DG
2013}
2014
2015/*
2016 * Unregister app by removing it from the global traceable app list and freeing
2017 * the data struct.
2018 *
2019 * The socket is already closed at this point so no close to sock.
2020 */
2021void ust_app_unregister(int sock)
2022{
2023 struct ust_app *lta;
bec39940
DG
2024 struct lttng_ht_node_ulong *node;
2025 struct lttng_ht_iter iter;
d42f20df 2026 struct ust_app_session *ua_sess;
525b0740 2027 int ret;
5b4a0ec0
DG
2028
2029 rcu_read_lock();
886459c6 2030
5b4a0ec0 2031 /* Get the node reference for a call_rcu */
852d0037 2032 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 2033 node = lttng_ht_iter_get_node_ulong(&iter);
d0b96690 2034 assert(node);
284d8f55 2035
852d0037 2036 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
2037 DBG("PID %d unregistering with sock %d", lta->pid, sock);
2038
886459c6 2039 /* Remove application from PID hash table */
852d0037
DG
2040 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
2041 assert(!ret);
2042
d88aee68
DG
2043 /*
2044 * Remove application from notify hash table. The thread handling the
2045 * notify socket could have deleted the node so ignore on error because
2046 * either way it's valid. The close of that socket is handled by the other
2047 * thread.
2048 */
d0b96690 2049 iter.iter.node = &lta->notify_sock_n.node;
d88aee68 2050 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
852d0037 2051
5b98a774
DG
2052 /*
2053 * Ignore return value since the node might have been removed before by an
2054 * add replace during app registration because the PID can be reassigned by
2055 * the OS.
2056 */
d0b96690 2057 iter.iter.node = &lta->pid_n.node;
bec39940 2058 ret = lttng_ht_del(ust_app_ht, &iter);
5b98a774
DG
2059 if (ret) {
2060 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
2061 lta->pid);
2062 }
852d0037 2063
d42f20df
DG
2064 /* Remove sessions so they are not visible during deletion.*/
2065 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
2066 node.node) {
2067 ret = lttng_ht_del(lta->sessions, &iter);
2068 if (ret) {
2069 /* The session was already removed so scheduled for teardown. */
2070 continue;
2071 }
2072
2073 /*
2074 * Add session to list for teardown. This is safe since at this point we
2075 * are the only one using this list.
2076 */
d88aee68
DG
2077 pthread_mutex_lock(&ua_sess->lock);
2078
2079 /*
2080 * Normally, this is done in the delete session process which is
2081 * executed in the call rcu below. However, upon registration we can't
2082 * afford to wait for the grace period before pushing data or else the
2083 * data pending feature can race between the unregistration and stop
2084 * command where the data pending command is sent *before* the grace
2085 * period ended.
2086 *
2087 * The close metadata below nullifies the metadata pointer in the
2088 * session so the delete session will NOT push/close a second time.
2089 */
2090 (void) push_metadata(lta, ua_sess);
2091 (void) close_metadata(lta, ua_sess);
2092
d42f20df 2093 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
d88aee68 2094 pthread_mutex_unlock(&ua_sess->lock);
d42f20df
DG
2095 }
2096
852d0037
DG
2097 /* Free memory */
2098 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
2099
5b4a0ec0
DG
2100 rcu_read_unlock();
2101 return;
284d8f55
DG
2102}
2103
2104/*
5b4a0ec0 2105 * Return traceable_app_count
284d8f55 2106 */
5b4a0ec0 2107unsigned long ust_app_list_count(void)
284d8f55 2108{
5b4a0ec0 2109 unsigned long count;
284d8f55 2110
5b4a0ec0 2111 rcu_read_lock();
bec39940 2112 count = lttng_ht_get_count(ust_app_ht);
5b4a0ec0 2113 rcu_read_unlock();
284d8f55 2114
5b4a0ec0 2115 return count;
284d8f55
DG
2116}
2117
5b4a0ec0
DG
2118/*
2119 * Fill events array with all events name of all registered apps.
2120 */
2121int ust_app_list_events(struct lttng_event **events)
421cb601 2122{
5b4a0ec0
DG
2123 int ret, handle;
2124 size_t nbmem, count = 0;
bec39940 2125 struct lttng_ht_iter iter;
5b4a0ec0 2126 struct ust_app *app;
c617c0c6 2127 struct lttng_event *tmp_event;
421cb601 2128
5b4a0ec0 2129 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
2130 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
2131 if (tmp_event == NULL) {
5b4a0ec0
DG
2132 PERROR("zmalloc ust app events");
2133 ret = -ENOMEM;
421cb601
DG
2134 goto error;
2135 }
2136
5b4a0ec0 2137 rcu_read_lock();
421cb601 2138
852d0037 2139 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 2140 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 2141
840cb59c 2142 health_code_update();
86acf0da 2143
e0c7ec2b
DG
2144 if (!app->compatible) {
2145 /*
2146 * TODO: In time, we should notice the caller of this error by
2147 * telling him that this is a version error.
2148 */
2149 continue;
2150 }
852d0037 2151 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0 2152 if (handle < 0) {
ffe60014
DG
2153 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
2154 ERR("UST app list events getting handle failed for app pid %d",
2155 app->pid);
2156 }
5b4a0ec0
DG
2157 continue;
2158 }
421cb601 2159
852d0037 2160 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 2161 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
2162 /* Handle ustctl error. */
2163 if (ret < 0) {
2164 free(tmp_event);
2165 if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) {
2166 ERR("UST app tp list get failed for app %d with ret %d",
2167 app->sock, ret);
2168 } else {
2169 DBG3("UST app tp list get failed. Application is dead");
2170 }
2171 goto rcu_error;
2172 }
2173
840cb59c 2174 health_code_update();
815564d8 2175 if (count >= nbmem) {
d7b3776f 2176 /* In case the realloc fails, we free the memory */
c617c0c6
MD
2177 void *ptr;
2178
815564d8
MD
2179 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
2180 2 * nbmem);
2181 nbmem *= 2;
c617c0c6
MD
2182 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event));
2183 if (ptr == NULL) {
5b4a0ec0 2184 PERROR("realloc ust app events");
c617c0c6 2185 free(tmp_event);
5b4a0ec0
DG
2186 ret = -ENOMEM;
2187 goto rcu_error;
2188 }
c617c0c6 2189 tmp_event = ptr;
5b4a0ec0 2190 }
c617c0c6
MD
2191 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
2192 tmp_event[count].loglevel = uiter.loglevel;
2193 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
2194 tmp_event[count].pid = app->pid;
2195 tmp_event[count].enabled = -1;
5b4a0ec0 2196 count++;
421cb601 2197 }
421cb601
DG
2198 }
2199
5b4a0ec0 2200 ret = count;
c617c0c6 2201 *events = tmp_event;
421cb601 2202
5b4a0ec0 2203 DBG2("UST app list events done (%zu events)", count);
421cb601 2204
5b4a0ec0
DG
2205rcu_error:
2206 rcu_read_unlock();
421cb601 2207error:
840cb59c 2208 health_code_update();
5b4a0ec0 2209 return ret;
421cb601
DG
2210}
2211
f37d259d
MD
2212/*
2213 * Fill events array with all events name of all registered apps.
2214 */
2215int ust_app_list_event_fields(struct lttng_event_field **fields)
2216{
2217 int ret, handle;
2218 size_t nbmem, count = 0;
2219 struct lttng_ht_iter iter;
2220 struct ust_app *app;
c617c0c6 2221 struct lttng_event_field *tmp_event;
f37d259d
MD
2222
2223 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
2224 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
2225 if (tmp_event == NULL) {
f37d259d
MD
2226 PERROR("zmalloc ust app event fields");
2227 ret = -ENOMEM;
2228 goto error;
2229 }
2230
2231 rcu_read_lock();
2232
2233 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2234 struct lttng_ust_field_iter uiter;
2235
840cb59c 2236 health_code_update();
86acf0da 2237
f37d259d
MD
2238 if (!app->compatible) {
2239 /*
2240 * TODO: In time, we should notice the caller of this error by
2241 * telling him that this is a version error.
2242 */
2243 continue;
2244 }
2245 handle = ustctl_tracepoint_field_list(app->sock);
2246 if (handle < 0) {
ffe60014
DG
2247 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
2248 ERR("UST app list field getting handle failed for app pid %d",
2249 app->pid);
2250 }
f37d259d
MD
2251 continue;
2252 }
2253
2254 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 2255 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
2256 /* Handle ustctl error. */
2257 if (ret < 0) {
2258 free(tmp_event);
2259 if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) {
2260 ERR("UST app tp list field failed for app %d with ret %d",
2261 app->sock, ret);
2262 } else {
2263 DBG3("UST app tp list field failed. Application is dead");
2264 }
2265 goto rcu_error;
2266 }
2267
840cb59c 2268 health_code_update();
f37d259d 2269 if (count >= nbmem) {
d7b3776f 2270 /* In case the realloc fails, we free the memory */
c617c0c6
MD
2271 void *ptr;
2272
f37d259d
MD
2273 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
2274 2 * nbmem);
2275 nbmem *= 2;
c617c0c6
MD
2276 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field));
2277 if (ptr == NULL) {
f37d259d 2278 PERROR("realloc ust app event fields");
c617c0c6 2279 free(tmp_event);
f37d259d
MD
2280 ret = -ENOMEM;
2281 goto rcu_error;
2282 }
c617c0c6 2283 tmp_event = ptr;
f37d259d 2284 }
f37d259d 2285
c617c0c6
MD
2286 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
2287 tmp_event[count].type = uiter.type;
2288 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 2289
c617c0c6
MD
2290 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
2291 tmp_event[count].event.loglevel = uiter.loglevel;
2292 tmp_event[count].event.type = LTTNG_UST_TRACEPOINT;
2293 tmp_event[count].event.pid = app->pid;
2294 tmp_event[count].event.enabled = -1;
f37d259d
MD
2295 count++;
2296 }
2297 }
2298
2299 ret = count;
c617c0c6 2300 *fields = tmp_event;
f37d259d
MD
2301
2302 DBG2("UST app list event fields done (%zu events)", count);
2303
2304rcu_error:
2305 rcu_read_unlock();
2306error:
840cb59c 2307 health_code_update();
f37d259d
MD
2308 return ret;
2309}
2310
5b4a0ec0
DG
2311/*
2312 * Free and clean all traceable apps of the global list.
2313 */
2314void ust_app_clean_list(void)
421cb601 2315{
5b4a0ec0 2316 int ret;
659ed79f 2317 struct ust_app *app;
bec39940 2318 struct lttng_ht_iter iter;
421cb601 2319
5b4a0ec0 2320 DBG2("UST app cleaning registered apps hash table");
421cb601 2321
5b4a0ec0 2322 rcu_read_lock();
421cb601 2323
659ed79f 2324 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 2325 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 2326 assert(!ret);
659ed79f 2327 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
421cb601
DG
2328 }
2329
852d0037 2330 /* Cleanup socket hash table */
659ed79f
DG
2331 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
2332 sock_n.node) {
852d0037 2333 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
bec39940
DG
2334 assert(!ret);
2335 }
852d0037 2336
d88aee68
DG
2337 /* Cleanup notify socket hash table */
2338 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
2339 notify_sock_n.node) {
2340 ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
2341 assert(!ret);
2342 }
2343
bec39940 2344 /* Destroy is done only when the ht is empty */
852d0037
DG
2345 lttng_ht_destroy(ust_app_ht);
2346 lttng_ht_destroy(ust_app_ht_by_sock);
d88aee68 2347 lttng_ht_destroy(ust_app_ht_by_notify_sock);
421cb601 2348
5b4a0ec0
DG
2349 rcu_read_unlock();
2350}
2351
2352/*
2353 * Init UST app hash table.
2354 */
2355void ust_app_ht_alloc(void)
2356{
bec39940 2357 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
852d0037 2358 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690 2359 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
421cb601
DG
2360}
2361
78f0bacd
DG
2362/*
2363 * For a specific UST session, disable the channel for all registered apps.
2364 */
35a9059d 2365int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
2366 struct ltt_ust_channel *uchan)
2367{
2368 int ret = 0;
bec39940
DG
2369 struct lttng_ht_iter iter;
2370 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
2371 struct ust_app *app;
2372 struct ust_app_session *ua_sess;
8535a6d9 2373 struct ust_app_channel *ua_chan;
78f0bacd
DG
2374
2375 if (usess == NULL || uchan == NULL) {
2376 ERR("Disabling UST global channel with NULL values");
2377 ret = -1;
2378 goto error;
2379 }
2380
a991f516
MD
2381 DBG2("UST app disabling channel %s from global domain for session id %d",
2382 uchan->name, usess->id);
78f0bacd
DG
2383
2384 rcu_read_lock();
2385
2386 /* For every registered applications */
852d0037 2387 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 2388 struct lttng_ht_iter uiter;
e0c7ec2b
DG
2389 if (!app->compatible) {
2390 /*
2391 * TODO: In time, we should notice the caller of this error by
2392 * telling him that this is a version error.
2393 */
2394 continue;
2395 }
78f0bacd
DG
2396 ua_sess = lookup_session_by_app(usess, app);
2397 if (ua_sess == NULL) {
2398 continue;
2399 }
2400
8535a6d9 2401 /* Get channel */
bec39940
DG
2402 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2403 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
2404 /* If the session if found for the app, the channel must be there */
2405 assert(ua_chan_node);
2406
2407 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2408 /* The channel must not be already disabled */
2409 assert(ua_chan->enabled == 1);
2410
2411 /* Disable channel onto application */
2412 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
2413 if (ret < 0) {
2414 /* XXX: We might want to report this error at some point... */
2415 continue;
2416 }
2417 }
2418
2419 rcu_read_unlock();
2420
2421error:
2422 return ret;
2423}
2424
2425/*
2426 * For a specific UST session, enable the channel for all registered apps.
2427 */
35a9059d 2428int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
2429 struct ltt_ust_channel *uchan)
2430{
2431 int ret = 0;
bec39940 2432 struct lttng_ht_iter iter;
78f0bacd
DG
2433 struct ust_app *app;
2434 struct ust_app_session *ua_sess;
2435
2436 if (usess == NULL || uchan == NULL) {
2437 ERR("Adding UST global channel to NULL values");
2438 ret = -1;
2439 goto error;
2440 }
2441
a991f516
MD
2442 DBG2("UST app enabling channel %s to global domain for session id %d",
2443 uchan->name, usess->id);
78f0bacd
DG
2444
2445 rcu_read_lock();
2446
2447 /* For every registered applications */
852d0037 2448 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2449 if (!app->compatible) {
2450 /*
2451 * TODO: In time, we should notice the caller of this error by
2452 * telling him that this is a version error.
2453 */
2454 continue;
2455 }
78f0bacd
DG
2456 ua_sess = lookup_session_by_app(usess, app);
2457 if (ua_sess == NULL) {
2458 continue;
2459 }
2460
2461 /* Enable channel onto application */
2462 ret = enable_ust_app_channel(ua_sess, uchan, app);
2463 if (ret < 0) {
2464 /* XXX: We might want to report this error at some point... */
2465 continue;
2466 }
2467 }
2468
2469 rcu_read_unlock();
2470
2471error:
2472 return ret;
2473}
2474
b0a40d28
DG
2475/*
2476 * Disable an event in a channel and for a specific session.
2477 */
35a9059d
DG
2478int ust_app_disable_event_glb(struct ltt_ust_session *usess,
2479 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
2480{
2481 int ret = 0;
bec39940
DG
2482 struct lttng_ht_iter iter, uiter;
2483 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
b0a40d28
DG
2484 struct ust_app *app;
2485 struct ust_app_session *ua_sess;
2486 struct ust_app_channel *ua_chan;
2487 struct ust_app_event *ua_event;
2488
2489 DBG("UST app disabling event %s for all apps in channel "
a991f516 2490 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
2491
2492 rcu_read_lock();
2493
2494 /* For all registered applications */
852d0037 2495 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2496 if (!app->compatible) {
2497 /*
2498 * TODO: In time, we should notice the caller of this error by
2499 * telling him that this is a version error.
2500 */
2501 continue;
2502 }
b0a40d28
DG
2503 ua_sess = lookup_session_by_app(usess, app);
2504 if (ua_sess == NULL) {
2505 /* Next app */
2506 continue;
2507 }
2508
2509 /* Lookup channel in the ust app session */
bec39940
DG
2510 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2511 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 2512 if (ua_chan_node == NULL) {
a991f516 2513 DBG2("Channel %s not found in session id %d for app pid %d."
852d0037 2514 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
2515 continue;
2516 }
2517 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2518
bec39940
DG
2519 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2520 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28
DG
2521 if (ua_event_node == NULL) {
2522 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 2523 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
2524 continue;
2525 }
2526 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2527
7f79d3a1 2528 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
2529 if (ret < 0) {
2530 /* XXX: Report error someday... */
2531 continue;
2532 }
2533 }
2534
2535 rcu_read_unlock();
2536
2537 return ret;
2538}
2539
9730260e 2540/*
edb67388 2541 * For a specific UST session and UST channel, the event for all
9730260e
DG
2542 * registered apps.
2543 */
35a9059d 2544int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
9730260e
DG
2545 struct ltt_ust_channel *uchan)
2546{
2547 int ret = 0;
bec39940
DG
2548 struct lttng_ht_iter iter, uiter;
2549 struct lttng_ht_node_str *ua_chan_node;
9730260e
DG
2550 struct ust_app *app;
2551 struct ust_app_session *ua_sess;
2552 struct ust_app_channel *ua_chan;
2553 struct ust_app_event *ua_event;
2554
2555 DBG("UST app disabling all event for all apps in channel "
a991f516 2556 "%s for session id %d", uchan->name, usess->id);
9730260e
DG
2557
2558 rcu_read_lock();
2559
2560 /* For all registered applications */
852d0037 2561 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2562 if (!app->compatible) {
2563 /*
2564 * TODO: In time, we should notice the caller of this error by
2565 * telling him that this is a version error.
2566 */
2567 continue;
2568 }
9730260e 2569 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2570 if (!ua_sess) {
2571 /* The application has problem or is probably dead. */
2572 continue;
2573 }
9730260e
DG
2574
2575 /* Lookup channel in the ust app session */
bec39940
DG
2576 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2577 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2578 /* If the channel is not found, there is a code flow error */
2579 assert(ua_chan_node);
2580
9730260e
DG
2581 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2582
2583 /* Disable each events of channel */
bec39940
DG
2584 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2585 node.node) {
7f79d3a1 2586 ret = disable_ust_app_event(ua_sess, ua_event, app);
9730260e
DG
2587 if (ret < 0) {
2588 /* XXX: Report error someday... */
2589 continue;
2590 }
2591 }
2592 }
2593
2594 rcu_read_unlock();
2595
2596 return ret;
2597}
2598
421cb601 2599/*
5b4a0ec0 2600 * For a specific UST session, create the channel for all registered apps.
421cb601 2601 */
35a9059d 2602int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
2603 struct ltt_ust_channel *uchan)
2604{
3d8ca23b 2605 int ret = 0, created;
bec39940 2606 struct lttng_ht_iter iter;
48842b30 2607 struct ust_app *app;
3d8ca23b 2608 struct ust_app_session *ua_sess = NULL;
48842b30 2609
fc34caaa
DG
2610 /* Very wrong code flow */
2611 assert(usess);
2612 assert(uchan);
421cb601 2613
a991f516
MD
2614 DBG2("UST app adding channel %s to global domain for session id %d",
2615 uchan->name, usess->id);
48842b30
DG
2616
2617 rcu_read_lock();
421cb601 2618
5b4a0ec0 2619 /* For every registered applications */
852d0037 2620 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2621 if (!app->compatible) {
2622 /*
2623 * TODO: In time, we should notice the caller of this error by
2624 * telling him that this is a version error.
2625 */
2626 continue;
2627 }
edb67388
DG
2628 /*
2629 * Create session on the tracer side and add it to app session HT. Note
2630 * that if session exist, it will simply return a pointer to the ust
2631 * app session.
2632 */
3d8ca23b
DG
2633 ret = create_ust_app_session(usess, app, &ua_sess, &created);
2634 if (ret < 0) {
2635 switch (ret) {
2636 case -ENOTCONN:
2637 /*
2638 * The application's socket is not valid. Either a bad socket
2639 * or a timeout on it. We can't inform the caller that for a
2640 * specific app, the session failed so lets continue here.
2641 */
2642 continue;
2643 case -ENOMEM:
2644 default:
2645 goto error_rcu_unlock;
2646 }
48842b30 2647 }
3d8ca23b 2648 assert(ua_sess);
48842b30 2649
d0b96690 2650 pthread_mutex_lock(&ua_sess->lock);
4d710ac2 2651 /* Create channel onto application. We don't need the chan ref. */
ffe60014
DG
2652 ret = create_ust_app_channel(ua_sess, uchan, app, usess->consumer,
2653 LTTNG_UST_CHAN_PER_CPU, NULL);
d0b96690 2654 pthread_mutex_unlock(&ua_sess->lock);
3d8ca23b
DG
2655 if (ret < 0) {
2656 if (ret == -ENOMEM) {
2657 /* No more memory is a fatal error. Stop right now. */
2658 goto error_rcu_unlock;
2659 }
2660 /* Cleanup the created session if it's the case. */
2661 if (created) {
d0b96690 2662 destroy_app_session(app, ua_sess);
3d8ca23b 2663 }
48842b30 2664 }
48842b30 2665 }
5b4a0ec0 2666
95e047ff 2667error_rcu_unlock:
48842b30 2668 rcu_read_unlock();
3c14c33f 2669 return ret;
48842b30
DG
2670}
2671
5b4a0ec0 2672/*
edb67388 2673 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 2674 */
35a9059d 2675int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
2676 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2677{
2678 int ret = 0;
bec39940 2679 struct lttng_ht_iter iter, uiter;
18eace3b 2680 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
2681 struct ust_app *app;
2682 struct ust_app_session *ua_sess;
2683 struct ust_app_channel *ua_chan;
2684 struct ust_app_event *ua_event;
48842b30 2685
a991f516
MD
2686 DBG("UST app enabling event %s for all apps for session id %d",
2687 uevent->attr.name, usess->id);
48842b30 2688
edb67388
DG
2689 /*
2690 * NOTE: At this point, this function is called only if the session and
2691 * channel passed are already created for all apps. and enabled on the
2692 * tracer also.
2693 */
2694
48842b30 2695 rcu_read_lock();
421cb601
DG
2696
2697 /* For all registered applications */
852d0037 2698 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2699 if (!app->compatible) {
2700 /*
2701 * TODO: In time, we should notice the caller of this error by
2702 * telling him that this is a version error.
2703 */
2704 continue;
2705 }
edb67388 2706 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2707 if (!ua_sess) {
2708 /* The application has problem or is probably dead. */
2709 continue;
2710 }
ba767faf 2711
d0b96690
DG
2712 pthread_mutex_lock(&ua_sess->lock);
2713
edb67388 2714 /* Lookup channel in the ust app session */
bec39940
DG
2715 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2716 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2717 /* If the channel is not found, there is a code flow error */
2718 assert(ua_chan_node);
2719
2720 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2721
18eace3b
DG
2722 /* Get event node */
2723 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2724 uevent->filter, uevent->attr.loglevel);
2725 if (ua_event == NULL) {
7f79d3a1 2726 DBG3("UST app enable event %s not found for app PID %d."
852d0037 2727 "Skipping app", uevent->attr.name, app->pid);
d0b96690 2728 goto next_app;
35a9059d 2729 }
35a9059d
DG
2730
2731 ret = enable_ust_app_event(ua_sess, ua_event, app);
2732 if (ret < 0) {
d0b96690 2733 pthread_mutex_unlock(&ua_sess->lock);
7f79d3a1 2734 goto error;
48842b30 2735 }
d0b96690
DG
2736 next_app:
2737 pthread_mutex_unlock(&ua_sess->lock);
edb67388
DG
2738 }
2739
7f79d3a1 2740error:
edb67388 2741 rcu_read_unlock();
edb67388
DG
2742 return ret;
2743}
2744
2745/*
2746 * For a specific existing UST session and UST channel, creates the event for
2747 * all registered apps.
2748 */
35a9059d 2749int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
2750 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2751{
2752 int ret = 0;
bec39940
DG
2753 struct lttng_ht_iter iter, uiter;
2754 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
2755 struct ust_app *app;
2756 struct ust_app_session *ua_sess;
2757 struct ust_app_channel *ua_chan;
2758
a991f516
MD
2759 DBG("UST app creating event %s for all apps for session id %d",
2760 uevent->attr.name, usess->id);
edb67388 2761
edb67388
DG
2762 rcu_read_lock();
2763
2764 /* For all registered applications */
852d0037 2765 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2766 if (!app->compatible) {
2767 /*
2768 * TODO: In time, we should notice the caller of this error by
2769 * telling him that this is a version error.
2770 */
2771 continue;
2772 }
edb67388 2773 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2774 if (!ua_sess) {
2775 /* The application has problem or is probably dead. */
2776 continue;
2777 }
48842b30 2778
d0b96690 2779 pthread_mutex_lock(&ua_sess->lock);
48842b30 2780 /* Lookup channel in the ust app session */
bec39940
DG
2781 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2782 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2783 /* If the channel is not found, there is a code flow error */
2784 assert(ua_chan_node);
2785
48842b30
DG
2786 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2787
edb67388 2788 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
d0b96690 2789 pthread_mutex_unlock(&ua_sess->lock);
edb67388 2790 if (ret < 0) {
49c336c1 2791 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
2792 /* Possible value at this point: -ENOMEM. If so, we stop! */
2793 break;
2794 }
2795 DBG2("UST app event %s already exist on app PID %d",
852d0037 2796 uevent->attr.name, app->pid);
5b4a0ec0 2797 continue;
48842b30 2798 }
48842b30 2799 }
5b4a0ec0 2800
48842b30
DG
2801 rcu_read_unlock();
2802
2803 return ret;
2804}
2805
5b4a0ec0
DG
2806/*
2807 * Start tracing for a specific UST session and app.
2808 */
421cb601 2809int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
2810{
2811 int ret = 0;
48842b30 2812 struct ust_app_session *ua_sess;
48842b30 2813
852d0037 2814 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 2815
509cbaf8
MD
2816 rcu_read_lock();
2817
e0c7ec2b
DG
2818 if (!app->compatible) {
2819 goto end;
2820 }
2821
421cb601
DG
2822 ua_sess = lookup_session_by_app(usess, app);
2823 if (ua_sess == NULL) {
d42f20df
DG
2824 /* The session is in teardown process. Ignore and continue. */
2825 goto end;
421cb601 2826 }
48842b30 2827
d0b96690
DG
2828 pthread_mutex_lock(&ua_sess->lock);
2829
aea829b3
DG
2830 /* Upon restart, we skip the setup, already done */
2831 if (ua_sess->started) {
8be98f9a 2832 goto skip_setup;
aea829b3 2833 }
8be98f9a 2834
a4b92340
DG
2835 /* Create directories if consumer is LOCAL and has a path defined. */
2836 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2837 strlen(usess->consumer->dst.trace_path) > 0) {
2838 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2839 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2840 if (ret < 0) {
2841 if (ret != -EEXIST) {
2842 ERR("Trace directory creation error");
d0b96690 2843 goto error_unlock;
421cb601 2844 }
173af62f 2845 }
7753dea8 2846 }
aea829b3 2847
ffe60014
DG
2848 /* Create the metadata for the application. */
2849 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
421cb601 2850 if (ret < 0) {
d0b96690 2851 goto error_unlock;
421cb601 2852 }
48842b30 2853
840cb59c 2854 health_code_update();
86acf0da 2855
8be98f9a 2856skip_setup:
421cb601 2857 /* This start the UST tracing */
852d0037 2858 ret = ustctl_start_session(app->sock, ua_sess->handle);
421cb601 2859 if (ret < 0) {
ffe60014
DG
2860 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2861 ERR("Error starting tracing for app pid: %d (ret: %d)",
2862 app->pid, ret);
2863 } else {
2864 DBG("UST app start session failed. Application is dead.");
2865 }
d0b96690 2866 goto error_unlock;
421cb601 2867 }
5b4a0ec0 2868
55c3953d
DG
2869 /* Indicate that the session has been started once */
2870 ua_sess->started = 1;
2871
d0b96690
DG
2872 pthread_mutex_unlock(&ua_sess->lock);
2873
840cb59c 2874 health_code_update();
86acf0da 2875
421cb601 2876 /* Quiescent wait after starting trace */
ffe60014
DG
2877 ret = ustctl_wait_quiescent(app->sock);
2878 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2879 ERR("UST app wait quiescent failed for app pid %d ret %d",
2880 app->pid, ret);
2881 }
48842b30 2882
e0c7ec2b
DG
2883end:
2884 rcu_read_unlock();
840cb59c 2885 health_code_update();
421cb601 2886 return 0;
48842b30 2887
d0b96690
DG
2888error_unlock:
2889 pthread_mutex_unlock(&ua_sess->lock);
509cbaf8 2890 rcu_read_unlock();
840cb59c 2891 health_code_update();
421cb601
DG
2892 return -1;
2893}
48842b30 2894
8be98f9a
MD
2895/*
2896 * Stop tracing for a specific UST session and app.
2897 */
2898int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2899{
2900 int ret = 0;
bec39940 2901 struct lttng_ht_iter iter;
8be98f9a 2902 struct ust_app_session *ua_sess;
6d3686da 2903 struct ust_app_channel *ua_chan;
8be98f9a 2904
852d0037 2905 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
2906
2907 rcu_read_lock();
2908
e0c7ec2b 2909 if (!app->compatible) {
d88aee68 2910 goto end_no_session;
e0c7ec2b
DG
2911 }
2912
8be98f9a
MD
2913 ua_sess = lookup_session_by_app(usess, app);
2914 if (ua_sess == NULL) {
d88aee68 2915 goto end_no_session;
8be98f9a
MD
2916 }
2917
d88aee68
DG
2918 pthread_mutex_lock(&ua_sess->lock);
2919
9bc07046
DG
2920 /*
2921 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
2922 * that was never started. It's possible since we can have a fail start
2923 * from either the application manager thread or the command thread. Simply
2924 * indicate that this is a stop error.
9bc07046 2925 */
f9dfc3d9 2926 if (!ua_sess->started) {
c45536e1
DG
2927 goto error_rcu_unlock;
2928 }
7db205b5 2929
840cb59c 2930 health_code_update();
86acf0da 2931
9d6c7d3f 2932 /* This inhibits UST tracing */
852d0037 2933 ret = ustctl_stop_session(app->sock, ua_sess->handle);
9d6c7d3f 2934 if (ret < 0) {
ffe60014
DG
2935 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2936 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2937 app->pid, ret);
2938 } else {
2939 DBG("UST app stop session failed. Application is dead.");
2940 }
9d6c7d3f
DG
2941 goto error_rcu_unlock;
2942 }
2943
840cb59c 2944 health_code_update();
86acf0da 2945
9d6c7d3f 2946 /* Quiescent wait after stopping trace */
ffe60014
DG
2947 ret = ustctl_wait_quiescent(app->sock);
2948 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2949 ERR("UST app wait quiescent failed for app pid %d ret %d",
2950 app->pid, ret);
2951 }
9d6c7d3f 2952
840cb59c 2953 health_code_update();
86acf0da 2954
9d6c7d3f 2955 /* Flushing buffers */
bec39940
DG
2956 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2957 node.node) {
840cb59c 2958 health_code_update();
ffe60014 2959 assert(ua_chan->is_sent);
852d0037 2960 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
8be98f9a 2961 if (ret < 0) {
ffe60014
DG
2962 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2963 ERR("UST app PID %d channel %s flush failed with ret %d",
2964 app->pid, ua_chan->name, ret);
2965 } else {
2966 DBG3("UST app failed to flush %s. Application is dead.",
2967 ua_chan->name);
2968 /* No need to continue. */
d88aee68 2969 break;
ffe60014 2970 }
6d3686da
DG
2971 /* Continuing flushing all buffers */
2972 continue;
8be98f9a
MD
2973 }
2974 }
8be98f9a 2975
840cb59c 2976 health_code_update();
86acf0da 2977
d88aee68 2978 ret = push_metadata(app, ua_sess);
90d97d10 2979 if (ret < 0) {
d88aee68 2980 goto error_rcu_unlock;
90d97d10
DG
2981 }
2982
d88aee68
DG
2983 pthread_mutex_unlock(&ua_sess->lock);
2984end_no_session:
7db205b5 2985 rcu_read_unlock();
840cb59c 2986 health_code_update();
8be98f9a
MD
2987 return 0;
2988
2989error_rcu_unlock:
d88aee68 2990 pthread_mutex_unlock(&ua_sess->lock);
8be98f9a 2991 rcu_read_unlock();
840cb59c 2992 health_code_update();
8be98f9a
MD
2993 return -1;
2994}
2995
84cd17c6
MD
2996/*
2997 * Destroy a specific UST session in apps.
2998 */
3353de95 2999static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6 3000{
ffe60014 3001 int ret;
84cd17c6 3002 struct ust_app_session *ua_sess;
bec39940
DG
3003 struct lttng_ht_iter iter;
3004 struct lttng_ht_node_ulong *node;
84cd17c6 3005
852d0037 3006 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
3007
3008 rcu_read_lock();
3009
e0c7ec2b
DG
3010 if (!app->compatible) {
3011 goto end;
3012 }
3013
84cd17c6 3014 __lookup_session_by_app(usess, app, &iter);
bec39940 3015 node = lttng_ht_iter_get_node_ulong(&iter);
84cd17c6 3016 if (node == NULL) {
d42f20df
DG
3017 /* Session is being or is deleted. */
3018 goto end;
84cd17c6
MD
3019 }
3020 ua_sess = caa_container_of(node, struct ust_app_session, node);
c4a1715b 3021
840cb59c 3022 health_code_update();
d0b96690 3023 destroy_app_session(app, ua_sess);
84cd17c6 3024
840cb59c 3025 health_code_update();
7db205b5 3026
84cd17c6 3027 /* Quiescent wait after stopping trace */
ffe60014
DG
3028 ret = ustctl_wait_quiescent(app->sock);
3029 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3030 ERR("UST app wait quiescent failed for app pid %d ret %d",
3031 app->pid, ret);
3032 }
e0c7ec2b
DG
3033end:
3034 rcu_read_unlock();
840cb59c 3035 health_code_update();
84cd17c6 3036 return 0;
84cd17c6
MD
3037}
3038
5b4a0ec0
DG
3039/*
3040 * Start tracing for the UST session.
3041 */
421cb601
DG
3042int ust_app_start_trace_all(struct ltt_ust_session *usess)
3043{
3044 int ret = 0;
bec39940 3045 struct lttng_ht_iter iter;
421cb601 3046 struct ust_app *app;
48842b30 3047
421cb601
DG
3048 DBG("Starting all UST traces");
3049
3050 rcu_read_lock();
421cb601 3051
852d0037 3052 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
421cb601 3053 ret = ust_app_start_trace(usess, app);
48842b30 3054 if (ret < 0) {
5b4a0ec0
DG
3055 /* Continue to next apps even on error */
3056 continue;
48842b30 3057 }
48842b30 3058 }
5b4a0ec0 3059
48842b30
DG
3060 rcu_read_unlock();
3061
3062 return 0;
3063}
487cf67c 3064
8be98f9a
MD
3065/*
3066 * Start tracing for the UST session.
3067 */
3068int ust_app_stop_trace_all(struct ltt_ust_session *usess)
3069{
3070 int ret = 0;
bec39940 3071 struct lttng_ht_iter iter;
8be98f9a
MD
3072 struct ust_app *app;
3073
3074 DBG("Stopping all UST traces");
3075
3076 rcu_read_lock();
3077
852d0037 3078 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
8be98f9a
MD
3079 ret = ust_app_stop_trace(usess, app);
3080 if (ret < 0) {
d88aee68 3081 ERR("UST app stop trace failed with ret %d", ret);
8be98f9a
MD
3082 /* Continue to next apps even on error */
3083 continue;
3084 }
3085 }
3086
3087 rcu_read_unlock();
3088
3089 return 0;
3090}
3091
84cd17c6
MD
3092/*
3093 * Destroy app UST session.
3094 */
3095int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
3096{
3097 int ret = 0;
bec39940 3098 struct lttng_ht_iter iter;
84cd17c6
MD
3099 struct ust_app *app;
3100
3101 DBG("Destroy all UST traces");
3102
3103 rcu_read_lock();
3104
852d0037 3105 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 3106 ret = destroy_trace(usess, app);
84cd17c6
MD
3107 if (ret < 0) {
3108 /* Continue to next apps even on error */
3109 continue;
3110 }
3111 }
3112
3113 rcu_read_unlock();
3114
3115 return 0;
3116}
3117
5b4a0ec0
DG
3118/*
3119 * Add channels/events from UST global domain to registered apps at sock.
3120 */
487cf67c
DG
3121void ust_app_global_update(struct ltt_ust_session *usess, int sock)
3122{
55c54cce 3123 int ret = 0;
727d5404 3124 struct lttng_ht_iter iter, uiter, iter_ctx;
487cf67c 3125 struct ust_app *app;
3d8ca23b 3126 struct ust_app_session *ua_sess = NULL;
487cf67c
DG
3127 struct ust_app_channel *ua_chan;
3128 struct ust_app_event *ua_event;
727d5404 3129 struct ust_app_ctx *ua_ctx;
1f3580c7 3130
95e047ff 3131 assert(usess);
ffe60014 3132 assert(sock >= 0);
1f3580c7 3133
a991f516
MD
3134 DBG2("UST app global update for app sock %d for session id %d", sock,
3135 usess->id);
487cf67c 3136
284d8f55
DG
3137 rcu_read_lock();
3138
487cf67c
DG
3139 app = find_app_by_sock(sock);
3140 if (app == NULL) {
d88aee68
DG
3141 /*
3142 * Application can be unregistered before so this is possible hence
3143 * simply stopping the update.
3144 */
3145 DBG3("UST app update failed to find app sock %d", sock);
487cf67c
DG
3146 goto error;
3147 }
3148
e0c7ec2b
DG
3149 if (!app->compatible) {
3150 goto error;
3151 }
3152
3d8ca23b
DG
3153 ret = create_ust_app_session(usess, app, &ua_sess, NULL);
3154 if (ret < 0) {
3155 /* Tracer is probably gone or ENOMEM. */
487cf67c
DG
3156 goto error;
3157 }
3d8ca23b 3158 assert(ua_sess);
487cf67c 3159
d0b96690
DG
3160 pthread_mutex_lock(&ua_sess->lock);
3161
284d8f55 3162 /*
d0b96690 3163 * We can iterate safely here over all UST app session since the create ust
284d8f55
DG
3164 * app session above made a shadow copy of the UST global domain from the
3165 * ltt ust session.
3166 */
bec39940
DG
3167 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
3168 node.node) {
ffe60014 3169 ret = create_ust_channel(app, ua_sess, ua_chan, usess->consumer);
284d8f55 3170 if (ret < 0) {
ffe60014
DG
3171 /*
3172 * Stop everything. On error, the application failed, no more file
3173