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