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