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