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