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