Update following loglevel support UST API change
[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,
999 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event,
1000 struct ust_app *app)
1001{
1002 int ret;
1003
1004 ret = disable_ust_event(app, ua_sess, ua_event);
1005 if (ret < 0) {
1006 goto error;
1007 }
1008
1009 ua_event->enabled = 0;
1010
1011error:
1012 return ret;
1013}
1014
78f0bacd
DG
1015/*
1016 * Lookup ust app channel for session and disable it on the tracer side.
1017 */
8535a6d9
DG
1018static
1019int disable_ust_app_channel(struct ust_app_session *ua_sess,
1020 struct ust_app_channel *ua_chan, struct ust_app *app)
78f0bacd 1021{
8535a6d9 1022 int ret;
78f0bacd
DG
1023
1024 ret = disable_ust_channel(app, ua_sess, ua_chan);
1025 if (ret < 0) {
1026 goto error;
1027 }
1028
8535a6d9
DG
1029 ua_chan->enabled = 0;
1030
78f0bacd
DG
1031error:
1032 return ret;
1033}
1034
1035/*
1036 * Lookup ust app channel for session and enable it on the tracer side.
1037 */
1038static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1039 struct ltt_ust_channel *uchan, struct ust_app *app)
1040{
1041 int ret = 0;
1042 struct cds_lfht_iter iter;
1043 struct cds_lfht_node *ua_chan_node;
1044 struct ust_app_channel *ua_chan;
1045
1046 ua_chan_node = hashtable_lookup(ua_sess->channels,
1047 (void *)uchan->name, strlen(uchan->name), &iter);
1048 if (ua_chan_node == NULL) {
1049 DBG2("Unable to find channel %s in ust session uid %u",
1050 uchan->name, ua_sess->uid);
1051 goto error;
1052 }
1053
1054 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1055
1056 ret = enable_ust_channel(app, ua_sess, ua_chan);
1057 if (ret < 0) {
1058 goto error;
1059 }
1060
1061error:
1062 return ret;
1063}
1064
284d8f55 1065/*
5b4a0ec0 1066 * Create UST app channel and create it on the tracer.
284d8f55 1067 */
5b4a0ec0
DG
1068static struct ust_app_channel *create_ust_app_channel(
1069 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1070 struct ust_app *app)
1071{
1072 int ret = 0;
1073 struct cds_lfht_iter iter;
1074 struct cds_lfht_node *ua_chan_node;
1075 struct ust_app_channel *ua_chan;
1076
1077 /* Lookup channel in the ust app session */
1078 ua_chan_node = hashtable_lookup(ua_sess->channels,
1079 (void *)uchan->name, strlen(uchan->name), &iter);
1080 if (ua_chan_node == NULL) {
1081 DBG2("Unable to find channel %s in ust session uid %u",
1082 uchan->name, ua_sess->uid);
1083 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1084 if (ua_chan == NULL) {
1085 goto error;
1086 }
1087 shadow_copy_channel(ua_chan, uchan);
1088
1089 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
1090 } else {
1091 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1092 }
1093
1094 ret = create_ust_channel(app, ua_sess, ua_chan);
1095 if (ret < 0) {
1096 goto error;
1097 }
1098
1099 return ua_chan;
1100
1101error:
1102 return NULL;
1103}
1104
1105/*
1106 * Create UST app event and create it on the tracer side.
1107 */
edb67388
DG
1108static
1109int create_ust_app_event(struct ust_app_session *ua_sess,
1110 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1111 struct ust_app *app)
284d8f55 1112{
edb67388 1113 int ret = 0;
5b4a0ec0
DG
1114 struct cds_lfht_iter iter;
1115 struct cds_lfht_node *ua_event_node;
1116 struct ust_app_event *ua_event;
284d8f55 1117
5b4a0ec0
DG
1118 /* Get event node */
1119 ua_event_node = hashtable_lookup(ua_chan->events,
1120 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
edb67388
DG
1121 if (ua_event_node != NULL) {
1122 ERR("UST app event %s already exist. Stopping creation.",
1123 uevent->attr.name);
1124 goto end;
1125 }
5b4a0ec0 1126
edb67388
DG
1127 /* Does not exist so create one */
1128 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1129 if (ua_event == NULL) {
1130 /* Only malloc can failed so something is really wrong */
1131 ret = -ENOMEM;
1132 goto error;
5b4a0ec0 1133 }
edb67388 1134 shadow_copy_event(ua_event, uevent);
5b4a0ec0 1135
edb67388 1136 /* Create it on the tracer side */
5b4a0ec0 1137 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 1138 if (ret < 0) {
e5abf1bf 1139 rcu_read_lock();
edb67388 1140 delete_ust_app_event(app->key.sock, ua_event);
e5abf1bf 1141 rcu_read_unlock();
284d8f55
DG
1142 goto error;
1143 }
1144
8535a6d9
DG
1145 ua_event->enabled = 1;
1146
edb67388 1147 hashtable_add_unique(ua_chan->events, &ua_event->node);
284d8f55 1148
edb67388 1149end:
5b4a0ec0 1150error:
edb67388 1151 return ret;
5b4a0ec0
DG
1152}
1153
1154/*
1155 * Create UST metadata and open it on the tracer side.
1156 */
1157static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1158 char *pathname, struct ust_app *app)
1159{
1160 int ret = 0;
1161
1162 if (ua_sess->metadata == NULL) {
1163 /* Allocate UST metadata */
1164 ua_sess->metadata = trace_ust_create_metadata(pathname);
1165 if (ua_sess->metadata == NULL) {
1166 ERR("UST app session %d creating metadata failed",
1167 ua_sess->handle);
1168 goto error;
1169 }
1170
1171 ret = open_ust_metadata(app, ua_sess);
1172 if (ret < 0) {
1173 goto error;
1174 }
1175
1176 DBG2("UST metadata opened for app pid %d", app->key.pid);
1177 }
1178
1179 /* Open UST metadata stream */
1180 if (ua_sess->metadata->stream_obj == NULL) {
1181 ret = create_ust_stream(app, ua_sess);
1182 if (ret < 0) {
1183 goto error;
1184 }
1185
477d7741 1186 ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
5b4a0ec0
DG
1187 if (ret < 0) {
1188 PERROR("mkdir UST metadata");
1189 goto error;
1190 }
1191
477d7741
MD
1192 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1193 "%s/metadata", ua_sess->path);
5b4a0ec0
DG
1194 if (ret < 0) {
1195 PERROR("asprintf UST create stream");
1196 goto error;
1197 }
1198
1199 DBG2("UST metadata stream object created for app pid %d",
1200 app->key.pid);
1201 } else {
1202 ERR("Attempting to create stream without metadata opened");
1203 goto error;
1204 }
1205
1206 return 0;
1207
1208error:
1209 return -1;
1210}
1211
1212/*
1213 * Return pointer to traceable apps list.
1214 */
1215struct cds_lfht *ust_app_get_ht(void)
1216{
1217 return ust_app_ht;
1218}
1219
1220/*
1221 * Return ust app pointer or NULL if not found.
1222 */
1223struct ust_app *ust_app_find_by_pid(pid_t pid)
1224{
1225 struct cds_lfht_node *node;
1226 struct cds_lfht_iter iter;
1227
1228 rcu_read_lock();
1229 node = hashtable_lookup(ust_app_ht,
1230 (void *)((unsigned long) pid), sizeof(void *), &iter);
1231 if (node == NULL) {
1232 DBG2("UST app no found with pid %d", pid);
1233 goto error;
1234 }
1235 rcu_read_unlock();
1236
1237 DBG2("Found UST app by pid %d", pid);
1238
1239 return caa_container_of(node, struct ust_app, node);
1240
1241error:
1242 rcu_read_unlock();
1243 return NULL;
1244}
1245
1246/*
1247 * Using pid and uid (of the app), allocate a new ust_app struct and
1248 * add it to the global traceable app list.
1249 *
0df502fd
MD
1250 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1251 * bitness is not supported.
5b4a0ec0
DG
1252 */
1253int ust_app_register(struct ust_register_msg *msg, int sock)
1254{
1255 struct ust_app *lta;
1256
7753dea8
MD
1257 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1258 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
f943b0fb 1259 ERR("Registration failed: application \"%s\" (pid: %d) has "
7753dea8
MD
1260 "%d-bit long, but no consumerd for this long size is available.\n",
1261 msg->name, msg->pid, msg->bits_per_long);
88ff5b7f 1262 close(sock);
0df502fd
MD
1263 return -EINVAL;
1264 }
5b4a0ec0
DG
1265 lta = zmalloc(sizeof(struct ust_app));
1266 if (lta == NULL) {
1267 PERROR("malloc");
1268 return -ENOMEM;
1269 }
1270
1271 lta->ppid = msg->ppid;
1272 lta->uid = msg->uid;
1273 lta->gid = msg->gid;
7753dea8 1274 lta->bits_per_long = msg->bits_per_long;
5b4a0ec0
DG
1275 lta->v_major = msg->major;
1276 lta->v_minor = msg->minor;
1277 strncpy(lta->name, msg->name, sizeof(lta->name));
1278 lta->name[16] = '\0';
1279 lta->sessions = hashtable_new(0);
1280
1281 /* Set key map */
1282 lta->key.pid = msg->pid;
1283 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
1284 sizeof(void *));
1285 lta->key.sock = sock;
1286 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
1287 sizeof(void *));
1288
1289 rcu_read_lock();
1290 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
1291 hashtable_add_unique(ust_app_ht, &lta->node);
1292 rcu_read_unlock();
1293
1294 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1295 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
1296 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
1297
1298 return 0;
1299}
1300
1301/*
1302 * Unregister app by removing it from the global traceable app list and freeing
1303 * the data struct.
1304 *
1305 * The socket is already closed at this point so no close to sock.
1306 */
1307void ust_app_unregister(int sock)
1308{
1309 struct ust_app *lta;
1310 struct cds_lfht_node *node;
1311 struct cds_lfht_iter iter;
525b0740 1312 int ret;
5b4a0ec0
DG
1313
1314 rcu_read_lock();
1315 lta = find_app_by_sock(sock);
1316 if (lta == NULL) {
1317 ERR("Unregister app sock %d not found!", sock);
1318 goto error;
1319 }
1320
1321 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
1322
1323 /* Get the node reference for a call_rcu */
1324 node = hashtable_lookup(ust_app_ht,
1325 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
1326 if (node == NULL) {
1327 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
1328 goto error;
1329 }
284d8f55 1330
525b0740
MD
1331 ret = hashtable_del(ust_app_ht, &iter);
1332 assert(!ret);
5b4a0ec0 1333 call_rcu(&node->head, delete_ust_app_rcu);
284d8f55 1334error:
5b4a0ec0
DG
1335 rcu_read_unlock();
1336 return;
284d8f55
DG
1337}
1338
1339/*
5b4a0ec0 1340 * Return traceable_app_count
284d8f55 1341 */
5b4a0ec0 1342unsigned long ust_app_list_count(void)
284d8f55 1343{
5b4a0ec0 1344 unsigned long count;
284d8f55 1345
5b4a0ec0
DG
1346 rcu_read_lock();
1347 count = hashtable_get_count(ust_app_ht);
1348 rcu_read_unlock();
284d8f55 1349
5b4a0ec0 1350 return count;
284d8f55
DG
1351}
1352
5b4a0ec0
DG
1353/*
1354 * Fill events array with all events name of all registered apps.
1355 */
1356int ust_app_list_events(struct lttng_event **events)
421cb601 1357{
5b4a0ec0
DG
1358 int ret, handle;
1359 size_t nbmem, count = 0;
421cb601 1360 struct cds_lfht_iter iter;
5b4a0ec0
DG
1361 struct ust_app *app;
1362 struct lttng_event *tmp;
421cb601 1363
5b4a0ec0
DG
1364 nbmem = UST_APP_EVENT_LIST_SIZE;
1365 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1366 if (tmp == NULL) {
1367 PERROR("zmalloc ust app events");
1368 ret = -ENOMEM;
421cb601
DG
1369 goto error;
1370 }
1371
5b4a0ec0 1372 rcu_read_lock();
421cb601 1373
5b4a0ec0 1374 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
ac3bd9c0
MD
1375 struct lttng_ust_tracepoint_iter iter;
1376
5b4a0ec0
DG
1377 handle = ustctl_tracepoint_list(app->key.sock);
1378 if (handle < 0) {
1379 ERR("UST app list events getting handle failed for app pid %d",
1380 app->key.pid);
1381 continue;
1382 }
421cb601 1383
5b4a0ec0 1384 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
ac3bd9c0
MD
1385 &iter)) != -ENOENT) {
1386 memcpy(tmp[count].name, iter.name, LTTNG_UST_SYM_NAME_LEN);
1387 /* TODO : get loglevel too */
5b4a0ec0
DG
1388 if (count > nbmem) {
1389 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
1390 nbmem + UST_APP_EVENT_LIST_SIZE);
1391 nbmem += UST_APP_EVENT_LIST_SIZE;
2f221590 1392 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
5b4a0ec0
DG
1393 if (tmp == NULL) {
1394 PERROR("realloc ust app events");
1395 ret = -ENOMEM;
1396 goto rcu_error;
1397 }
1398 }
421cb601 1399
5b4a0ec0
DG
1400 tmp[count].type = LTTNG_UST_TRACEPOINT;
1401 tmp[count].pid = app->key.pid;
1402 tmp[count].enabled = -1;
1403 count++;
421cb601 1404 }
421cb601
DG
1405 }
1406
5b4a0ec0
DG
1407 ret = count;
1408 *events = tmp;
421cb601 1409
5b4a0ec0 1410 DBG2("UST app list events done (%zu events)", count);
421cb601 1411
5b4a0ec0
DG
1412rcu_error:
1413 rcu_read_unlock();
421cb601 1414error:
5b4a0ec0 1415 return ret;
421cb601
DG
1416}
1417
5b4a0ec0
DG
1418/*
1419 * Free and clean all traceable apps of the global list.
1420 */
1421void ust_app_clean_list(void)
421cb601 1422{
5b4a0ec0 1423 int ret;
5b4a0ec0
DG
1424 struct cds_lfht_iter iter;
1425 struct ust_app *app;
421cb601 1426
5b4a0ec0 1427 DBG2("UST app cleaning registered apps hash table");
421cb601 1428
5b4a0ec0 1429 rcu_read_lock();
421cb601 1430
778ca3dd 1431 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
5b4a0ec0 1432 ret = hashtable_del(ust_app_ht, &iter);
525b0740 1433 assert(!ret);
0d5d001d 1434 call_rcu(&iter.node->head, delete_ust_app_rcu);
421cb601
DG
1435 }
1436
5b4a0ec0
DG
1437 hashtable_destroy(ust_app_ht);
1438 hashtable_destroy(ust_app_sock_key_map);
421cb601 1439
5b4a0ec0
DG
1440 rcu_read_unlock();
1441}
1442
1443/*
1444 * Init UST app hash table.
1445 */
1446void ust_app_ht_alloc(void)
1447{
1448 ust_app_ht = hashtable_new(0);
1449 ust_app_sock_key_map = hashtable_new(0);
421cb601
DG
1450}
1451
78f0bacd
DG
1452/*
1453 * For a specific UST session, disable the channel for all registered apps.
1454 */
35a9059d 1455int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1456 struct ltt_ust_channel *uchan)
1457{
1458 int ret = 0;
1459 struct cds_lfht_iter iter;
8535a6d9 1460 struct cds_lfht_node *ua_chan_node;
78f0bacd
DG
1461 struct ust_app *app;
1462 struct ust_app_session *ua_sess;
8535a6d9 1463 struct ust_app_channel *ua_chan;
78f0bacd
DG
1464
1465 if (usess == NULL || uchan == NULL) {
1466 ERR("Disabling UST global channel with NULL values");
1467 ret = -1;
1468 goto error;
1469 }
1470
8535a6d9 1471 DBG2("UST app disabling channel %s from global domain for session uid %d",
78f0bacd
DG
1472 uchan->name, usess->uid);
1473
1474 rcu_read_lock();
1475
1476 /* For every registered applications */
1477 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1478 ua_sess = lookup_session_by_app(usess, app);
1479 if (ua_sess == NULL) {
1480 continue;
1481 }
1482
8535a6d9
DG
1483 /* Get channel */
1484 ua_chan_node = hashtable_lookup(ua_sess->channels,
1485 (void *)uchan->name, strlen(uchan->name), &iter);
1486 /* If the session if found for the app, the channel must be there */
1487 assert(ua_chan_node);
1488
1489 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1490 /* The channel must not be already disabled */
1491 assert(ua_chan->enabled == 1);
1492
1493 /* Disable channel onto application */
1494 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
1495 if (ret < 0) {
1496 /* XXX: We might want to report this error at some point... */
1497 continue;
1498 }
1499 }
1500
1501 rcu_read_unlock();
1502
1503error:
1504 return ret;
1505}
1506
1507/*
1508 * For a specific UST session, enable the channel for all registered apps.
1509 */
35a9059d 1510int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1511 struct ltt_ust_channel *uchan)
1512{
1513 int ret = 0;
1514 struct cds_lfht_iter iter;
1515 struct ust_app *app;
1516 struct ust_app_session *ua_sess;
1517
1518 if (usess == NULL || uchan == NULL) {
1519 ERR("Adding UST global channel to NULL values");
1520 ret = -1;
1521 goto error;
1522 }
1523
1524 DBG2("UST app enabling channel %s to global domain for session uid %d",
1525 uchan->name, usess->uid);
1526
1527 rcu_read_lock();
1528
1529 /* For every registered applications */
1530 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1531 ua_sess = lookup_session_by_app(usess, app);
1532 if (ua_sess == NULL) {
1533 continue;
1534 }
1535
1536 /* Enable channel onto application */
1537 ret = enable_ust_app_channel(ua_sess, uchan, app);
1538 if (ret < 0) {
1539 /* XXX: We might want to report this error at some point... */
1540 continue;
1541 }
1542 }
1543
1544 rcu_read_unlock();
1545
1546error:
1547 return ret;
1548}
1549
b0a40d28
DG
1550/*
1551 * Disable an event in a channel and for a specific session.
1552 */
35a9059d
DG
1553int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1554 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
1555{
1556 int ret = 0;
1557 struct cds_lfht_iter iter, uiter;
1558 struct cds_lfht_node *ua_chan_node, *ua_event_node;
1559 struct ust_app *app;
1560 struct ust_app_session *ua_sess;
1561 struct ust_app_channel *ua_chan;
1562 struct ust_app_event *ua_event;
1563
1564 DBG("UST app disabling event %s for all apps in channel "
35a9059d 1565 "%s for session uid %d", uevent->attr.name, uchan->name, usess->uid);
b0a40d28
DG
1566
1567 rcu_read_lock();
1568
1569 /* For all registered applications */
1570 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1571 ua_sess = lookup_session_by_app(usess, app);
1572 if (ua_sess == NULL) {
1573 /* Next app */
1574 continue;
1575 }
1576
1577 /* Lookup channel in the ust app session */
1578 ua_chan_node = hashtable_lookup(ua_sess->channels,
1579 (void *)uchan->name, strlen(uchan->name), &uiter);
1580 if (ua_chan_node == NULL) {
1581 DBG2("Channel %s not found in session uid %d for app pid %d."
1582 "Skipping", uchan->name, usess->uid, app->key.pid);
1583 continue;
1584 }
1585 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1586
1587 ua_event_node = hashtable_lookup(ua_chan->events,
35a9059d 1588 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
b0a40d28
DG
1589 if (ua_event_node == NULL) {
1590 DBG2("Event %s not found in channel %s for app pid %d."
35a9059d 1591 "Skipping", uevent->attr.name, uchan->name, app->key.pid);
b0a40d28
DG
1592 continue;
1593 }
1594 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1595
1596 ret = disable_ust_app_event(ua_sess, ua_chan, ua_event, app);
1597 if (ret < 0) {
1598 /* XXX: Report error someday... */
1599 continue;
1600 }
1601 }
1602
1603 rcu_read_unlock();
1604
1605 return ret;
1606}
1607
9730260e 1608/*
edb67388 1609 * For a specific UST session and UST channel, the event for all
9730260e
DG
1610 * registered apps.
1611 */
35a9059d 1612int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
9730260e
DG
1613 struct ltt_ust_channel *uchan)
1614{
1615 int ret = 0;
1616 struct cds_lfht_iter iter, uiter;
1617 struct cds_lfht_node *ua_chan_node;
1618 struct ust_app *app;
1619 struct ust_app_session *ua_sess;
1620 struct ust_app_channel *ua_chan;
1621 struct ust_app_event *ua_event;
1622
1623 DBG("UST app disabling all event for all apps in channel "
1624 "%s for session uid %d", uchan->name, usess->uid);
1625
1626 rcu_read_lock();
1627
1628 /* For all registered applications */
1629 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1630 ua_sess = lookup_session_by_app(usess, app);
edb67388
DG
1631 /* If ua_sess is NULL, there is a code flow error */
1632 assert(ua_sess);
9730260e
DG
1633
1634 /* Lookup channel in the ust app session */
edb67388
DG
1635 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1636 strlen(uchan->name), &uiter);
1637 /* If the channel is not found, there is a code flow error */
1638 assert(ua_chan_node);
1639
9730260e
DG
1640 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1641
1642 /* Disable each events of channel */
1643 cds_lfht_for_each_entry(ua_chan->events, &uiter, ua_event, node) {
1644 ret = disable_ust_app_event(ua_sess, ua_chan, ua_event, app);
1645 if (ret < 0) {
1646 /* XXX: Report error someday... */
1647 continue;
1648 }
1649 }
1650 }
1651
1652 rcu_read_unlock();
1653
1654 return ret;
1655}
1656
421cb601 1657/*
5b4a0ec0 1658 * For a specific UST session, create the channel for all registered apps.
421cb601 1659 */
35a9059d 1660int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
1661 struct ltt_ust_channel *uchan)
1662{
1663 int ret = 0;
1664 struct cds_lfht_iter iter;
48842b30
DG
1665 struct ust_app *app;
1666 struct ust_app_session *ua_sess;
1667 struct ust_app_channel *ua_chan;
1668
421cb601
DG
1669 if (usess == NULL || uchan == NULL) {
1670 ERR("Adding UST global channel to NULL values");
1671 ret = -1;
1672 goto error;
1673 }
1674
48842b30
DG
1675 DBG2("UST app adding channel %s to global domain for session uid %d",
1676 uchan->name, usess->uid);
1677
1678 rcu_read_lock();
421cb601 1679
5b4a0ec0
DG
1680 /* For every registered applications */
1681 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
edb67388
DG
1682 /*
1683 * Create session on the tracer side and add it to app session HT. Note
1684 * that if session exist, it will simply return a pointer to the ust
1685 * app session.
1686 */
421cb601 1687 ua_sess = create_ust_app_session(usess, app);
509cbaf8 1688 if (ua_sess == NULL) {
5b4a0ec0 1689 continue;
48842b30
DG
1690 }
1691
421cb601
DG
1692 /* Create channel onto application */
1693 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1694 if (ua_chan == NULL) {
5b4a0ec0 1695 continue;
48842b30 1696 }
48842b30 1697 }
5b4a0ec0 1698
48842b30
DG
1699 rcu_read_unlock();
1700
421cb601 1701error:
48842b30
DG
1702 return ret;
1703}
1704
5b4a0ec0 1705/*
edb67388 1706 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 1707 */
35a9059d 1708int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
1709 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1710{
1711 int ret = 0;
edb67388 1712 struct cds_lfht_iter iter, uiter;
35a9059d 1713 struct cds_lfht_node *ua_chan_node, *ua_event_node;
48842b30
DG
1714 struct ust_app *app;
1715 struct ust_app_session *ua_sess;
1716 struct ust_app_channel *ua_chan;
1717 struct ust_app_event *ua_event;
48842b30 1718
edb67388 1719 DBG("UST app enabling event %s for all apps for session uid %d",
48842b30
DG
1720 uevent->attr.name, usess->uid);
1721
edb67388
DG
1722 /*
1723 * NOTE: At this point, this function is called only if the session and
1724 * channel passed are already created for all apps. and enabled on the
1725 * tracer also.
1726 */
1727
48842b30 1728 rcu_read_lock();
421cb601
DG
1729
1730 /* For all registered applications */
5b4a0ec0 1731 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
edb67388
DG
1732 ua_sess = lookup_session_by_app(usess, app);
1733 /* If ua_sess is NULL, there is a code flow error */
1734 assert(ua_sess);
ba767faf 1735
edb67388
DG
1736 /* Lookup channel in the ust app session */
1737 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1738 strlen(uchan->name), &uiter);
1739 /* If the channel is not found, there is a code flow error */
1740 assert(ua_chan_node);
1741
1742 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1743
35a9059d
DG
1744 ua_event_node = hashtable_lookup(ua_sess->channels,
1745 (void*)uevent->attr.name, strlen(uevent->attr.name), &uiter);
1746 if (ua_event_node == NULL) {
1747 DBG3("UST app enable event %s not found. Skipping app",
1748 uevent->attr.name);
1749 continue;
1750 }
1751 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1752
1753 ret = enable_ust_app_event(ua_sess, ua_event, app);
1754 if (ret < 0) {
1755 /* XXX: Report error someday... */
1756 continue;
48842b30 1757 }
edb67388
DG
1758 }
1759
1760 rcu_read_unlock();
1761
1762 return ret;
1763}
1764
1765/*
1766 * For a specific existing UST session and UST channel, creates the event for
1767 * all registered apps.
1768 */
35a9059d 1769int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
1770 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1771{
1772 int ret = 0;
1773 struct cds_lfht_iter iter, uiter;
1774 struct cds_lfht_node *ua_chan_node;
1775 struct ust_app *app;
1776 struct ust_app_session *ua_sess;
1777 struct ust_app_channel *ua_chan;
1778
1779 DBG("UST app creating event %s for all apps for session uid %d",
1780 uevent->attr.name, usess->uid);
1781
1782 /*
1783 * NOTE: At this point, this function is called only if the session and
1784 * channel passed are already created for all apps. and enabled on the
1785 * tracer also.
1786 */
1787
1788 rcu_read_lock();
1789
1790 /* For all registered applications */
1791 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1792 ua_sess = lookup_session_by_app(usess, app);
1793 /* If ua_sess is NULL, there is a code flow error */
1794 assert(ua_sess);
48842b30
DG
1795
1796 /* Lookup channel in the ust app session */
edb67388
DG
1797 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1798 strlen(uchan->name), &uiter);
1799 /* If the channel is not found, there is a code flow error */
1800 assert(ua_chan_node);
1801
48842b30
DG
1802 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1803
edb67388
DG
1804 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1805 if (ret < 0) {
5b4a0ec0 1806 continue;
48842b30 1807 }
48842b30 1808 }
5b4a0ec0 1809
48842b30
DG
1810 rcu_read_unlock();
1811
1812 return ret;
1813}
1814
5b4a0ec0
DG
1815/*
1816 * Start tracing for a specific UST session and app.
1817 */
421cb601 1818int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
1819{
1820 int ret = 0;
1821 struct cds_lfht_iter iter;
48842b30
DG
1822 struct ust_app_session *ua_sess;
1823 struct ust_app_channel *ua_chan;
5b4a0ec0 1824 struct ltt_ust_stream *ustream;
7753dea8 1825 int consumerd_fd;
48842b30 1826
421cb601 1827 DBG("Starting tracing for ust app pid %d", app->key.pid);
5cf5d0e7 1828
509cbaf8
MD
1829 rcu_read_lock();
1830
421cb601
DG
1831 ua_sess = lookup_session_by_app(usess, app);
1832 if (ua_sess == NULL) {
509cbaf8 1833 goto error_rcu_unlock;
421cb601 1834 }
48842b30 1835
aea829b3
DG
1836 /* Upon restart, we skip the setup, already done */
1837 if (ua_sess->started) {
8be98f9a 1838 goto skip_setup;
aea829b3 1839 }
8be98f9a 1840
421cb601
DG
1841 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1842 if (ret < 0) {
509cbaf8 1843 goto error_rcu_unlock;
421cb601 1844 }
48842b30 1845
421cb601 1846 /* For each channel */
5b4a0ec0 1847 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
421cb601
DG
1848 /* Create all streams */
1849 while (1) {
5b4a0ec0 1850 /* Create UST stream */
421cb601
DG
1851 ustream = zmalloc(sizeof(*ustream));
1852 if (ustream == NULL) {
1853 PERROR("zmalloc ust stream");
509cbaf8 1854 goto error_rcu_unlock;
421cb601 1855 }
48842b30 1856
421cb601
DG
1857 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1858 &ustream->obj);
48842b30 1859 if (ret < 0) {
421cb601
DG
1860 /* Got all streams */
1861 break;
48842b30 1862 }
421cb601 1863 ustream->handle = ustream->obj->handle;
48842b30 1864
421cb601
DG
1865 /* Order is important */
1866 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
477d7741
MD
1867 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1868 ua_sess->path, ua_chan->name,
1869 ua_chan->streams.count++);
aba8e916
DG
1870 if (ret < 0) {
1871 PERROR("asprintf UST create stream");
421cb601 1872 continue;
aba8e916 1873 }
421cb601
DG
1874 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1875 ustream->pathname);
1876 }
421cb601 1877 }
aba8e916 1878
7753dea8
MD
1879 switch (app->bits_per_long) {
1880 case 64:
1881 consumerd_fd = ust_consumerd64_fd;
1882 break;
1883 case 32:
1884 consumerd_fd = ust_consumerd32_fd;
1885 break;
1886 default:
1887 ret = -EINVAL;
1888 goto error_rcu_unlock;
1889 }
aea829b3 1890
421cb601 1891 /* Setup UST consumer socket and send fds to it */
7753dea8 1892 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
421cb601 1893 if (ret < 0) {
509cbaf8 1894 goto error_rcu_unlock;
421cb601 1895 }
8be98f9a 1896 ua_sess->started = 1;
48842b30 1897
8be98f9a 1898skip_setup:
421cb601
DG
1899 /* This start the UST tracing */
1900 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1901 if (ret < 0) {
1902 ERR("Error starting tracing for app pid: %d", app->key.pid);
509cbaf8 1903 goto error_rcu_unlock;
421cb601 1904 }
5b4a0ec0 1905
509cbaf8 1906 rcu_read_unlock();
48842b30 1907
421cb601
DG
1908 /* Quiescent wait after starting trace */
1909 ustctl_wait_quiescent(app->key.sock);
48842b30 1910
421cb601 1911 return 0;
48842b30 1912
509cbaf8
MD
1913error_rcu_unlock:
1914 rcu_read_unlock();
421cb601
DG
1915 return -1;
1916}
48842b30 1917
8be98f9a
MD
1918/*
1919 * Stop tracing for a specific UST session and app.
1920 */
1921int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1922{
1923 int ret = 0;
1924 struct ust_app_session *ua_sess;
1925
1926 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1927
1928 rcu_read_lock();
1929
1930 ua_sess = lookup_session_by_app(usess, app);
1931 if (ua_sess == NULL) {
1932 /* Only malloc can failed so something is really wrong */
1933 goto error_rcu_unlock;
1934 }
1935
1936#if 0 /* only useful when periodical flush will be supported */
1937 /* need to keep a handle on shm in session for this. */
1938 /* Flush all buffers before stopping */
1939 ret = ustctl_flush_buffer(usess->sock, usess->metadata->obj);
1940 if (ret < 0) {
1941 ERR("UST metadata flush failed");
1942 }
1943
1944 cds_list_for_each_entry(ustchan, &usess->channels.head, list) {
1945 ret = ustctl_flush_buffer(usess->sock, ustchan->obj);
1946 if (ret < 0) {
1947 ERR("UST flush buffer error");
1948 }
1949 }
1950#endif
1951
1952 /* This inhibits UST tracing */
1953 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1954 if (ret < 0) {
1955 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1956 goto error_rcu_unlock;
1957 }
1958
1959 rcu_read_unlock();
1960
1961 /* Quiescent wait after stopping trace */
1962 ustctl_wait_quiescent(app->key.sock);
1963
1964 return 0;
1965
1966error_rcu_unlock:
1967 rcu_read_unlock();
1968 return -1;
1969}
1970
84cd17c6
MD
1971/*
1972 * Destroy a specific UST session in apps.
1973 */
1974int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1975{
1976 struct ust_app_session *ua_sess;
1977 struct lttng_ust_object_data obj;
1978 struct cds_lfht_iter iter;
1979 struct cds_lfht_node *node;
525b0740 1980 int ret;
84cd17c6
MD
1981
1982 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1983
1984 rcu_read_lock();
1985
1986 __lookup_session_by_app(usess, app, &iter);
1987 node = hashtable_iter_get_node(&iter);
1988 if (node == NULL) {
1989 /* Only malloc can failed so something is really wrong */
1990 goto error_rcu_unlock;
1991 }
1992 ua_sess = caa_container_of(node, struct ust_app_session, node);
525b0740
MD
1993 ret = hashtable_del(app->sessions, &iter);
1994 assert(!ret);
84cd17c6
MD
1995 delete_ust_app_session(app->key.sock, ua_sess);
1996 obj.handle = ua_sess->handle;
1997 obj.shm_fd = -1;
1998 obj.wait_fd = -1;
1999 obj.memory_map_size = 0;
2000 ustctl_release_object(app->key.sock, &obj);
2001
2002 rcu_read_unlock();
2003
2004 /* Quiescent wait after stopping trace */
2005 ustctl_wait_quiescent(app->key.sock);
2006
2007 return 0;
2008
2009error_rcu_unlock:
2010 rcu_read_unlock();
2011 return -1;
2012}
2013
5b4a0ec0
DG
2014/*
2015 * Start tracing for the UST session.
2016 */
421cb601
DG
2017int ust_app_start_trace_all(struct ltt_ust_session *usess)
2018{
2019 int ret = 0;
2020 struct cds_lfht_iter iter;
421cb601 2021 struct ust_app *app;
48842b30 2022
421cb601
DG
2023 DBG("Starting all UST traces");
2024
2025 rcu_read_lock();
421cb601 2026
5b4a0ec0 2027 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
421cb601 2028 ret = ust_app_start_trace(usess, app);
48842b30 2029 if (ret < 0) {
5b4a0ec0
DG
2030 /* Continue to next apps even on error */
2031 continue;
48842b30 2032 }
48842b30 2033 }
5b4a0ec0 2034
48842b30
DG
2035 rcu_read_unlock();
2036
2037 return 0;
2038}
487cf67c 2039
8be98f9a
MD
2040/*
2041 * Start tracing for the UST session.
2042 */
2043int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2044{
2045 int ret = 0;
2046 struct cds_lfht_iter iter;
2047 struct ust_app *app;
2048
2049 DBG("Stopping all UST traces");
2050
2051 rcu_read_lock();
2052
2053 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2054 ret = ust_app_stop_trace(usess, app);
2055 if (ret < 0) {
2056 /* Continue to next apps even on error */
2057 continue;
2058 }
2059 }
2060
2061 rcu_read_unlock();
2062
2063 return 0;
2064}
2065
84cd17c6
MD
2066/*
2067 * Destroy app UST session.
2068 */
2069int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2070{
2071 int ret = 0;
2072 struct cds_lfht_iter iter;
2073 struct ust_app *app;
2074
2075 DBG("Destroy all UST traces");
2076
2077 rcu_read_lock();
2078
2079 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2080 ret = ust_app_destroy_trace(usess, app);
2081 if (ret < 0) {
2082 /* Continue to next apps even on error */
2083 continue;
2084 }
2085 }
2086
2087 rcu_read_unlock();
2088
2089 return 0;
2090}
2091
5b4a0ec0
DG
2092/*
2093 * Add channels/events from UST global domain to registered apps at sock.
2094 */
487cf67c
DG
2095void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2096{
2097 int ret = 0;
487cf67c 2098 struct cds_lfht_iter iter;
487cf67c
DG
2099 struct ust_app *app;
2100 struct ust_app_session *ua_sess;
2101 struct ust_app_channel *ua_chan;
2102 struct ust_app_event *ua_event;
1f3580c7
DG
2103
2104 if (usess == NULL) {
5b4a0ec0 2105 ERR("No UST session on global update. Returning");
1f3580c7
DG
2106 goto error;
2107 }
2108
487cf67c
DG
2109 DBG2("UST app global update for app sock %d for session uid %d", sock,
2110 usess->uid);
2111
284d8f55
DG
2112 rcu_read_lock();
2113
487cf67c
DG
2114 app = find_app_by_sock(sock);
2115 if (app == NULL) {
2116 ERR("Failed to update app sock %d", sock);
2117 goto error;
2118 }
2119
421cb601 2120 ua_sess = create_ust_app_session(usess, app);
487cf67c 2121 if (ua_sess == NULL) {
487cf67c
DG
2122 goto error;
2123 }
2124
284d8f55
DG
2125 /*
2126 * We can iterate safely here over all UST app session sicne the create ust
2127 * app session above made a shadow copy of the UST global domain from the
2128 * ltt ust session.
2129 */
2130 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
2131 ret = create_ust_channel(app, ua_sess, ua_chan);
2132 if (ret < 0) {
2133 /* FIXME: Should we quit here or continue... */
2134 continue;
487cf67c
DG
2135 }
2136
284d8f55
DG
2137 /* For each events */
2138 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
2139 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2140 if (ret < 0) {
2141 /* FIXME: Should we quit here or continue... */
2142 continue;
487cf67c 2143 }
36dc12cc 2144 }
487cf67c
DG
2145 }
2146
36dc12cc 2147 if (usess->start_trace) {
421cb601 2148 ret = ust_app_start_trace(usess, app);
36dc12cc 2149 if (ret < 0) {
36dc12cc
DG
2150 goto error;
2151 }
2152
36dc12cc
DG
2153 DBG2("UST trace started for app pid %d", app->key.pid);
2154 }
2155
487cf67c
DG
2156error:
2157 rcu_read_unlock();
2158 return;
2159}
55cc08a6
DG
2160
2161/*
2162 * Add context to a specific channel for global UST domain.
2163 */
2164int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2165 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2166{
2167 int ret = 0;
2168 struct cds_lfht_node *ua_chan_node;
2169 struct cds_lfht_iter iter, uiter;
2170 struct ust_app_channel *ua_chan = NULL;
2171 struct ust_app_session *ua_sess;
2172 struct ust_app *app;
2173
2174 rcu_read_lock();
2175
2176 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2177 ua_sess = lookup_session_by_app(usess, app);
2178 if (ua_sess == NULL) {
2179 continue;
2180 }
2181
2182 /* Lookup channel in the ust app session */
2183 ua_chan_node = hashtable_lookup(ua_sess->channels,
2184 (void *)uchan->name, strlen(uchan->name), &uiter);
2185 if (ua_chan_node == NULL) {
2186 continue;
2187 }
2188 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2189 node);
2190
2191 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2192 if (ret < 0) {
2193 continue;
2194 }
2195 }
2196
2197 /* Add ltt UST context node to ltt UST channel */
2198 hashtable_add_unique(uchan->ctx, &uctx->node);
2199
2200 rcu_read_unlock();
2201 return ret;
2202}
2203
2204/*
2205 * Add context to a specific event in a channel for global UST domain.
2206 */
2207int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2208 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2209 struct ltt_ust_context *uctx)
2210{
2211 int ret = 0;
2212 struct cds_lfht_node *ua_chan_node, *ua_event_node;
2213 struct cds_lfht_iter iter, uiter;
2214 struct ust_app_session *ua_sess;
2215 struct ust_app_event *ua_event;
2216 struct ust_app_channel *ua_chan = NULL;
2217 struct ust_app *app;
2218
2219 rcu_read_lock();
2220
2221 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2222 ua_sess = lookup_session_by_app(usess, app);
2223 if (ua_sess == NULL) {
2224 continue;
2225 }
2226
2227 /* Lookup channel in the ust app session */
2228 ua_chan_node = hashtable_lookup(ua_sess->channels,
2229 (void *)uchan->name, strlen(uchan->name), &uiter);
2230 if (ua_chan_node == NULL) {
2231 continue;
2232 }
2233 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2234 node);
2235
2236 ua_event_node = hashtable_lookup(ua_chan->events,
2237 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
2238 if (ua_event_node == NULL) {
2239 continue;
2240 }
2241 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2242 node);
2243
2244 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2245 if (ret < 0) {
2246 continue;
2247 }
2248 }
2249
2250 /* Add ltt UST context node to ltt UST event */
2251 hashtable_add_unique(uevent->ctx, &uctx->node);
2252
2253 rcu_read_unlock();
2254 return ret;
2255}
This page took 0.219327 seconds and 4 git commands to generate.