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