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