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