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