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