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