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