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