Remove application from socket hash table upon unregister
[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) {
587 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
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
DG
594 ua_chan->handle = ua_chan->obj->handle;
595 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
596 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
597 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
b551a063 598
5b4a0ec0
DG
599 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
600 ua_chan->name, app->key.pid, app->key.sock);
b551a063 601
8535a6d9
DG
602 /* If channel is not enabled, disable it on the tracer */
603 if (!ua_chan->enabled) {
604 ret = disable_ust_channel(app, ua_sess, ua_chan);
605 if (ret < 0) {
606 goto error;
607 }
608 }
609
b551a063
DG
610error:
611 return ret;
612}
613
91d76f53 614/*
5b4a0ec0 615 * Create the specified event onto the UST tracer for a UST session.
91d76f53 616 */
edb67388
DG
617static
618int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
619 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 620{
5b4a0ec0 621 int ret = 0;
284d8f55 622
5b4a0ec0
DG
623 /* Create UST event on tracer */
624 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
625 &ua_event->obj);
626 if (ret < 0) {
627 ERR("Error ustctl create event %s for app pid: %d with ret %d",
628 ua_event->attr.name, app->key.pid, ret);
629 goto error;
91d76f53 630 }
f6a9efaa 631
5b4a0ec0 632 ua_event->handle = ua_event->obj->handle;
284d8f55 633
5b4a0ec0
DG
634 DBG2("UST app event %s created successfully for pid:%d",
635 ua_event->attr.name, app->key.pid);
f6a9efaa 636
8535a6d9
DG
637 /* If event not enabled, disable it on the tracer */
638 if (!ua_event->enabled) {
639 ret = disable_ust_event(app, ua_sess, ua_event);
640 if (ret < 0) {
641 goto error;
642 }
643 }
644
5b4a0ec0
DG
645error:
646 return ret;
91d76f53 647}
48842b30 648
5b4a0ec0
DG
649/*
650 * Copy data between an UST app event and a LTT event.
651 */
421cb601 652static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
653 struct ltt_ust_event *uevent)
654{
bec39940 655 struct lttng_ht_iter iter;
55cc08a6
DG
656 struct ltt_ust_context *uctx;
657 struct ust_app_ctx *ua_ctx;
658
48842b30
DG
659 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
660 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
661
5b4a0ec0
DG
662 /* Copy event attributes */
663 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
664
bec39940 665 cds_lfht_for_each_entry(uevent->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
666 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
667 if (ua_ctx == NULL) {
668 continue;
669 }
bec39940
DG
670 lttng_ht_node_init_ulong(&ua_ctx->node,
671 (unsigned long) ua_ctx->ctx.ctx);
672 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
55cc08a6 673 }
48842b30
DG
674}
675
5b4a0ec0
DG
676/*
677 * Copy data between an UST app channel and a LTT channel.
678 */
421cb601 679static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
680 struct ltt_ust_channel *uchan)
681{
bec39940
DG
682 struct lttng_ht_iter iter;
683 struct lttng_ht_node_str *ua_event_node;
48842b30 684 struct ltt_ust_event *uevent;
55cc08a6 685 struct ltt_ust_context *uctx;
48842b30 686 struct ust_app_event *ua_event;
55cc08a6 687 struct ust_app_ctx *ua_ctx;
48842b30 688
421cb601 689 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
48842b30
DG
690
691 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
692 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
5b4a0ec0
DG
693 /* Copy event attributes */
694 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
48842b30 695
bec39940 696 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
697 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
698 if (ua_ctx == NULL) {
699 continue;
700 }
bec39940
DG
701 lttng_ht_node_init_ulong(&ua_ctx->node,
702 (unsigned long) ua_ctx->ctx.ctx);
703 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6 704 }
48842b30 705
421cb601 706 /* Copy all events from ltt ust channel to ust app channel */
bec39940
DG
707 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
708 struct lttng_ht_iter uiter;
ba767faf 709
bec39940
DG
710 lttng_ht_lookup(ua_chan->events, (void *) uevent->attr.name, &uiter);
711 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
48842b30 712 if (ua_event_node == NULL) {
421cb601 713 DBG2("UST event %s not found on shadow copy channel",
48842b30 714 uevent->attr.name);
284d8f55 715 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 716 if (ua_event == NULL) {
5b4a0ec0 717 continue;
48842b30 718 }
421cb601 719 shadow_copy_event(ua_event, uevent);
bec39940 720 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
48842b30 721 }
48842b30
DG
722 }
723
421cb601 724 DBG3("Shadow copy channel done");
48842b30
DG
725}
726
5b4a0ec0
DG
727/*
728 * Copy data between a UST app session and a regular LTT session.
729 */
421cb601 730static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 731 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 732{
bec39940
DG
733 struct lttng_ht_node_str *ua_chan_node;
734 struct lttng_ht_iter iter;
48842b30
DG
735 struct ltt_ust_channel *uchan;
736 struct ust_app_channel *ua_chan;
477d7741
MD
737 time_t rawtime;
738 struct tm *timeinfo;
739 char datetime[16];
740 int ret;
741
742 /* Get date and time for unique app path */
743 time(&rawtime);
744 timeinfo = localtime(&rawtime);
745 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 746
421cb601 747 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 748
a991f516 749 ua_sess->id = usess->id;
6df2e2c9
MD
750 ua_sess->uid = usess->uid;
751 ua_sess->gid = usess->gid;
48842b30 752
477d7741
MD
753 ret = snprintf(ua_sess->path, PATH_MAX,
754 "%s/%s-%d-%s",
755 usess->pathname, app->name, app->key.pid,
756 datetime);
757 if (ret < 0) {
758 PERROR("asprintf UST shadow copy session");
759 /* TODO: We cannot return an error from here.. */
760 assert(0);
761 }
762
48842b30
DG
763 /* TODO: support all UST domain */
764
765 /* Iterate over all channels in global domain. */
bec39940
DG
766 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
767 uchan, node.node) {
768 struct lttng_ht_iter uiter;
ba767faf 769
bec39940
DG
770 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
771 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0
DG
772 if (ua_chan_node != NULL) {
773 continue;
774 }
421cb601 775
5b4a0ec0
DG
776 DBG2("Channel %s not found on shadow session copy, creating it",
777 uchan->name);
778 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
779 if (ua_chan == NULL) {
780 /* malloc failed... continuing */
781 continue;
48842b30
DG
782 }
783
5b4a0ec0 784 shadow_copy_channel(ua_chan, uchan);
bec39940 785 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30
DG
786 }
787}
788
78f0bacd
DG
789/*
790 * Lookup sesison wrapper.
791 */
84cd17c6
MD
792static
793void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 794 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
795{
796 /* Get right UST app session from app */
2c348c10 797 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
84cd17c6
MD
798}
799
421cb601
DG
800/*
801 * Return ust app session from the app session hashtable using the UST session
a991f516 802 * id.
421cb601 803 */
48842b30
DG
804static struct ust_app_session *lookup_session_by_app(
805 struct ltt_ust_session *usess, struct ust_app *app)
806{
bec39940
DG
807 struct lttng_ht_iter iter;
808 struct lttng_ht_node_ulong *node;
48842b30 809
84cd17c6 810 __lookup_session_by_app(usess, app, &iter);
bec39940 811 node = lttng_ht_iter_get_node_ulong(&iter);
48842b30
DG
812 if (node == NULL) {
813 goto error;
814 }
815
816 return caa_container_of(node, struct ust_app_session, node);
817
818error:
819 return NULL;
820}
821
421cb601
DG
822/*
823 * Create a UST session onto the tracer of app and add it the session
824 * hashtable.
825 *
826 * Return ust app session or NULL on error.
827 */
828static struct ust_app_session *create_ust_app_session(
829 struct ltt_ust_session *usess, struct ust_app *app)
830{
831 int ret;
832 struct ust_app_session *ua_sess;
833
834 ua_sess = lookup_session_by_app(usess, app);
835 if (ua_sess == NULL) {
a991f516
MD
836 DBG2("UST app pid: %d session id %d not found, creating it",
837 app->key.pid, usess->id);
421cb601
DG
838 ua_sess = alloc_ust_app_session();
839 if (ua_sess == NULL) {
840 /* Only malloc can failed so something is really wrong */
841 goto error;
842 }
477d7741 843 shadow_copy_session(ua_sess, usess, app);
421cb601
DG
844 }
845
846 if (ua_sess->handle == -1) {
847 ret = ustctl_create_session(app->key.sock);
848 if (ret < 0) {
849 ERR("Error creating session for app pid %d, sock %d",
850 app->key.pid, app->key.sock);
851 /* TODO: free() ua_sess */
852 goto error;
853 }
854
855 DBG2("UST app ustctl create session handle %d", ret);
856 ua_sess->handle = ret;
857
858 /* Add ust app session to app's HT */
2c348c10 859 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
bec39940 860 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
421cb601
DG
861
862 DBG2("UST app session created successfully with handle %d", ret);
863 }
864
865 return ua_sess;
866
867error:
868 return NULL;
869}
870
55cc08a6
DG
871/*
872 * Create a context for the channel on the tracer.
873 */
874static
875int create_ust_app_channel_context(struct ust_app_session *ua_sess,
876 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
877 struct ust_app *app)
878{
879 int ret = 0;
bec39940
DG
880 struct lttng_ht_iter iter;
881 struct lttng_ht_node_ulong *node;
55cc08a6
DG
882 struct ust_app_ctx *ua_ctx;
883
884 DBG2("UST app adding context to channel %s", ua_chan->name);
885
bec39940
DG
886 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
887 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
888 if (node != NULL) {
889 ret = -EEXIST;
890 goto error;
891 }
892
893 ua_ctx = alloc_ust_app_ctx(uctx);
894 if (ua_ctx == NULL) {
895 /* malloc failed */
896 ret = -1;
897 goto error;
898 }
899
bec39940
DG
900 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
901 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6
DG
902
903 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
904 if (ret < 0) {
905 goto error;
906 }
907
908error:
909 return ret;
910}
911
912/*
913 * Create an UST context and enable it for the event on the tracer.
914 */
915static
916int create_ust_app_event_context(struct ust_app_session *ua_sess,
917 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
918 struct ust_app *app)
919{
920 int ret = 0;
bec39940
DG
921 struct lttng_ht_iter iter;
922 struct lttng_ht_node_ulong *node;
55cc08a6
DG
923 struct ust_app_ctx *ua_ctx;
924
925 DBG2("UST app adding context to event %s", ua_event->name);
926
bec39940
DG
927 lttng_ht_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx), &iter);
928 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
929 if (node != NULL) {
930 ret = -EEXIST;
931 goto error;
932 }
933
934 ua_ctx = alloc_ust_app_ctx(uctx);
935 if (ua_ctx == NULL) {
936 /* malloc failed */
937 ret = -1;
938 goto error;
939 }
940
bec39940
DG
941 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
942 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
55cc08a6
DG
943
944 ret = create_ust_event_context(ua_event, ua_ctx, app);
945 if (ret < 0) {
946 goto error;
947 }
948
949error:
950 return ret;
951}
952
edb67388
DG
953/*
954 * Enable on the tracer side a ust app event for the session and channel.
955 */
956static
957int enable_ust_app_event(struct ust_app_session *ua_sess,
35a9059d 958 struct ust_app_event *ua_event, struct ust_app *app)
edb67388
DG
959{
960 int ret;
961
962 ret = enable_ust_event(app, ua_sess, ua_event);
963 if (ret < 0) {
964 goto error;
965 }
966
967 ua_event->enabled = 1;
968
969error:
970 return ret;
971}
972
9730260e
DG
973/*
974 * Disable on the tracer side a ust app event for the session and channel.
975 */
976static int disable_ust_app_event(struct ust_app_session *ua_sess,
7f79d3a1 977 struct ust_app_event *ua_event, struct ust_app *app)
9730260e
DG
978{
979 int ret;
980
981 ret = disable_ust_event(app, ua_sess, ua_event);
982 if (ret < 0) {
983 goto error;
984 }
985
986 ua_event->enabled = 0;
987
988error:
989 return ret;
990}
991
78f0bacd
DG
992/*
993 * Lookup ust app channel for session and disable it on the tracer side.
994 */
8535a6d9
DG
995static
996int disable_ust_app_channel(struct ust_app_session *ua_sess,
997 struct ust_app_channel *ua_chan, struct ust_app *app)
78f0bacd 998{
8535a6d9 999 int ret;
78f0bacd
DG
1000
1001 ret = disable_ust_channel(app, ua_sess, ua_chan);
1002 if (ret < 0) {
1003 goto error;
1004 }
1005
8535a6d9
DG
1006 ua_chan->enabled = 0;
1007
78f0bacd
DG
1008error:
1009 return ret;
1010}
1011
1012/*
1013 * Lookup ust app channel for session and enable it on the tracer side.
1014 */
1015static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1016 struct ltt_ust_channel *uchan, struct ust_app *app)
1017{
1018 int ret = 0;
bec39940
DG
1019 struct lttng_ht_iter iter;
1020 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1021 struct ust_app_channel *ua_chan;
1022
bec39940
DG
1023 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1024 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
78f0bacd 1025 if (ua_chan_node == NULL) {
a991f516
MD
1026 DBG2("Unable to find channel %s in ust session id %u",
1027 uchan->name, ua_sess->id);
78f0bacd
DG
1028 goto error;
1029 }
1030
1031 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1032
1033 ret = enable_ust_channel(app, ua_sess, ua_chan);
1034 if (ret < 0) {
1035 goto error;
1036 }
1037
1038error:
1039 return ret;
1040}
1041
284d8f55 1042/*
5b4a0ec0 1043 * Create UST app channel and create it on the tracer.
284d8f55 1044 */
5b4a0ec0
DG
1045static struct ust_app_channel *create_ust_app_channel(
1046 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1047 struct ust_app *app)
1048{
1049 int ret = 0;
bec39940
DG
1050 struct lttng_ht_iter iter;
1051 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
1052 struct ust_app_channel *ua_chan;
1053
1054 /* Lookup channel in the ust app session */
bec39940
DG
1055 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1056 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5b4a0ec0 1057 if (ua_chan_node == NULL) {
a991f516
MD
1058 DBG2("Unable to find channel %s in ust session id %u",
1059 uchan->name, ua_sess->id);
5b4a0ec0
DG
1060 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1061 if (ua_chan == NULL) {
1062 goto error;
1063 }
1064 shadow_copy_channel(ua_chan, uchan);
1065
bec39940 1066 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
5b4a0ec0
DG
1067 } else {
1068 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1069 }
1070
1071 ret = create_ust_channel(app, ua_sess, ua_chan);
1072 if (ret < 0) {
1073 goto error;
1074 }
1075
1076 return ua_chan;
1077
1078error:
1079 return NULL;
1080}
1081
1082/*
1083 * Create UST app event and create it on the tracer side.
1084 */
edb67388
DG
1085static
1086int create_ust_app_event(struct ust_app_session *ua_sess,
1087 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1088 struct ust_app *app)
284d8f55 1089{
edb67388 1090 int ret = 0;
bec39940
DG
1091 struct lttng_ht_iter iter;
1092 struct lttng_ht_node_str *ua_event_node;
5b4a0ec0 1093 struct ust_app_event *ua_event;
284d8f55 1094
5b4a0ec0 1095 /* Get event node */
bec39940
DG
1096 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
1097 ua_event_node = lttng_ht_iter_get_node_str(&iter);
edb67388
DG
1098 if (ua_event_node != NULL) {
1099 ERR("UST app event %s already exist. Stopping creation.",
1100 uevent->attr.name);
1101 goto end;
1102 }
5b4a0ec0 1103
edb67388
DG
1104 /* Does not exist so create one */
1105 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1106 if (ua_event == NULL) {
1107 /* Only malloc can failed so something is really wrong */
1108 ret = -ENOMEM;
1109 goto error;
5b4a0ec0 1110 }
edb67388 1111 shadow_copy_event(ua_event, uevent);
5b4a0ec0 1112
edb67388 1113 /* Create it on the tracer side */
5b4a0ec0 1114 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 1115 if (ret < 0) {
e5abf1bf 1116 rcu_read_lock();
edb67388 1117 delete_ust_app_event(app->key.sock, ua_event);
e5abf1bf 1118 rcu_read_unlock();
284d8f55
DG
1119 goto error;
1120 }
1121
8535a6d9
DG
1122 ua_event->enabled = 1;
1123
bec39940 1124 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
284d8f55 1125
7f79d3a1
DG
1126 DBG2("UST app create event %s for PID %d completed",
1127 ua_event->name, app->key.pid);
1128
edb67388 1129end:
5b4a0ec0 1130error:
edb67388 1131 return ret;
5b4a0ec0
DG
1132}
1133
1134/*
1135 * Create UST metadata and open it on the tracer side.
1136 */
1137static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1138 char *pathname, struct ust_app *app)
1139{
1140 int ret = 0;
1141
1142 if (ua_sess->metadata == NULL) {
1143 /* Allocate UST metadata */
1144 ua_sess->metadata = trace_ust_create_metadata(pathname);
1145 if (ua_sess->metadata == NULL) {
1146 ERR("UST app session %d creating metadata failed",
1147 ua_sess->handle);
1148 goto error;
1149 }
1150
1151 ret = open_ust_metadata(app, ua_sess);
1152 if (ret < 0) {
1153 goto error;
1154 }
1155
1156 DBG2("UST metadata opened for app pid %d", app->key.pid);
1157 }
1158
1159 /* Open UST metadata stream */
1160 if (ua_sess->metadata->stream_obj == NULL) {
1161 ret = create_ust_stream(app, ua_sess);
1162 if (ret < 0) {
1163 goto error;
1164 }
1165
67f747d8
MD
1166 ret = mkdir_run_as(ua_sess->path, S_IRWXU | S_IRWXG,
1167 ua_sess->uid, ua_sess->gid);
5b4a0ec0
DG
1168 if (ret < 0) {
1169 PERROR("mkdir UST metadata");
1170 goto error;
1171 }
1172
477d7741
MD
1173 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1174 "%s/metadata", ua_sess->path);
5b4a0ec0
DG
1175 if (ret < 0) {
1176 PERROR("asprintf UST create stream");
1177 goto error;
1178 }
1179
1180 DBG2("UST metadata stream object created for app pid %d",
1181 app->key.pid);
1182 } else {
1183 ERR("Attempting to create stream without metadata opened");
1184 goto error;
1185 }
1186
1187 return 0;
1188
1189error:
1190 return -1;
1191}
1192
1193/*
1194 * Return pointer to traceable apps list.
1195 */
bec39940 1196struct lttng_ht *ust_app_get_ht(void)
5b4a0ec0
DG
1197{
1198 return ust_app_ht;
1199}
1200
1201/*
1202 * Return ust app pointer or NULL if not found.
1203 */
1204struct ust_app *ust_app_find_by_pid(pid_t pid)
1205{
bec39940
DG
1206 struct lttng_ht_node_ulong *node;
1207 struct lttng_ht_iter iter;
5b4a0ec0
DG
1208
1209 rcu_read_lock();
bec39940
DG
1210 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1211 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
1212 if (node == NULL) {
1213 DBG2("UST app no found with pid %d", pid);
1214 goto error;
1215 }
1216 rcu_read_unlock();
1217
1218 DBG2("Found UST app by pid %d", pid);
1219
1220 return caa_container_of(node, struct ust_app, node);
1221
1222error:
1223 rcu_read_unlock();
1224 return NULL;
1225}
1226
1227/*
1228 * Using pid and uid (of the app), allocate a new ust_app struct and
1229 * add it to the global traceable app list.
1230 *
0df502fd
MD
1231 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1232 * bitness is not supported.
5b4a0ec0
DG
1233 */
1234int ust_app_register(struct ust_register_msg *msg, int sock)
1235{
1236 struct ust_app *lta;
1237
7753dea8
MD
1238 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1239 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
f943b0fb 1240 ERR("Registration failed: application \"%s\" (pid: %d) has "
7753dea8
MD
1241 "%d-bit long, but no consumerd for this long size is available.\n",
1242 msg->name, msg->pid, msg->bits_per_long);
88ff5b7f 1243 close(sock);
0df502fd
MD
1244 return -EINVAL;
1245 }
5b4a0ec0
DG
1246 lta = zmalloc(sizeof(struct ust_app));
1247 if (lta == NULL) {
1248 PERROR("malloc");
1249 return -ENOMEM;
1250 }
1251
1252 lta->ppid = msg->ppid;
1253 lta->uid = msg->uid;
1254 lta->gid = msg->gid;
7753dea8 1255 lta->bits_per_long = msg->bits_per_long;
5b4a0ec0
DG
1256 lta->v_major = msg->major;
1257 lta->v_minor = msg->minor;
1258 strncpy(lta->name, msg->name, sizeof(lta->name));
1259 lta->name[16] = '\0';
bec39940 1260 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
5b4a0ec0
DG
1261
1262 /* Set key map */
1263 lta->key.pid = msg->pid;
bec39940 1264 lttng_ht_node_init_ulong(&lta->node, (unsigned long)lta->key.pid);
5b4a0ec0 1265 lta->key.sock = sock;
bec39940 1266 lttng_ht_node_init_ulong(&lta->key.node, (unsigned long)lta->key.sock);
5b4a0ec0
DG
1267
1268 rcu_read_lock();
bec39940
DG
1269 lttng_ht_add_unique_ulong(ust_app_sock_key_map, &lta->key.node);
1270 lttng_ht_add_unique_ulong(ust_app_ht, &lta->node);
5b4a0ec0
DG
1271 rcu_read_unlock();
1272
1273 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1274 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
1275 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
1276
1277 return 0;
1278}
1279
1280/*
1281 * Unregister app by removing it from the global traceable app list and freeing
1282 * the data struct.
1283 *
1284 * The socket is already closed at this point so no close to sock.
1285 */
1286void ust_app_unregister(int sock)
1287{
1288 struct ust_app *lta;
bec39940
DG
1289 struct lttng_ht_node_ulong *node;
1290 struct lttng_ht_iter iter;
525b0740 1291 int ret;
5b4a0ec0
DG
1292
1293 rcu_read_lock();
1294 lta = find_app_by_sock(sock);
1295 if (lta == NULL) {
1296 ERR("Unregister app sock %d not found!", sock);
1297 goto error;
1298 }
1299
1300 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
1301
886459c6
MD
1302 /* Remove application from socket hash table */
1303 lttng_ht_lookup(ust_app_sock_key_map, (void *)((unsigned long) sock), &iter);
1304 ret = lttng_ht_del(ust_app_sock_key_map, &iter);
1305 assert(!ret);
1306
5b4a0ec0 1307 /* Get the node reference for a call_rcu */
bec39940
DG
1308 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) lta->key.pid), &iter);
1309 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
1310 if (node == NULL) {
1311 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
1312 goto error;
1313 }
284d8f55 1314
886459c6 1315 /* Remove application from PID hash table */
bec39940 1316 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 1317 assert(!ret);
5b4a0ec0 1318 call_rcu(&node->head, delete_ust_app_rcu);
284d8f55 1319error:
5b4a0ec0
DG
1320 rcu_read_unlock();
1321 return;
284d8f55
DG
1322}
1323
1324/*
5b4a0ec0 1325 * Return traceable_app_count
284d8f55 1326 */
5b4a0ec0 1327unsigned long ust_app_list_count(void)
284d8f55 1328{
5b4a0ec0 1329 unsigned long count;
284d8f55 1330
5b4a0ec0 1331 rcu_read_lock();
bec39940 1332 count = lttng_ht_get_count(ust_app_ht);
5b4a0ec0 1333 rcu_read_unlock();
284d8f55 1334
5b4a0ec0 1335 return count;
284d8f55
DG
1336}
1337
5b4a0ec0
DG
1338/*
1339 * Fill events array with all events name of all registered apps.
1340 */
1341int ust_app_list_events(struct lttng_event **events)
421cb601 1342{
5b4a0ec0
DG
1343 int ret, handle;
1344 size_t nbmem, count = 0;
bec39940 1345 struct lttng_ht_iter iter;
5b4a0ec0
DG
1346 struct ust_app *app;
1347 struct lttng_event *tmp;
421cb601 1348
5b4a0ec0
DG
1349 nbmem = UST_APP_EVENT_LIST_SIZE;
1350 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1351 if (tmp == NULL) {
1352 PERROR("zmalloc ust app events");
1353 ret = -ENOMEM;
421cb601
DG
1354 goto error;
1355 }
1356
5b4a0ec0 1357 rcu_read_lock();
421cb601 1358
bec39940 1359 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
90eaa0d2 1360 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 1361
5b4a0ec0
DG
1362 handle = ustctl_tracepoint_list(app->key.sock);
1363 if (handle < 0) {
1364 ERR("UST app list events getting handle failed for app pid %d",
1365 app->key.pid);
1366 continue;
1367 }
421cb601 1368
5b4a0ec0 1369 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
90eaa0d2 1370 &uiter)) != -ENOENT) {
815564d8
MD
1371 if (count >= nbmem) {
1372 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1373 2 * nbmem);
1374 nbmem *= 2;
2f221590 1375 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
5b4a0ec0
DG
1376 if (tmp == NULL) {
1377 PERROR("realloc ust app events");
1378 ret = -ENOMEM;
1379 goto rcu_error;
1380 }
1381 }
90eaa0d2
DG
1382 memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1383 memcpy(tmp[count].loglevel, uiter.loglevel, LTTNG_UST_SYM_NAME_LEN);
1384 tmp[count].loglevel_value = uiter.loglevel_value;
5b4a0ec0
DG
1385 tmp[count].type = LTTNG_UST_TRACEPOINT;
1386 tmp[count].pid = app->key.pid;
1387 tmp[count].enabled = -1;
1388 count++;
421cb601 1389 }
421cb601
DG
1390 }
1391
5b4a0ec0
DG
1392 ret = count;
1393 *events = tmp;
421cb601 1394
5b4a0ec0 1395 DBG2("UST app list events done (%zu events)", count);
421cb601 1396
5b4a0ec0
DG
1397rcu_error:
1398 rcu_read_unlock();
421cb601 1399error:
5b4a0ec0 1400 return ret;
421cb601
DG
1401}
1402
5b4a0ec0
DG
1403/*
1404 * Free and clean all traceable apps of the global list.
1405 */
1406void ust_app_clean_list(void)
421cb601 1407{
5b4a0ec0 1408 int ret;
bec39940
DG
1409 struct lttng_ht_iter iter;
1410 struct lttng_ht_node_ulong *node;
421cb601 1411
5b4a0ec0 1412 DBG2("UST app cleaning registered apps hash table");
421cb601 1413
5b4a0ec0 1414 rcu_read_lock();
421cb601 1415
bec39940
DG
1416 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, node, node) {
1417 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 1418 assert(!ret);
bec39940 1419 call_rcu(&node->head, delete_ust_app_rcu);
421cb601 1420 }
bec39940
DG
1421 /* Destroy is done only when the ht is empty */
1422 lttng_ht_destroy(ust_app_ht);
421cb601 1423
bec39940
DG
1424 cds_lfht_for_each_entry(ust_app_sock_key_map->ht, &iter.iter, node, node) {
1425 ret = lttng_ht_del(ust_app_sock_key_map, &iter);
1426 assert(!ret);
1427 }
1428 /* Destroy is done only when the ht is empty */
1429 lttng_ht_destroy(ust_app_sock_key_map);
421cb601 1430
5b4a0ec0
DG
1431 rcu_read_unlock();
1432}
1433
1434/*
1435 * Init UST app hash table.
1436 */
1437void ust_app_ht_alloc(void)
1438{
bec39940
DG
1439 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1440 ust_app_sock_key_map = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
421cb601
DG
1441}
1442
78f0bacd
DG
1443/*
1444 * For a specific UST session, disable the channel for all registered apps.
1445 */
35a9059d 1446int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1447 struct ltt_ust_channel *uchan)
1448{
1449 int ret = 0;
bec39940
DG
1450 struct lttng_ht_iter iter;
1451 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1452 struct ust_app *app;
1453 struct ust_app_session *ua_sess;
8535a6d9 1454 struct ust_app_channel *ua_chan;
78f0bacd
DG
1455
1456 if (usess == NULL || uchan == NULL) {
1457 ERR("Disabling UST global channel with NULL values");
1458 ret = -1;
1459 goto error;
1460 }
1461
a991f516
MD
1462 DBG2("UST app disabling channel %s from global domain for session id %d",
1463 uchan->name, usess->id);
78f0bacd
DG
1464
1465 rcu_read_lock();
1466
1467 /* For every registered applications */
bec39940
DG
1468 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
1469 struct lttng_ht_iter uiter;
78f0bacd
DG
1470 ua_sess = lookup_session_by_app(usess, app);
1471 if (ua_sess == NULL) {
1472 continue;
1473 }
1474
8535a6d9 1475 /* Get channel */
bec39940
DG
1476 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1477 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
1478 /* If the session if found for the app, the channel must be there */
1479 assert(ua_chan_node);
1480
1481 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1482 /* The channel must not be already disabled */
1483 assert(ua_chan->enabled == 1);
1484
1485 /* Disable channel onto application */
1486 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
1487 if (ret < 0) {
1488 /* XXX: We might want to report this error at some point... */
1489 continue;
1490 }
1491 }
1492
1493 rcu_read_unlock();
1494
1495error:
1496 return ret;
1497}
1498
1499/*
1500 * For a specific UST session, enable the channel for all registered apps.
1501 */
35a9059d 1502int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1503 struct ltt_ust_channel *uchan)
1504{
1505 int ret = 0;
bec39940 1506 struct lttng_ht_iter iter;
78f0bacd
DG
1507 struct ust_app *app;
1508 struct ust_app_session *ua_sess;
1509
1510 if (usess == NULL || uchan == NULL) {
1511 ERR("Adding UST global channel to NULL values");
1512 ret = -1;
1513 goto error;
1514 }
1515
a991f516
MD
1516 DBG2("UST app enabling channel %s to global domain for session id %d",
1517 uchan->name, usess->id);
78f0bacd
DG
1518
1519 rcu_read_lock();
1520
1521 /* For every registered applications */
bec39940 1522 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
78f0bacd
DG
1523 ua_sess = lookup_session_by_app(usess, app);
1524 if (ua_sess == NULL) {
1525 continue;
1526 }
1527
1528 /* Enable channel onto application */
1529 ret = enable_ust_app_channel(ua_sess, uchan, app);
1530 if (ret < 0) {
1531 /* XXX: We might want to report this error at some point... */
1532 continue;
1533 }
1534 }
1535
1536 rcu_read_unlock();
1537
1538error:
1539 return ret;
1540}
1541
b0a40d28
DG
1542/*
1543 * Disable an event in a channel and for a specific session.
1544 */
35a9059d
DG
1545int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1546 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
1547{
1548 int ret = 0;
bec39940
DG
1549 struct lttng_ht_iter iter, uiter;
1550 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
b0a40d28
DG
1551 struct ust_app *app;
1552 struct ust_app_session *ua_sess;
1553 struct ust_app_channel *ua_chan;
1554 struct ust_app_event *ua_event;
1555
1556 DBG("UST app disabling event %s for all apps in channel "
a991f516 1557 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
1558
1559 rcu_read_lock();
1560
1561 /* For all registered applications */
bec39940 1562 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
b0a40d28
DG
1563 ua_sess = lookup_session_by_app(usess, app);
1564 if (ua_sess == NULL) {
1565 /* Next app */
1566 continue;
1567 }
1568
1569 /* Lookup channel in the ust app session */
bec39940
DG
1570 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1571 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 1572 if (ua_chan_node == NULL) {
a991f516
MD
1573 DBG2("Channel %s not found in session id %d for app pid %d."
1574 "Skipping", uchan->name, usess->id, app->key.pid);
b0a40d28
DG
1575 continue;
1576 }
1577 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1578
bec39940
DG
1579 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1580 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28
DG
1581 if (ua_event_node == NULL) {
1582 DBG2("Event %s not found in channel %s for app pid %d."
35a9059d 1583 "Skipping", uevent->attr.name, uchan->name, app->key.pid);
b0a40d28
DG
1584 continue;
1585 }
1586 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1587
7f79d3a1 1588 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
1589 if (ret < 0) {
1590 /* XXX: Report error someday... */
1591 continue;
1592 }
1593 }
1594
1595 rcu_read_unlock();
1596
1597 return ret;
1598}
1599
9730260e 1600/*
edb67388 1601 * For a specific UST session and UST channel, the event for all
9730260e
DG
1602 * registered apps.
1603 */
35a9059d 1604int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
9730260e
DG
1605 struct ltt_ust_channel *uchan)
1606{
1607 int ret = 0;
bec39940
DG
1608 struct lttng_ht_iter iter, uiter;
1609 struct lttng_ht_node_str *ua_chan_node;
9730260e
DG
1610 struct ust_app *app;
1611 struct ust_app_session *ua_sess;
1612 struct ust_app_channel *ua_chan;
1613 struct ust_app_event *ua_event;
1614
1615 DBG("UST app disabling all event for all apps in channel "
a991f516 1616 "%s for session id %d", uchan->name, usess->id);
9730260e
DG
1617
1618 rcu_read_lock();
1619
1620 /* For all registered applications */
bec39940 1621 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
9730260e 1622 ua_sess = lookup_session_by_app(usess, app);
edb67388
DG
1623 /* If ua_sess is NULL, there is a code flow error */
1624 assert(ua_sess);
9730260e
DG
1625
1626 /* Lookup channel in the ust app session */
bec39940
DG
1627 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1628 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1629 /* If the channel is not found, there is a code flow error */
1630 assert(ua_chan_node);
1631
9730260e
DG
1632 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1633
1634 /* Disable each events of channel */
bec39940
DG
1635 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
1636 node.node) {
7f79d3a1 1637 ret = disable_ust_app_event(ua_sess, ua_event, app);
9730260e
DG
1638 if (ret < 0) {
1639 /* XXX: Report error someday... */
1640 continue;
1641 }
1642 }
1643 }
1644
1645 rcu_read_unlock();
1646
1647 return ret;
1648}
1649
421cb601 1650/*
5b4a0ec0 1651 * For a specific UST session, create the channel for all registered apps.
421cb601 1652 */
35a9059d 1653int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
1654 struct ltt_ust_channel *uchan)
1655{
1656 int ret = 0;
bec39940 1657 struct lttng_ht_iter iter;
48842b30
DG
1658 struct ust_app *app;
1659 struct ust_app_session *ua_sess;
1660 struct ust_app_channel *ua_chan;
1661
421cb601
DG
1662 if (usess == NULL || uchan == NULL) {
1663 ERR("Adding UST global channel to NULL values");
1664 ret = -1;
1665 goto error;
1666 }
1667
a991f516
MD
1668 DBG2("UST app adding channel %s to global domain for session id %d",
1669 uchan->name, usess->id);
48842b30
DG
1670
1671 rcu_read_lock();
421cb601 1672
5b4a0ec0 1673 /* For every registered applications */
bec39940 1674 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
edb67388
DG
1675 /*
1676 * Create session on the tracer side and add it to app session HT. Note
1677 * that if session exist, it will simply return a pointer to the ust
1678 * app session.
1679 */
421cb601 1680 ua_sess = create_ust_app_session(usess, app);
509cbaf8 1681 if (ua_sess == NULL) {
5b4a0ec0 1682 continue;
48842b30
DG
1683 }
1684
421cb601
DG
1685 /* Create channel onto application */
1686 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1687 if (ua_chan == NULL) {
5b4a0ec0 1688 continue;
48842b30 1689 }
48842b30 1690 }
5b4a0ec0 1691
48842b30
DG
1692 rcu_read_unlock();
1693
421cb601 1694error:
48842b30
DG
1695 return ret;
1696}
1697
5b4a0ec0 1698/*
edb67388 1699 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 1700 */
35a9059d 1701int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
1702 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1703{
1704 int ret = 0;
bec39940
DG
1705 struct lttng_ht_iter iter, uiter;
1706 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
48842b30
DG
1707 struct ust_app *app;
1708 struct ust_app_session *ua_sess;
1709 struct ust_app_channel *ua_chan;
1710 struct ust_app_event *ua_event;
48842b30 1711
a991f516
MD
1712 DBG("UST app enabling event %s for all apps for session id %d",
1713 uevent->attr.name, usess->id);
48842b30 1714
edb67388
DG
1715 /*
1716 * NOTE: At this point, this function is called only if the session and
1717 * channel passed are already created for all apps. and enabled on the
1718 * tracer also.
1719 */
1720
48842b30 1721 rcu_read_lock();
421cb601
DG
1722
1723 /* For all registered applications */
bec39940 1724 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
edb67388
DG
1725 ua_sess = lookup_session_by_app(usess, app);
1726 /* If ua_sess is NULL, there is a code flow error */
1727 assert(ua_sess);
ba767faf 1728
edb67388 1729 /* Lookup channel in the ust app session */
bec39940
DG
1730 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1731 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1732 /* If the channel is not found, there is a code flow error */
1733 assert(ua_chan_node);
1734
1735 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1736
bec39940
DG
1737 lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter);
1738 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
35a9059d 1739 if (ua_event_node == NULL) {
7f79d3a1
DG
1740 DBG3("UST app enable event %s not found for app PID %d."
1741 "Skipping app", uevent->attr.name, app->key.pid);
35a9059d
DG
1742 continue;
1743 }
1744 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1745
1746 ret = enable_ust_app_event(ua_sess, ua_event, app);
1747 if (ret < 0) {
7f79d3a1 1748 goto error;
48842b30 1749 }
edb67388
DG
1750 }
1751
7f79d3a1 1752error:
edb67388 1753 rcu_read_unlock();
edb67388
DG
1754 return ret;
1755}
1756
1757/*
1758 * For a specific existing UST session and UST channel, creates the event for
1759 * all registered apps.
1760 */
35a9059d 1761int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
1762 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1763{
1764 int ret = 0;
bec39940
DG
1765 struct lttng_ht_iter iter, uiter;
1766 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
1767 struct ust_app *app;
1768 struct ust_app_session *ua_sess;
1769 struct ust_app_channel *ua_chan;
1770
a991f516
MD
1771 DBG("UST app creating event %s for all apps for session id %d",
1772 uevent->attr.name, usess->id);
edb67388
DG
1773
1774 /*
1775 * NOTE: At this point, this function is called only if the session and
1776 * channel passed are already created for all apps. and enabled on the
1777 * tracer also.
1778 */
1779
1780 rcu_read_lock();
1781
1782 /* For all registered applications */
bec39940 1783 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
edb67388
DG
1784 ua_sess = lookup_session_by_app(usess, app);
1785 /* If ua_sess is NULL, there is a code flow error */
1786 assert(ua_sess);
48842b30
DG
1787
1788 /* Lookup channel in the ust app session */
bec39940
DG
1789 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1790 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
1791 /* If the channel is not found, there is a code flow error */
1792 assert(ua_chan_node);
1793
48842b30
DG
1794 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1795
edb67388
DG
1796 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1797 if (ret < 0) {
5b4a0ec0 1798 continue;
48842b30 1799 }
48842b30 1800 }
5b4a0ec0 1801
48842b30
DG
1802 rcu_read_unlock();
1803
1804 return ret;
1805}
1806
5b4a0ec0
DG
1807/*
1808 * Start tracing for a specific UST session and app.
1809 */
421cb601 1810int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
1811{
1812 int ret = 0;
bec39940 1813 struct lttng_ht_iter iter;
48842b30
DG
1814 struct ust_app_session *ua_sess;
1815 struct ust_app_channel *ua_chan;
5b4a0ec0 1816 struct ltt_ust_stream *ustream;
7753dea8 1817 int consumerd_fd;
48842b30 1818
421cb601 1819 DBG("Starting tracing for ust app pid %d", app->key.pid);
5cf5d0e7 1820
509cbaf8
MD
1821 rcu_read_lock();
1822
421cb601
DG
1823 ua_sess = lookup_session_by_app(usess, app);
1824 if (ua_sess == NULL) {
509cbaf8 1825 goto error_rcu_unlock;
421cb601 1826 }
48842b30 1827
aea829b3
DG
1828 /* Upon restart, we skip the setup, already done */
1829 if (ua_sess->started) {
8be98f9a 1830 goto skip_setup;
aea829b3 1831 }
8be98f9a 1832
421cb601
DG
1833 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1834 if (ret < 0) {
509cbaf8 1835 goto error_rcu_unlock;
421cb601 1836 }
48842b30 1837
421cb601 1838 /* For each channel */
bec39940
DG
1839 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
1840 node.node) {
421cb601
DG
1841 /* Create all streams */
1842 while (1) {
5b4a0ec0 1843 /* Create UST stream */
421cb601
DG
1844 ustream = zmalloc(sizeof(*ustream));
1845 if (ustream == NULL) {
1846 PERROR("zmalloc ust stream");
509cbaf8 1847 goto error_rcu_unlock;
421cb601 1848 }
48842b30 1849
421cb601
DG
1850 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1851 &ustream->obj);
48842b30 1852 if (ret < 0) {
421cb601
DG
1853 /* Got all streams */
1854 break;
48842b30 1855 }
421cb601 1856 ustream->handle = ustream->obj->handle;
48842b30 1857
421cb601
DG
1858 /* Order is important */
1859 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
477d7741
MD
1860 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1861 ua_sess->path, ua_chan->name,
1862 ua_chan->streams.count++);
aba8e916
DG
1863 if (ret < 0) {
1864 PERROR("asprintf UST create stream");
421cb601 1865 continue;
aba8e916 1866 }
421cb601
DG
1867 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1868 ustream->pathname);
1869 }
421cb601 1870 }
aba8e916 1871
7753dea8
MD
1872 switch (app->bits_per_long) {
1873 case 64:
1874 consumerd_fd = ust_consumerd64_fd;
1875 break;
1876 case 32:
1877 consumerd_fd = ust_consumerd32_fd;
1878 break;
1879 default:
1880 ret = -EINVAL;
1881 goto error_rcu_unlock;
1882 }
aea829b3 1883
421cb601 1884 /* Setup UST consumer socket and send fds to it */
7753dea8 1885 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
421cb601 1886 if (ret < 0) {
509cbaf8 1887 goto error_rcu_unlock;
421cb601 1888 }
8be98f9a 1889 ua_sess->started = 1;
48842b30 1890
8be98f9a 1891skip_setup:
421cb601
DG
1892 /* This start the UST tracing */
1893 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1894 if (ret < 0) {
1895 ERR("Error starting tracing for app pid: %d", app->key.pid);
509cbaf8 1896 goto error_rcu_unlock;
421cb601 1897 }
5b4a0ec0 1898
509cbaf8 1899 rcu_read_unlock();
48842b30 1900
421cb601
DG
1901 /* Quiescent wait after starting trace */
1902 ustctl_wait_quiescent(app->key.sock);
48842b30 1903
421cb601 1904 return 0;
48842b30 1905
509cbaf8
MD
1906error_rcu_unlock:
1907 rcu_read_unlock();
421cb601
DG
1908 return -1;
1909}
48842b30 1910
8be98f9a
MD
1911/*
1912 * Stop tracing for a specific UST session and app.
1913 */
1914int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1915{
1916 int ret = 0;
bec39940 1917 struct lttng_ht_iter iter;
8be98f9a 1918 struct ust_app_session *ua_sess;
6d3686da 1919 struct ust_app_channel *ua_chan;
8be98f9a
MD
1920
1921 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1922
1923 rcu_read_lock();
1924
1925 ua_sess = lookup_session_by_app(usess, app);
1926 if (ua_sess == NULL) {
1927 /* Only malloc can failed so something is really wrong */
1928 goto error_rcu_unlock;
1929 }
1930
9d6c7d3f
DG
1931 /* This inhibits UST tracing */
1932 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1933 if (ret < 0) {
1934 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1935 goto error_rcu_unlock;
1936 }
1937
1938 /* Quiescent wait after stopping trace */
1939 ustctl_wait_quiescent(app->key.sock);
1940
1941 /* Flushing buffers */
bec39940
DG
1942 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
1943 node.node) {
6d3686da 1944 ret = ustctl_sock_flush_buffer(app->key.sock, ua_chan->obj);
8be98f9a 1945 if (ret < 0) {
6d3686da
DG
1946 ERR("UST app PID %d channel %s flush failed",
1947 app->key.pid, ua_chan->name);
1948 ERR("Ended with ret %d", ret);
1949 /* Continuing flushing all buffers */
1950 continue;
8be98f9a
MD
1951 }
1952 }
8be98f9a 1953
90d97d10
DG
1954 /* Flush all buffers before stopping */
1955 ret = ustctl_sock_flush_buffer(app->key.sock, ua_sess->metadata->obj);
1956 if (ret < 0) {
1957 ERR("UST app PID %d metadata flush failed", app->key.pid);
1958 ERR("Ended with ret %d", ret);
1959 }
1960
9d6c7d3f
DG
1961 rcu_read_unlock();
1962
8be98f9a
MD
1963 return 0;
1964
1965error_rcu_unlock:
1966 rcu_read_unlock();
1967 return -1;
1968}
1969
84cd17c6
MD
1970/*
1971 * Destroy a specific UST session in apps.
1972 */
1973int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1974{
1975 struct ust_app_session *ua_sess;
1976 struct lttng_ust_object_data obj;
bec39940
DG
1977 struct lttng_ht_iter iter;
1978 struct lttng_ht_node_ulong *node;
525b0740 1979 int ret;
84cd17c6
MD
1980
1981 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1982
1983 rcu_read_lock();
1984
1985 __lookup_session_by_app(usess, app, &iter);
bec39940 1986 node = lttng_ht_iter_get_node_ulong(&iter);
84cd17c6
MD
1987 if (node == NULL) {
1988 /* Only malloc can failed so something is really wrong */
1989 goto error_rcu_unlock;
1990 }
1991 ua_sess = caa_container_of(node, struct ust_app_session, node);
bec39940 1992 ret = lttng_ht_del(app->sessions, &iter);
525b0740 1993 assert(!ret);
84cd17c6
MD
1994 delete_ust_app_session(app->key.sock, ua_sess);
1995 obj.handle = ua_sess->handle;
1996 obj.shm_fd = -1;
1997 obj.wait_fd = -1;
1998 obj.memory_map_size = 0;
1999 ustctl_release_object(app->key.sock, &obj);
2000
2001 rcu_read_unlock();
2002
2003 /* Quiescent wait after stopping trace */
2004 ustctl_wait_quiescent(app->key.sock);
2005
2006 return 0;
2007
2008error_rcu_unlock:
2009 rcu_read_unlock();
2010 return -1;
2011}
2012
5b4a0ec0
DG
2013/*
2014 * Start tracing for the UST session.
2015 */
421cb601
DG
2016int ust_app_start_trace_all(struct ltt_ust_session *usess)
2017{
2018 int ret = 0;
bec39940 2019 struct lttng_ht_iter iter;
421cb601 2020 struct ust_app *app;
48842b30 2021
421cb601
DG
2022 DBG("Starting all UST traces");
2023
2024 rcu_read_lock();
421cb601 2025
bec39940 2026 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
421cb601 2027 ret = ust_app_start_trace(usess, app);
48842b30 2028 if (ret < 0) {
5b4a0ec0
DG
2029 /* Continue to next apps even on error */
2030 continue;
48842b30 2031 }
48842b30 2032 }
5b4a0ec0 2033
48842b30
DG
2034 rcu_read_unlock();
2035
2036 return 0;
2037}
487cf67c 2038
8be98f9a
MD
2039/*
2040 * Start tracing for the UST session.
2041 */
2042int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2043{
2044 int ret = 0;
bec39940 2045 struct lttng_ht_iter iter;
8be98f9a
MD
2046 struct ust_app *app;
2047
2048 DBG("Stopping all UST traces");
2049
2050 rcu_read_lock();
2051
bec39940 2052 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
8be98f9a
MD
2053 ret = ust_app_stop_trace(usess, app);
2054 if (ret < 0) {
2055 /* Continue to next apps even on error */
2056 continue;
2057 }
2058 }
2059
2060 rcu_read_unlock();
2061
2062 return 0;
2063}
2064
84cd17c6
MD
2065/*
2066 * Destroy app UST session.
2067 */
2068int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2069{
2070 int ret = 0;
bec39940 2071 struct lttng_ht_iter iter;
84cd17c6
MD
2072 struct ust_app *app;
2073
2074 DBG("Destroy all UST traces");
2075
2076 rcu_read_lock();
2077
bec39940 2078 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
84cd17c6
MD
2079 ret = ust_app_destroy_trace(usess, app);
2080 if (ret < 0) {
2081 /* Continue to next apps even on error */
2082 continue;
2083 }
2084 }
2085
2086 rcu_read_unlock();
2087
2088 return 0;
2089}
2090
5b4a0ec0
DG
2091/*
2092 * Add channels/events from UST global domain to registered apps at sock.
2093 */
487cf67c
DG
2094void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2095{
2096 int ret = 0;
bec39940 2097 struct lttng_ht_iter iter, uiter;
487cf67c
DG
2098 struct ust_app *app;
2099 struct ust_app_session *ua_sess;
2100 struct ust_app_channel *ua_chan;
2101 struct ust_app_event *ua_event;
1f3580c7
DG
2102
2103 if (usess == NULL) {
5b4a0ec0 2104 ERR("No UST session on global update. Returning");
1f3580c7
DG
2105 goto error;
2106 }
2107
a991f516
MD
2108 DBG2("UST app global update for app sock %d for session id %d", sock,
2109 usess->id);
487cf67c 2110
284d8f55
DG
2111 rcu_read_lock();
2112
487cf67c
DG
2113 app = find_app_by_sock(sock);
2114 if (app == NULL) {
2115 ERR("Failed to update app sock %d", sock);
2116 goto error;
2117 }
2118
421cb601 2119 ua_sess = create_ust_app_session(usess, app);
487cf67c 2120 if (ua_sess == NULL) {
487cf67c
DG
2121 goto error;
2122 }
2123
284d8f55
DG
2124 /*
2125 * We can iterate safely here over all UST app session sicne the create ust
2126 * app session above made a shadow copy of the UST global domain from the
2127 * ltt ust session.
2128 */
bec39940
DG
2129 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2130 node.node) {
284d8f55
DG
2131 ret = create_ust_channel(app, ua_sess, ua_chan);
2132 if (ret < 0) {
2133 /* FIXME: Should we quit here or continue... */
2134 continue;
487cf67c
DG
2135 }
2136
284d8f55 2137 /* For each events */
bec39940
DG
2138 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2139 node.node) {
284d8f55
DG
2140 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2141 if (ret < 0) {
2142 /* FIXME: Should we quit here or continue... */
2143 continue;
487cf67c 2144 }
36dc12cc 2145 }
487cf67c
DG
2146 }
2147
36dc12cc 2148 if (usess->start_trace) {
421cb601 2149 ret = ust_app_start_trace(usess, app);
36dc12cc 2150 if (ret < 0) {
36dc12cc
DG
2151 goto error;
2152 }
2153
36dc12cc
DG
2154 DBG2("UST trace started for app pid %d", app->key.pid);
2155 }
2156
487cf67c
DG
2157error:
2158 rcu_read_unlock();
2159 return;
2160}
55cc08a6
DG
2161
2162/*
2163 * Add context to a specific channel for global UST domain.
2164 */
2165int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2166 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2167{
2168 int ret = 0;
bec39940
DG
2169 struct lttng_ht_node_str *ua_chan_node;
2170 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
2171 struct ust_app_channel *ua_chan = NULL;
2172 struct ust_app_session *ua_sess;
2173 struct ust_app *app;
2174
2175 rcu_read_lock();
2176
bec39940 2177 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
55cc08a6
DG
2178 ua_sess = lookup_session_by_app(usess, app);
2179 if (ua_sess == NULL) {
2180 continue;
2181 }
2182
2183 /* Lookup channel in the ust app session */
bec39940
DG
2184 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2185 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2186 if (ua_chan_node == NULL) {
2187 continue;
2188 }
2189 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2190 node);
2191
2192 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2193 if (ret < 0) {
2194 continue;
2195 }
2196 }
2197
55cc08a6
DG
2198 rcu_read_unlock();
2199 return ret;
2200}
2201
2202/*
2203 * Add context to a specific event in a channel for global UST domain.
2204 */
2205int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2206 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2207 struct ltt_ust_context *uctx)
2208{
2209 int ret = 0;
bec39940
DG
2210 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2211 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
2212 struct ust_app_session *ua_sess;
2213 struct ust_app_event *ua_event;
2214 struct ust_app_channel *ua_chan = NULL;
2215 struct ust_app *app;
2216
2217 rcu_read_lock();
2218
bec39940 2219 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, node.node) {
55cc08a6
DG
2220 ua_sess = lookup_session_by_app(usess, app);
2221 if (ua_sess == NULL) {
2222 continue;
2223 }
2224
2225 /* Lookup channel in the ust app session */
bec39940
DG
2226 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2227 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2228 if (ua_chan_node == NULL) {
2229 continue;
2230 }
2231 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2232 node);
2233
bec39940
DG
2234 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2235 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2236 if (ua_event_node == NULL) {
2237 continue;
2238 }
2239 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2240 node);
2241
2242 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2243 if (ret < 0) {
2244 continue;
2245 }
2246 }
2247
55cc08a6
DG
2248 rcu_read_unlock();
2249 return ret;
2250}
76d45b40
DG
2251
2252/*
2253 * Enable event for a channel from a UST session for a specific PID.
2254 */
2255int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2256 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2257{
2258 int ret = 0;
bec39940
DG
2259 struct lttng_ht_iter iter;
2260 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
76d45b40
DG
2261 struct ust_app *app;
2262 struct ust_app_session *ua_sess;
2263 struct ust_app_channel *ua_chan;
2264 struct ust_app_event *ua_event;
2265
2266 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2267
2268 rcu_read_lock();
2269
2270 app = ust_app_find_by_pid(pid);
2271 if (app == NULL) {
2272 ERR("UST app enable event per PID %d not found", pid);
2273 ret = -1;
2274 goto error;
2275 }
2276
2277 ua_sess = lookup_session_by_app(usess, app);
2278 /* If ua_sess is NULL, there is a code flow error */
2279 assert(ua_sess);
2280
2281 /* Lookup channel in the ust app session */
bec39940
DG
2282 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2283 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
2284 /* If the channel is not found, there is a code flow error */
2285 assert(ua_chan_node);
2286
2287 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2288
bec39940
DG
2289 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2290 ua_event_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
2291 if (ua_event_node == NULL) {
2292 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2293 if (ret < 0) {
2294 goto error;
2295 }
2296 } else {
2297 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2298
2299 ret = enable_ust_app_event(ua_sess, ua_event, app);
2300 if (ret < 0) {
2301 goto error;
2302 }
2303 }
2304
2305error:
2306 rcu_read_unlock();
2307 return ret;
2308}
7f79d3a1
DG
2309
2310/*
2311 * Disable event for a channel from a UST session for a specific PID.
2312 */
2313int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2314 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2315{
2316 int ret = 0;
bec39940
DG
2317 struct lttng_ht_iter iter;
2318 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
7f79d3a1
DG
2319 struct ust_app *app;
2320 struct ust_app_session *ua_sess;
2321 struct ust_app_channel *ua_chan;
2322 struct ust_app_event *ua_event;
2323
2324 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2325
2326 rcu_read_lock();
2327
2328 app = ust_app_find_by_pid(pid);
2329 if (app == NULL) {
2330 ERR("UST app disable event per PID %d not found", pid);
2331 ret = -1;
2332 goto error;
2333 }
2334
2335 ua_sess = lookup_session_by_app(usess, app);
2336 /* If ua_sess is NULL, there is a code flow error */
2337 assert(ua_sess);
2338
2339 /* Lookup channel in the ust app session */
bec39940
DG
2340 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2341 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2342 if (ua_chan_node == NULL) {
2343 /* Channel does not exist, skip disabling */
2344 goto error;
2345 }
2346 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2347
bec39940
DG
2348 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2349 ua_event_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2350 if (ua_event_node == NULL) {
2351 /* Event does not exist, skip disabling */
2352 goto error;
2353 }
2354 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2355
2356 ret = disable_ust_app_event(ua_sess, ua_event, app);
2357 if (ret < 0) {
2358 goto error;
2359 }
2360
2361error:
2362 rcu_read_unlock();
2363 return ret;
2364}
This page took 0.245234 seconds and 4 git commands to generate.