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