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