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