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