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