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