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