Fix: Conditionally disable test requiring shared libs
[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 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
91d76f53
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
91d76f53
DG
16 */
17
18#define _GNU_SOURCE
19#include <errno.h>
20#include <pthread.h>
21#include <stdio.h>
22#include <stdlib.h>
099e26bd 23#include <string.h>
aba8e916
DG
24#include <sys/stat.h>
25#include <sys/types.h>
099e26bd 26#include <unistd.h>
0df502fd 27#include <urcu/compiler.h>
fb54cdbf 28#include <lttng/ust-error.h>
bec39940 29
990570ed 30#include <common/common.h>
86acf0da 31#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 32
86acf0da
DG
33#include "fd-limit.h"
34#include "health.h"
56fff090 35#include "ust-app.h"
48842b30 36#include "ust-consumer.h"
d80a6244
DG
37#include "ust-ctl.h"
38
025faf73
DG
39/*
40 * Match function for the hash table lookup.
41 *
42 * It matches an ust app event based on three attributes which are the event
43 * name, the filter bytecode and the loglevel.
44 */
18eace3b
DG
45static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
46{
47 struct ust_app_event *event;
48 const struct ust_app_ht_key *key;
49
50 assert(node);
51 assert(_key);
52
53 event = caa_container_of(node, struct ust_app_event, node.node);
54 key = _key;
55
56 /* Match the 3 elements of the key: name, filter and loglevel. */
57
58 /* Event name */
59 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
60 goto no_match;
61 }
62
63 /* Event loglevel. */
64 if (event->attr.loglevel != key->loglevel) {
025faf73
DG
65 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
66 && key->loglevel == 0 && event->attr.loglevel == -1) {
67 /*
68 * Match is accepted. This is because on event creation, the
69 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
70 * -1 are accepted for this loglevel type since 0 is the one set by
71 * the API when receiving an enable event.
72 */
73 } else {
74 goto no_match;
75 }
18eace3b
DG
76 }
77
78 /* One of the filters is NULL, fail. */
79 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
80 goto no_match;
81 }
82
025faf73
DG
83 if (key->filter && event->filter) {
84 /* Both filters exists, check length followed by the bytecode. */
85 if (event->filter->len != key->filter->len ||
86 memcmp(event->filter->data, key->filter->data,
87 event->filter->len) != 0) {
88 goto no_match;
89 }
18eace3b
DG
90 }
91
025faf73 92 /* Match. */
18eace3b
DG
93 return 1;
94
95no_match:
96 return 0;
18eace3b
DG
97}
98
025faf73
DG
99/*
100 * Unique add of an ust app event in the given ht. This uses the custom
101 * ht_match_ust_app_event match function and the event name as hash.
102 */
18eace3b
DG
103static void add_unique_ust_app_event(struct lttng_ht *ht,
104 struct ust_app_event *event)
105{
106 struct cds_lfht_node *node_ptr;
107 struct ust_app_ht_key key;
108
109 assert(ht);
110 assert(ht->ht);
111 assert(event);
112
113 key.name = event->attr.name;
114 key.filter = event->filter;
115 key.loglevel = event->attr.loglevel;
116
117 node_ptr = cds_lfht_add_unique(ht->ht,
118 ht->hash_fct(event->node.key, lttng_ht_seed),
119 ht_match_ust_app_event, &key, &event->node.node);
120 assert(node_ptr == &event->node.node);
121}
122
55cc08a6
DG
123/*
124 * Delete ust context safely. RCU read lock must be held before calling
125 * this function.
126 */
127static
128void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
129{
130 if (ua_ctx->obj) {
131 ustctl_release_object(sock, ua_ctx->obj);
132 free(ua_ctx->obj);
133 }
134 free(ua_ctx);
135}
136
d80a6244
DG
137/*
138 * Delete ust app event safely. RCU read lock must be held before calling
139 * this function.
140 */
8b366481
DG
141static
142void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
d80a6244 143{
53a80697 144 free(ua_event->filter);
d80a6244 145
edb67388
DG
146 if (ua_event->obj != NULL) {
147 ustctl_release_object(sock, ua_event->obj);
148 free(ua_event->obj);
149 }
d80a6244
DG
150 free(ua_event);
151}
152
153/*
154 * Delete ust app stream safely. RCU read lock must be held before calling
155 * this function.
156 */
8b366481 157static
030a66fa 158void delete_ust_app_stream(int sock, struct ust_app_stream *stream)
d80a6244 159{
8b366481
DG
160 if (stream->obj) {
161 ustctl_release_object(sock, stream->obj);
4063050c 162 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
163 free(stream->obj);
164 }
84cd17c6 165 free(stream);
d80a6244
DG
166}
167
168/*
169 * Delete ust app channel safely. RCU read lock must be held before calling
170 * this function.
171 */
8b366481
DG
172static
173void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
d80a6244
DG
174{
175 int ret;
bec39940 176 struct lttng_ht_iter iter;
d80a6244 177 struct ust_app_event *ua_event;
55cc08a6 178 struct ust_app_ctx *ua_ctx;
030a66fa 179 struct ust_app_stream *stream, *stmp;
d80a6244 180
55cc08a6 181 /* Wipe stream */
d80a6244 182 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 183 cds_list_del(&stream->list);
d80a6244
DG
184 delete_ust_app_stream(sock, stream);
185 }
186
55cc08a6 187 /* Wipe context */
bec39940
DG
188 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
189 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6
DG
190 assert(!ret);
191 delete_ust_app_ctx(sock, ua_ctx);
192 }
bec39940 193 lttng_ht_destroy(ua_chan->ctx);
d80a6244 194
55cc08a6 195 /* Wipe events */
bec39940
DG
196 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
197 node.node) {
198 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 199 assert(!ret);
d80a6244
DG
200 delete_ust_app_event(sock, ua_event);
201 }
bec39940 202 lttng_ht_destroy(ua_chan->events);
edb67388
DG
203
204 if (ua_chan->obj != NULL) {
205 ustctl_release_object(sock, ua_chan->obj);
4063050c 206 lttng_fd_put(LTTNG_FD_APPS, 2);
edb67388
DG
207 free(ua_chan->obj);
208 }
84cd17c6 209 free(ua_chan);
d80a6244
DG
210}
211
212/*
213 * Delete ust app session safely. RCU read lock must be held before calling
214 * this function.
215 */
8b366481
DG
216static
217void delete_ust_app_session(int sock, struct ust_app_session *ua_sess)
d80a6244
DG
218{
219 int ret;
bec39940 220 struct lttng_ht_iter iter;
d80a6244
DG
221 struct ust_app_channel *ua_chan;
222
223 if (ua_sess->metadata) {
8b366481
DG
224 if (ua_sess->metadata->stream_obj) {
225 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
4063050c 226 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
227 free(ua_sess->metadata->stream_obj);
228 }
229 if (ua_sess->metadata->obj) {
230 ustctl_release_object(sock, ua_sess->metadata->obj);
4063050c 231 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
232 free(ua_sess->metadata->obj);
233 }
a2c0da86 234 trace_ust_destroy_metadata(ua_sess->metadata);
d80a6244
DG
235 }
236
bec39940
DG
237 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
238 node.node) {
239 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 240 assert(!ret);
d80a6244
DG
241 delete_ust_app_channel(sock, ua_chan);
242 }
bec39940 243 lttng_ht_destroy(ua_sess->channels);
d80a6244 244
aee6bafd
MD
245 if (ua_sess->handle != -1) {
246 ustctl_release_handle(sock, ua_sess->handle);
247 }
8b366481 248 free(ua_sess);
d80a6244 249}
91d76f53
DG
250
251/*
284d8f55
DG
252 * Delete a traceable application structure from the global list. Never call
253 * this function outside of a call_rcu call.
91d76f53 254 */
8b366481
DG
255static
256void delete_ust_app(struct ust_app *app)
91d76f53 257{
8b366481 258 int ret, sock;
d42f20df 259 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 260
f6a9efaa 261 rcu_read_lock();
44d3bd01 262
d80a6244 263 /* Delete ust app sessions info */
852d0037
DG
264 sock = app->sock;
265 app->sock = -1;
d80a6244 266
d42f20df
DG
267 lttng_ht_destroy(app->sessions);
268
8b366481 269 /* Wipe sessions */
d42f20df
DG
270 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
271 teardown_node) {
272 /* Free every object in the session and the session. */
273 delete_ust_app_session(sock, ua_sess);
d80a6244 274 }
d80a6244 275
6414a713 276 /*
852d0037
DG
277 * Wait until we have deleted the application from the sock hash table
278 * before closing this socket, otherwise an application could re-use the
279 * socket ID and race with the teardown, using the same hash table entry.
280 *
281 * It's OK to leave the close in call_rcu. We want it to stay unique for
282 * all RCU readers that could run concurrently with unregister app,
283 * therefore we _need_ to only close that socket after a grace period. So
284 * it should stay in this RCU callback.
285 *
286 * This close() is a very important step of the synchronization model so
287 * every modification to this function must be carefully reviewed.
6414a713 288 */
799e2c4f
MD
289 ret = close(sock);
290 if (ret) {
291 PERROR("close");
292 }
4063050c 293 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 294
852d0037 295 DBG2("UST app pid %d deleted", app->pid);
284d8f55 296 free(app);
bec39940 297
f6a9efaa 298 rcu_read_unlock();
099e26bd
DG
299}
300
301/*
f6a9efaa 302 * URCU intermediate call to delete an UST app.
099e26bd 303 */
8b366481
DG
304static
305void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 306{
bec39940
DG
307 struct lttng_ht_node_ulong *node =
308 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 309 struct ust_app *app =
852d0037 310 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 311
852d0037 312 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 313 delete_ust_app(app);
099e26bd
DG
314}
315
8b366481
DG
316/*
317 * Alloc new UST app session.
318 */
319static
320struct ust_app_session *alloc_ust_app_session(void)
321{
322 struct ust_app_session *ua_sess;
323
324 /* Init most of the default value by allocating and zeroing */
325 ua_sess = zmalloc(sizeof(struct ust_app_session));
326 if (ua_sess == NULL) {
327 PERROR("malloc");
328 goto error;
329 }
330
331 ua_sess->handle = -1;
bec39940 332 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
8b366481
DG
333
334 return ua_sess;
335
336error:
337 return NULL;
338}
339
340/*
341 * Alloc new UST app channel.
342 */
343static
344struct ust_app_channel *alloc_ust_app_channel(char *name,
345 struct lttng_ust_channel *attr)
346{
347 struct ust_app_channel *ua_chan;
348
349 /* Init most of the default value by allocating and zeroing */
350 ua_chan = zmalloc(sizeof(struct ust_app_channel));
351 if (ua_chan == NULL) {
352 PERROR("malloc");
353 goto error;
354 }
355
356 /* Setup channel name */
357 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
358 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
359
360 ua_chan->enabled = 1;
361 ua_chan->handle = -1;
bec39940
DG
362 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
363 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
364 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
365
366 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
367
368 /* Copy attributes */
369 if (attr) {
2fe6e7f5
DG
370 /* Translate from lttng_ust_channel to lttng_ust_channel_attr.*/
371 ua_chan->attr.subbuf_size = attr->subbuf_size;
372 ua_chan->attr.num_subbuf = attr->num_subbuf;
373 ua_chan->attr.overwrite = attr->overwrite;
374 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
375 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
376 ua_chan->attr.output = attr->output;
8b366481
DG
377 }
378
379 DBG3("UST app channel %s allocated", ua_chan->name);
380
381 return ua_chan;
382
383error:
384 return NULL;
385}
386
37f1c236
DG
387/*
388 * Allocate and initialize a UST app stream.
389 *
390 * Return newly allocated stream pointer or NULL on error.
391 */
392static struct ust_app_stream *alloc_ust_app_stream(void)
393{
394 struct ust_app_stream *stream = NULL;
395
396 stream = zmalloc(sizeof(*stream));
397 if (stream == NULL) {
398 PERROR("zmalloc ust app stream");
399 goto error;
400 }
401
402 /* Zero could be a valid value for a handle so flag it to -1. */
403 stream->handle = -1;
404
405error:
406 return stream;
407}
408
8b366481
DG
409/*
410 * Alloc new UST app event.
411 */
412static
413struct ust_app_event *alloc_ust_app_event(char *name,
414 struct lttng_ust_event *attr)
415{
416 struct ust_app_event *ua_event;
417
418 /* Init most of the default value by allocating and zeroing */
419 ua_event = zmalloc(sizeof(struct ust_app_event));
420 if (ua_event == NULL) {
421 PERROR("malloc");
422 goto error;
423 }
424
425 ua_event->enabled = 1;
426 strncpy(ua_event->name, name, sizeof(ua_event->name));
427 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 428 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
429
430 /* Copy attributes */
431 if (attr) {
432 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
433 }
434
435 DBG3("UST app event %s allocated", ua_event->name);
436
437 return ua_event;
438
439error:
440 return NULL;
441}
442
443/*
444 * Alloc new UST app context.
445 */
446static
447struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
448{
449 struct ust_app_ctx *ua_ctx;
450
451 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
452 if (ua_ctx == NULL) {
453 goto error;
454 }
455
456 if (uctx) {
457 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
458 }
459
460 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
461
462error:
463 return ua_ctx;
464}
465
025faf73
DG
466/*
467 * Allocate a filter and copy the given original filter.
468 *
469 * Return allocated filter or NULL on error.
470 */
471static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
472 struct lttng_ust_filter_bytecode *orig_f)
473{
474 struct lttng_ust_filter_bytecode *filter = NULL;
475
476 /* Copy filter bytecode */
477 filter = zmalloc(sizeof(*filter) + orig_f->len);
478 if (!filter) {
479 PERROR("zmalloc alloc ust app filter");
480 goto error;
481 }
482
483 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
484
485error:
486 return filter;
487}
488
099e26bd 489/*
421cb601
DG
490 * Find an ust_app using the sock and return it. RCU read side lock must be
491 * held before calling this helper function.
099e26bd 492 */
8b366481
DG
493static
494struct ust_app *find_app_by_sock(int sock)
099e26bd 495{
bec39940 496 struct lttng_ht_node_ulong *node;
bec39940 497 struct lttng_ht_iter iter;
f6a9efaa 498
852d0037 499 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 500 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
501 if (node == NULL) {
502 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
503 goto error;
504 }
852d0037
DG
505
506 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
507
508error:
509 return NULL;
099e26bd
DG
510}
511
025faf73
DG
512/*
513 * Lookup for an ust app event based on event name, filter bytecode and the
514 * event loglevel.
515 *
516 * Return an ust_app_event object or NULL on error.
517 */
18eace3b
DG
518static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
519 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel)
520{
521 struct lttng_ht_iter iter;
522 struct lttng_ht_node_str *node;
523 struct ust_app_event *event = NULL;
524 struct ust_app_ht_key key;
18eace3b
DG
525
526 assert(name);
527 assert(ht);
528
529 /* Setup key for event lookup. */
530 key.name = name;
531 key.filter = filter;
532 key.loglevel = loglevel;
533
025faf73
DG
534 /* Lookup using the event name as hash and a custom match fct. */
535 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
536 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
537 node = lttng_ht_iter_get_node_str(&iter);
538 if (node == NULL) {
539 goto end;
540 }
541
542 event = caa_container_of(node, struct ust_app_event, node);
543
544end:
18eace3b
DG
545 return event;
546}
547
55cc08a6
DG
548/*
549 * Create the channel context on the tracer.
550 */
551static
552int create_ust_channel_context(struct ust_app_channel *ua_chan,
553 struct ust_app_ctx *ua_ctx, struct ust_app *app)
554{
555 int ret;
556
840cb59c 557 health_code_update();
86acf0da 558
852d0037 559 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6
DG
560 ua_chan->obj, &ua_ctx->obj);
561 if (ret < 0) {
562 goto error;
563 }
564
565 ua_ctx->handle = ua_ctx->obj->handle;
566
727d5404 567 DBG2("UST app context created successfully for channel %s", ua_chan->name);
55cc08a6
DG
568
569error:
840cb59c 570 health_code_update();
55cc08a6
DG
571 return ret;
572}
573
53a80697
MD
574/*
575 * Set the filter on the tracer.
576 */
577static
578int set_ust_event_filter(struct ust_app_event *ua_event,
579 struct ust_app *app)
580{
581 int ret;
582
840cb59c 583 health_code_update();
86acf0da 584
53a80697 585 if (!ua_event->filter) {
86acf0da
DG
586 ret = 0;
587 goto error;
53a80697
MD
588 }
589
590 ret = ustctl_set_filter(app->sock, ua_event->filter,
591 ua_event->obj);
592 if (ret < 0) {
593 goto error;
594 }
595
596 DBG2("UST filter set successfully for event %s", ua_event->name);
597
598error:
840cb59c 599 health_code_update();
53a80697
MD
600 return ret;
601}
602
9730260e
DG
603/*
604 * Disable the specified event on to UST tracer for the UST session.
605 */
606static int disable_ust_event(struct ust_app *app,
607 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
608{
609 int ret;
610
840cb59c 611 health_code_update();
86acf0da 612
852d0037 613 ret = ustctl_disable(app->sock, ua_event->obj);
9730260e
DG
614 if (ret < 0) {
615 ERR("UST app event %s disable failed for app (pid: %d) "
616 "and session handle %d with ret %d",
852d0037 617 ua_event->attr.name, app->pid, ua_sess->handle, ret);
9730260e
DG
618 goto error;
619 }
620
621 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 622 ua_event->attr.name, app->pid);
9730260e
DG
623
624error:
840cb59c 625 health_code_update();
9730260e
DG
626 return ret;
627}
628
78f0bacd
DG
629/*
630 * Disable the specified channel on to UST tracer for the UST session.
631 */
632static int disable_ust_channel(struct ust_app *app,
633 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
634{
635 int ret;
636
840cb59c 637 health_code_update();
86acf0da 638
852d0037 639 ret = ustctl_disable(app->sock, ua_chan->obj);
78f0bacd
DG
640 if (ret < 0) {
641 ERR("UST app channel %s disable failed for app (pid: %d) "
642 "and session handle %d with ret %d",
852d0037 643 ua_chan->name, app->pid, ua_sess->handle, ret);
78f0bacd
DG
644 goto error;
645 }
646
78f0bacd 647 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 648 ua_chan->name, app->pid);
78f0bacd
DG
649
650error:
840cb59c 651 health_code_update();
78f0bacd
DG
652 return ret;
653}
654
655/*
656 * Enable the specified channel on to UST tracer for the UST session.
657 */
658static int enable_ust_channel(struct ust_app *app,
659 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
660{
661 int ret;
662
840cb59c 663 health_code_update();
86acf0da 664
852d0037 665 ret = ustctl_enable(app->sock, ua_chan->obj);
78f0bacd
DG
666 if (ret < 0) {
667 ERR("UST app channel %s enable failed for app (pid: %d) "
668 "and session handle %d with ret %d",
852d0037 669 ua_chan->name, app->pid, ua_sess->handle, ret);
78f0bacd
DG
670 goto error;
671 }
672
673 ua_chan->enabled = 1;
674
675 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 676 ua_chan->name, app->pid);
78f0bacd
DG
677
678error:
840cb59c 679 health_code_update();
78f0bacd
DG
680 return ret;
681}
682
edb67388
DG
683/*
684 * Enable the specified event on to UST tracer for the UST session.
685 */
686static int enable_ust_event(struct ust_app *app,
687 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
688{
689 int ret;
690
840cb59c 691 health_code_update();
86acf0da 692
852d0037 693 ret = ustctl_enable(app->sock, ua_event->obj);
edb67388
DG
694 if (ret < 0) {
695 ERR("UST app event %s enable failed for app (pid: %d) "
696 "and session handle %d with ret %d",
852d0037 697 ua_event->attr.name, app->pid, ua_sess->handle, ret);
edb67388
DG
698 goto error;
699 }
700
701 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 702 ua_event->attr.name, app->pid);
edb67388
DG
703
704error:
840cb59c 705 health_code_update();
edb67388
DG
706 return ret;
707}
708
099e26bd 709/*
5b4a0ec0 710 * Open metadata onto the UST tracer for a UST session.
0177d773 711 */
5b4a0ec0
DG
712static int open_ust_metadata(struct ust_app *app,
713 struct ust_app_session *ua_sess)
0177d773 714{
5b4a0ec0
DG
715 int ret;
716 struct lttng_ust_channel_attr uattr;
0177d773 717
840cb59c 718 health_code_update();
86acf0da 719
5b4a0ec0
DG
720 uattr.overwrite = ua_sess->metadata->attr.overwrite;
721 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
722 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
723 uattr.switch_timer_interval =
724 ua_sess->metadata->attr.switch_timer_interval;
725 uattr.read_timer_interval =
726 ua_sess->metadata->attr.read_timer_interval;
727 uattr.output = ua_sess->metadata->attr.output;
728
4063050c
MD
729 /* We are going to receive 2 fds, we need to reserve them. */
730 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
731 if (ret < 0) {
732 ERR("Exhausted number of available FD upon metadata open");
733 goto error;
734 }
5b4a0ec0 735 /* UST tracer metadata creation */
852d0037 736 ret = ustctl_open_metadata(app->sock, ua_sess->handle, &uattr,
5b4a0ec0
DG
737 &ua_sess->metadata->obj);
738 if (ret < 0) {
fc34caaa 739 ERR("UST app open metadata failed for app pid:%d with ret %d",
852d0037 740 app->pid, ret);
f6a9efaa 741 goto error;
0177d773 742 }
f6a9efaa 743
6d3686da
DG
744 ua_sess->metadata->handle = ua_sess->metadata->obj->handle;
745
f6a9efaa 746error:
840cb59c 747 health_code_update();
5b4a0ec0 748 return ret;
91d76f53
DG
749}
750
751/*
5922e6c2 752 * Create metadata stream onto the UST tracer for a given session.
91d76f53 753 */
5922e6c2 754static int create_ust_metadata_stream(struct ust_app *app,
5b4a0ec0 755 struct ust_app_session *ua_sess)
91d76f53 756{
5b4a0ec0 757 int ret;
421cb601 758
840cb59c 759 health_code_update();
86acf0da 760
4063050c
MD
761 /* We are going to receive 2 fds, we need to reserve them. */
762 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
763 if (ret < 0) {
764 ERR("Exhausted number of available FD upon metadata stream create");
765 goto error;
766 }
852d0037 767 ret = ustctl_create_stream(app->sock, ua_sess->metadata->obj,
5b4a0ec0
DG
768 &ua_sess->metadata->stream_obj);
769 if (ret < 0) {
2ba04c3d 770 lttng_fd_put(LTTNG_FD_APPS, 2);
5b4a0ec0 771 ERR("UST create metadata stream failed");
421cb601 772 goto error;
91d76f53 773 }
421cb601 774
421cb601 775error:
840cb59c 776 health_code_update();
5b4a0ec0 777 return ret;
91d76f53
DG
778}
779
4f3ab6ee
DG
780/*
781 * Create stream onto the UST tracer for a given channel.
782 *
783 * Return -ENOENT if no more stream is available for this channel.
784 * On success, return 0.
785 * On error, return a negative value.
786 */
787static int create_ust_stream(struct ust_app *app,
030a66fa 788 struct ust_app_channel *ua_chan, struct ust_app_stream *stream)
4f3ab6ee
DG
789{
790 int ret;
791
792 assert(app);
793 assert(ua_chan);
794 assert(ua_chan->obj);
795 assert(stream);
796
840cb59c 797 health_code_update();
4f3ab6ee
DG
798
799 /* We are going to receive 2 fds, we need to reserve them. */
800 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
801 if (ret < 0) {
802 ERR("Exhausted number of available FD on stream creation");
803 /* Just to make sure we never return -ENOENT. */
804 ret = -1;
805 goto error;
806 }
807
7f133705
DG
808 /*
809 * Set the stream name before creating it. On error, we don't have to
810 * delete it on the tracer side.
811 */
812 ret = snprintf(stream->name, sizeof(stream->name), "%s_%u",
813 ua_chan->name, ua_chan->streams.count);
814 if (ret < 0) {
815 /* Without the stream name we can't continue using it. */
816 PERROR("snprintf UST create stream");
817 /* Just to make sure we never return -ENOENT. */
818 ret = -1;
819 goto error;
820 }
821
4f3ab6ee
DG
822 ret = ustctl_create_stream(app->sock, ua_chan->obj, &stream->obj);
823 if (ret < 0) {
824 lttng_fd_put(LTTNG_FD_APPS, 2);
825 /* Indicates that there is no more stream for that channel. */
495bbffb 826 if (ret != -LTTNG_UST_ERR_NOENT) {
4f3ab6ee
DG
827 ERR("UST create metadata stream failed (ret: %d)", ret);
828 }
829 goto error;
830 }
831
832 /* Set stream handle with the returned value. */
833 stream->handle = stream->obj->handle;
834
835error:
840cb59c 836 health_code_update();
4f3ab6ee
DG
837 return ret;
838}
839
b551a063 840/*
5b4a0ec0 841 * Create the specified channel onto the UST tracer for a UST session.
b551a063 842 */
5b4a0ec0
DG
843static int create_ust_channel(struct ust_app *app,
844 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
b551a063 845{
5b4a0ec0 846 int ret;
b551a063 847
840cb59c 848 health_code_update();
86acf0da 849
4063050c
MD
850 /* We are going to receive 2 fds, we need to reserve them. */
851 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
852 if (ret < 0) {
853 ERR("Exhausted number of available FD upon create channel");
854 goto error;
855 }
86acf0da 856
840cb59c 857 health_code_update();
86acf0da 858
2fe6e7f5
DG
859 ret = ustctl_create_channel(app->sock, ua_sess->handle, &ua_chan->attr,
860 &ua_chan->obj);
5b4a0ec0 861 if (ret < 0) {
58f3ca76 862 ERR("Creating channel %s for app (pid: %d, sock: %d) "
5b4a0ec0 863 "and session handle %d with ret %d",
852d0037 864 ua_chan->name, app->pid, app->sock,
5b4a0ec0 865 ua_sess->handle, ret);
4063050c 866 lttng_fd_put(LTTNG_FD_APPS, 2);
b551a063
DG
867 goto error;
868 }
869
5b4a0ec0 870 ua_chan->handle = ua_chan->obj->handle;
b551a063 871
5b4a0ec0 872 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
852d0037 873 ua_chan->name, app->pid, app->sock);
b551a063 874
840cb59c 875 health_code_update();
86acf0da 876
8535a6d9
DG
877 /* If channel is not enabled, disable it on the tracer */
878 if (!ua_chan->enabled) {
879 ret = disable_ust_channel(app, ua_sess, ua_chan);
880 if (ret < 0) {
881 goto error;
882 }
883 }
884
b551a063 885error:
840cb59c 886 health_code_update();
b551a063
DG
887 return ret;
888}
889
91d76f53 890/*
5b4a0ec0 891 * Create the specified event onto the UST tracer for a UST session.
91d76f53 892 */
edb67388
DG
893static
894int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
895 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 896{
5b4a0ec0 897 int ret = 0;
284d8f55 898
840cb59c 899 health_code_update();
86acf0da 900
5b4a0ec0 901 /* Create UST event on tracer */
852d0037 902 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0
DG
903 &ua_event->obj);
904 if (ret < 0) {
905 ERR("Error ustctl create event %s for app pid: %d with ret %d",
852d0037 906 ua_event->attr.name, app->pid, ret);
5b4a0ec0 907 goto error;
91d76f53 908 }
f6a9efaa 909
5b4a0ec0 910 ua_event->handle = ua_event->obj->handle;
284d8f55 911
5b4a0ec0 912 DBG2("UST app event %s created successfully for pid:%d",
852d0037 913 ua_event->attr.name, app->pid);
f6a9efaa 914
840cb59c 915 health_code_update();
86acf0da 916
025faf73
DG
917 /* Set filter if one is present. */
918 if (ua_event->filter) {
919 ret = set_ust_event_filter(ua_event, app);
920 if (ret < 0) {
921 goto error;
922 }
923 }
924
8535a6d9 925 /* If event not enabled, disable it on the tracer */
fc34caaa 926 if (ua_event->enabled == 0) {
8535a6d9
DG
927 ret = disable_ust_event(app, ua_sess, ua_event);
928 if (ret < 0) {
fc34caaa
DG
929 /*
930 * If we hit an EPERM, something is wrong with our disable call. If
931 * we get an EEXIST, there is a problem on the tracer side since we
932 * just created it.
933 */
934 switch (ret) {
49c336c1 935 case -LTTNG_UST_ERR_PERM:
fc34caaa
DG
936 /* Code flow problem */
937 assert(0);
49c336c1 938 case -LTTNG_UST_ERR_EXIST:
fc34caaa
DG
939 /* It's OK for our use case. */
940 ret = 0;
941 break;
942 default:
943 break;
944 }
8535a6d9
DG
945 goto error;
946 }
947 }
948
5b4a0ec0 949error:
840cb59c 950 health_code_update();
5b4a0ec0 951 return ret;
91d76f53 952}
48842b30 953
5b4a0ec0
DG
954/*
955 * Copy data between an UST app event and a LTT event.
956 */
421cb601 957static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
958 struct ltt_ust_event *uevent)
959{
960 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
961 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
962
fc34caaa
DG
963 ua_event->enabled = uevent->enabled;
964
5b4a0ec0
DG
965 /* Copy event attributes */
966 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
967
53a80697
MD
968 /* Copy filter bytecode */
969 if (uevent->filter) {
025faf73
DG
970 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
971 /* Filter might be NULL here in case of ENONEM. */
53a80697 972 }
48842b30
DG
973}
974
5b4a0ec0
DG
975/*
976 * Copy data between an UST app channel and a LTT channel.
977 */
421cb601 978static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
979 struct ltt_ust_channel *uchan)
980{
bec39940 981 struct lttng_ht_iter iter;
48842b30 982 struct ltt_ust_event *uevent;
55cc08a6 983 struct ltt_ust_context *uctx;
48842b30 984 struct ust_app_event *ua_event;
55cc08a6 985 struct ust_app_ctx *ua_ctx;
48842b30 986
fc34caaa 987 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
988
989 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
990 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
5b4a0ec0
DG
991 /* Copy event attributes */
992 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
48842b30 993
fc34caaa
DG
994 ua_chan->enabled = uchan->enabled;
995
bec39940 996 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
55cc08a6
DG
997 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
998 if (ua_ctx == NULL) {
999 continue;
1000 }
bec39940
DG
1001 lttng_ht_node_init_ulong(&ua_ctx->node,
1002 (unsigned long) ua_ctx->ctx.ctx);
1003 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6 1004 }
48842b30 1005
421cb601 1006 /* Copy all events from ltt ust channel to ust app channel */
bec39940 1007 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
18eace3b
DG
1008 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1009 uevent->filter, uevent->attr.loglevel);
1010 if (ua_event == NULL) {
421cb601 1011 DBG2("UST event %s not found on shadow copy channel",
48842b30 1012 uevent->attr.name);
284d8f55 1013 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 1014 if (ua_event == NULL) {
5b4a0ec0 1015 continue;
48842b30 1016 }
421cb601 1017 shadow_copy_event(ua_event, uevent);
18eace3b 1018 add_unique_ust_app_event(ua_chan->events, ua_event);
48842b30 1019 }
48842b30
DG
1020 }
1021
fc34caaa 1022 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1023}
1024
5b4a0ec0
DG
1025/*
1026 * Copy data between a UST app session and a regular LTT session.
1027 */
421cb601 1028static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1029 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1030{
bec39940
DG
1031 struct lttng_ht_node_str *ua_chan_node;
1032 struct lttng_ht_iter iter;
48842b30
DG
1033 struct ltt_ust_channel *uchan;
1034 struct ust_app_channel *ua_chan;
477d7741
MD
1035 time_t rawtime;
1036 struct tm *timeinfo;
1037 char datetime[16];
1038 int ret;
1039
1040 /* Get date and time for unique app path */
1041 time(&rawtime);
1042 timeinfo = localtime(&rawtime);
1043 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1044
421cb601 1045 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1046
a991f516 1047 ua_sess->id = usess->id;
6df2e2c9
MD
1048 ua_sess->uid = usess->uid;
1049 ua_sess->gid = usess->gid;
48842b30 1050
00e2e675
DG
1051 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
1052 datetime);
477d7741
MD
1053 if (ret < 0) {
1054 PERROR("asprintf UST shadow copy session");
1055 /* TODO: We cannot return an error from here.. */
1056 assert(0);
1057 }
1058
48842b30
DG
1059 /* TODO: support all UST domain */
1060
1061 /* Iterate over all channels in global domain. */
bec39940
DG
1062 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1063 uchan, node.node) {
1064 struct lttng_ht_iter uiter;
ba767faf 1065
bec39940
DG
1066 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1067 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0 1068 if (ua_chan_node != NULL) {
fc34caaa 1069 /* Session exist. Contiuing. */
5b4a0ec0
DG
1070 continue;
1071 }
421cb601 1072
5b4a0ec0
DG
1073 DBG2("Channel %s not found on shadow session copy, creating it",
1074 uchan->name);
1075 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1076 if (ua_chan == NULL) {
fc34caaa 1077 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
5b4a0ec0 1078 continue;
48842b30
DG
1079 }
1080
5b4a0ec0 1081 shadow_copy_channel(ua_chan, uchan);
bec39940 1082 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30
DG
1083 }
1084}
1085
78f0bacd
DG
1086/*
1087 * Lookup sesison wrapper.
1088 */
84cd17c6
MD
1089static
1090void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 1091 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1092{
1093 /* Get right UST app session from app */
2c348c10 1094 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
84cd17c6
MD
1095}
1096
421cb601
DG
1097/*
1098 * Return ust app session from the app session hashtable using the UST session
a991f516 1099 * id.
421cb601 1100 */
48842b30
DG
1101static struct ust_app_session *lookup_session_by_app(
1102 struct ltt_ust_session *usess, struct ust_app *app)
1103{
bec39940
DG
1104 struct lttng_ht_iter iter;
1105 struct lttng_ht_node_ulong *node;
48842b30 1106
84cd17c6 1107 __lookup_session_by_app(usess, app, &iter);
bec39940 1108 node = lttng_ht_iter_get_node_ulong(&iter);
48842b30
DG
1109 if (node == NULL) {
1110 goto error;
1111 }
1112
1113 return caa_container_of(node, struct ust_app_session, node);
1114
1115error:
1116 return NULL;
1117}
1118
421cb601 1119/*
3d8ca23b 1120 * Create a session on the tracer side for the given app.
421cb601 1121 *
3d8ca23b
DG
1122 * On success, ua_sess_ptr is populated with the session pointer or else left
1123 * untouched. If the session was created, is_created is set to 1. On error,
1124 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1125 * be NULL.
1126 *
1127 * Returns 0 on success or else a negative code which is either -ENOMEM or
1128 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 1129 */
3d8ca23b
DG
1130static int create_ust_app_session(struct ltt_ust_session *usess,
1131 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1132 int *is_created)
421cb601 1133{
3d8ca23b 1134 int ret, created = 0;
421cb601
DG
1135 struct ust_app_session *ua_sess;
1136
3d8ca23b
DG
1137 assert(usess);
1138 assert(app);
1139 assert(ua_sess_ptr);
1140
840cb59c 1141 health_code_update();
86acf0da 1142
421cb601
DG
1143 ua_sess = lookup_session_by_app(usess, app);
1144 if (ua_sess == NULL) {
a991f516 1145 DBG2("UST app pid: %d session id %d not found, creating it",
852d0037 1146 app->pid, usess->id);
421cb601
DG
1147 ua_sess = alloc_ust_app_session();
1148 if (ua_sess == NULL) {
1149 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
1150 ret = -ENOMEM;
1151 goto error;
421cb601 1152 }
477d7741 1153 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 1154 created = 1;
421cb601
DG
1155 }
1156
840cb59c 1157 health_code_update();
86acf0da 1158
421cb601 1159 if (ua_sess->handle == -1) {
852d0037 1160 ret = ustctl_create_session(app->sock);
421cb601 1161 if (ret < 0) {
852d0037 1162 ERR("Creating session for app pid %d", app->pid);
0f83395d 1163 delete_ust_app_session(-1, ua_sess);
3d8ca23b
DG
1164 if (ret != -ENOMEM) {
1165 /*
1166 * Tracer is probably gone or got an internal error so let's
1167 * behave like it will soon unregister or not usable.
1168 */
1169 ret = -ENOTCONN;
1170 }
1171 goto error;
421cb601
DG
1172 }
1173
421cb601
DG
1174 ua_sess->handle = ret;
1175
1176 /* Add ust app session to app's HT */
2c348c10 1177 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
bec39940 1178 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
421cb601
DG
1179
1180 DBG2("UST app session created successfully with handle %d", ret);
1181 }
1182
3d8ca23b
DG
1183 *ua_sess_ptr = ua_sess;
1184 if (is_created) {
1185 *is_created = created;
1186 }
1187 /* Everything went well. */
1188 ret = 0;
1189
1190error:
840cb59c 1191 health_code_update();
3d8ca23b 1192 return ret;
421cb601
DG
1193}
1194
55cc08a6
DG
1195/*
1196 * Create a context for the channel on the tracer.
1197 */
1198static
1199int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1200 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1201 struct ust_app *app)
1202{
1203 int ret = 0;
bec39940
DG
1204 struct lttng_ht_iter iter;
1205 struct lttng_ht_node_ulong *node;
55cc08a6
DG
1206 struct ust_app_ctx *ua_ctx;
1207
1208 DBG2("UST app adding context to channel %s", ua_chan->name);
1209
bec39940
DG
1210 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1211 node = lttng_ht_iter_get_node_ulong(&iter);
55cc08a6
DG
1212 if (node != NULL) {
1213 ret = -EEXIST;
1214 goto error;
1215 }
1216
1217 ua_ctx = alloc_ust_app_ctx(uctx);
1218 if (ua_ctx == NULL) {
1219 /* malloc failed */
1220 ret = -1;
1221 goto error;
1222 }
1223
bec39940
DG
1224 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1225 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
55cc08a6
DG
1226
1227 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1228 if (ret < 0) {
1229 goto error;
1230 }
1231
1232error:
1233 return ret;
1234}
1235
edb67388
DG
1236/*
1237 * Enable on the tracer side a ust app event for the session and channel.
1238 */
1239static
1240int enable_ust_app_event(struct ust_app_session *ua_sess,
35a9059d 1241 struct ust_app_event *ua_event, struct ust_app *app)
edb67388
DG
1242{
1243 int ret;
1244
1245 ret = enable_ust_event(app, ua_sess, ua_event);
1246 if (ret < 0) {
1247 goto error;
1248 }
1249
1250 ua_event->enabled = 1;
1251
1252error:
1253 return ret;
1254}
1255
9730260e
DG
1256/*
1257 * Disable on the tracer side a ust app event for the session and channel.
1258 */
1259static int disable_ust_app_event(struct ust_app_session *ua_sess,
7f79d3a1 1260 struct ust_app_event *ua_event, struct ust_app *app)
9730260e
DG
1261{
1262 int ret;
1263
1264 ret = disable_ust_event(app, ua_sess, ua_event);
1265 if (ret < 0) {
1266 goto error;
1267 }
1268
1269 ua_event->enabled = 0;
1270
1271error:
1272 return ret;
1273}
1274
78f0bacd
DG
1275/*
1276 * Lookup ust app channel for session and disable it on the tracer side.
1277 */
8535a6d9
DG
1278static
1279int disable_ust_app_channel(struct ust_app_session *ua_sess,
1280 struct ust_app_channel *ua_chan, struct ust_app *app)
78f0bacd 1281{
8535a6d9 1282 int ret;
78f0bacd
DG
1283
1284 ret = disable_ust_channel(app, ua_sess, ua_chan);
1285 if (ret < 0) {
1286 goto error;
1287 }
1288
8535a6d9
DG
1289 ua_chan->enabled = 0;
1290
78f0bacd
DG
1291error:
1292 return ret;
1293}
1294
1295/*
1296 * Lookup ust app channel for session and enable it on the tracer side.
1297 */
1298static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1299 struct ltt_ust_channel *uchan, struct ust_app *app)
1300{
1301 int ret = 0;
bec39940
DG
1302 struct lttng_ht_iter iter;
1303 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1304 struct ust_app_channel *ua_chan;
1305
bec39940
DG
1306 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1307 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
78f0bacd 1308 if (ua_chan_node == NULL) {
a991f516
MD
1309 DBG2("Unable to find channel %s in ust session id %u",
1310 uchan->name, ua_sess->id);
78f0bacd
DG
1311 goto error;
1312 }
1313
1314 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1315
1316 ret = enable_ust_channel(app, ua_sess, ua_chan);
1317 if (ret < 0) {
1318 goto error;
1319 }
1320
1321error:
1322 return ret;
1323}
1324
284d8f55 1325/*
4d710ac2
DG
1326 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1327 * newly created channel if not NULL.
284d8f55 1328 */
4d710ac2
DG
1329static int create_ust_app_channel(struct ust_app_session *ua_sess,
1330 struct ltt_ust_channel *uchan, struct ust_app *app,
1331 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
1332{
1333 int ret = 0;
bec39940
DG
1334 struct lttng_ht_iter iter;
1335 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
1336 struct ust_app_channel *ua_chan;
1337
1338 /* Lookup channel in the ust app session */
bec39940
DG
1339 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1340 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 1341 if (ua_chan_node != NULL) {
5b4a0ec0 1342 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 1343 goto end;
5b4a0ec0
DG
1344 }
1345
fc34caaa
DG
1346 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1347 if (ua_chan == NULL) {
1348 /* Only malloc can fail here */
4d710ac2 1349 ret = -ENOMEM;
fc34caaa
DG
1350 goto error;
1351 }
1352 shadow_copy_channel(ua_chan, uchan);
1353
5b4a0ec0
DG
1354 ret = create_ust_channel(app, ua_sess, ua_chan);
1355 if (ret < 0) {
fc34caaa 1356 /* Not found previously means that it does not exist on the tracer */
49c336c1 1357 assert(ret != -LTTNG_UST_ERR_EXIST);
5b4a0ec0
DG
1358 goto error;
1359 }
1360
4d710ac2 1361 /* Only add the channel if successful on the tracer side. */
58f3ca76
DG
1362 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1363
fc34caaa 1364 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
852d0037 1365 app->pid);
fc34caaa
DG
1366
1367end:
4d710ac2
DG
1368 if (ua_chanp) {
1369 *ua_chanp = ua_chan;
1370 }
1371
1372 /* Everything went well. */
1373 return 0;
5b4a0ec0
DG
1374
1375error:
fc34caaa 1376 delete_ust_app_channel(-1, ua_chan);
4d710ac2 1377 return ret;
5b4a0ec0
DG
1378}
1379
1380/*
1381 * Create UST app event and create it on the tracer side.
1382 */
edb67388
DG
1383static
1384int create_ust_app_event(struct ust_app_session *ua_sess,
1385 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1386 struct ust_app *app)
284d8f55 1387{
edb67388 1388 int ret = 0;
5b4a0ec0 1389 struct ust_app_event *ua_event;
284d8f55 1390
5b4a0ec0 1391 /* Get event node */
18eace3b
DG
1392 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1393 uevent->filter, uevent->attr.loglevel);
1394 if (ua_event != NULL) {
fc34caaa 1395 ret = -EEXIST;
edb67388
DG
1396 goto end;
1397 }
5b4a0ec0 1398
edb67388
DG
1399 /* Does not exist so create one */
1400 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1401 if (ua_event == NULL) {
1402 /* Only malloc can failed so something is really wrong */
1403 ret = -ENOMEM;
fc34caaa 1404 goto end;
5b4a0ec0 1405 }
edb67388 1406 shadow_copy_event(ua_event, uevent);
5b4a0ec0 1407
edb67388 1408 /* Create it on the tracer side */
5b4a0ec0 1409 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 1410 if (ret < 0) {
fc34caaa 1411 /* Not found previously means that it does not exist on the tracer */
76f66f63 1412 assert(ret != -LTTNG_UST_ERR_EXIST);
284d8f55
DG
1413 goto error;
1414 }
1415
18eace3b 1416 add_unique_ust_app_event(ua_chan->events, ua_event);
284d8f55 1417
fc34caaa 1418 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 1419 app->pid);
7f79d3a1 1420
edb67388 1421end:
fc34caaa
DG
1422 return ret;
1423
5b4a0ec0 1424error:
fc34caaa
DG
1425 /* Valid. Calling here is already in a read side lock */
1426 delete_ust_app_event(-1, ua_event);
edb67388 1427 return ret;
5b4a0ec0
DG
1428}
1429
1430/*
1431 * Create UST metadata and open it on the tracer side.
1432 */
1433static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1434 char *pathname, struct ust_app *app)
1435{
1436 int ret = 0;
1437
1438 if (ua_sess->metadata == NULL) {
1439 /* Allocate UST metadata */
1440 ua_sess->metadata = trace_ust_create_metadata(pathname);
1441 if (ua_sess->metadata == NULL) {
fc34caaa 1442 /* malloc() failed */
5b4a0ec0
DG
1443 goto error;
1444 }
1445
1446 ret = open_ust_metadata(app, ua_sess);
1447 if (ret < 0) {
7db205b5
DG
1448 DBG3("Opening metadata failed. Cleaning up memory");
1449
fc34caaa
DG
1450 /* Cleanup failed metadata struct */
1451 free(ua_sess->metadata);
7db205b5
DG
1452 /*
1453 * This is very important because delete_ust_app_session check if
1454 * the pointer is null or not in order to delete the metadata.
1455 */
1456 ua_sess->metadata = NULL;
5b4a0ec0
DG
1457 goto error;
1458 }
1459
852d0037 1460 DBG2("UST metadata opened for app pid %d", app->pid);
5b4a0ec0
DG
1461 }
1462
1463 /* Open UST metadata stream */
1464 if (ua_sess->metadata->stream_obj == NULL) {
5922e6c2 1465 ret = create_ust_metadata_stream(app, ua_sess);
5b4a0ec0
DG
1466 if (ret < 0) {
1467 goto error;
1468 }
1469
477d7741 1470 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
30079b6b 1471 "%s/" DEFAULT_METADATA_NAME, ua_sess->path);
5b4a0ec0
DG
1472 if (ret < 0) {
1473 PERROR("asprintf UST create stream");
1474 goto error;
1475 }
1476
1477 DBG2("UST metadata stream object created for app pid %d",
852d0037 1478 app->pid);
5b4a0ec0
DG
1479 } else {
1480 ERR("Attempting to create stream without metadata opened");
1481 goto error;
1482 }
1483
1484 return 0;
1485
1486error:
1487 return -1;
1488}
1489
1490/*
1491 * Return pointer to traceable apps list.
1492 */
bec39940 1493struct lttng_ht *ust_app_get_ht(void)
5b4a0ec0
DG
1494{
1495 return ust_app_ht;
1496}
1497
1498/*
1499 * Return ust app pointer or NULL if not found.
1500 */
1501struct ust_app *ust_app_find_by_pid(pid_t pid)
1502{
bec39940
DG
1503 struct lttng_ht_node_ulong *node;
1504 struct lttng_ht_iter iter;
5b4a0ec0
DG
1505
1506 rcu_read_lock();
bec39940
DG
1507 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1508 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
1509 if (node == NULL) {
1510 DBG2("UST app no found with pid %d", pid);
1511 goto error;
1512 }
1513 rcu_read_unlock();
1514
1515 DBG2("Found UST app by pid %d", pid);
1516
852d0037 1517 return caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
1518
1519error:
1520 rcu_read_unlock();
1521 return NULL;
1522}
1523
1524/*
1525 * Using pid and uid (of the app), allocate a new ust_app struct and
1526 * add it to the global traceable app list.
1527 *
0df502fd
MD
1528 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1529 * bitness is not supported.
5b4a0ec0
DG
1530 */
1531int ust_app_register(struct ust_register_msg *msg, int sock)
1532{
1533 struct ust_app *lta;
799e2c4f 1534 int ret;
5b4a0ec0 1535
173af62f
DG
1536 if ((msg->bits_per_long == 64 &&
1537 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1538 || (msg->bits_per_long == 32 &&
1539 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 1540 ERR("Registration failed: application \"%s\" (pid: %d) has "
7753dea8
MD
1541 "%d-bit long, but no consumerd for this long size is available.\n",
1542 msg->name, msg->pid, msg->bits_per_long);
799e2c4f
MD
1543 ret = close(sock);
1544 if (ret) {
1545 PERROR("close");
1546 }
4063050c 1547 lttng_fd_put(LTTNG_FD_APPS, 1);
0df502fd
MD
1548 return -EINVAL;
1549 }
3f2c5fcc
MD
1550 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1551 ERR("Registration failed: application \"%s\" (pid: %d) has "
1552 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1553 msg->name, msg->pid, msg->major, msg->minor);
799e2c4f
MD
1554 ret = close(sock);
1555 if (ret) {
1556 PERROR("close");
1557 }
4063050c 1558 lttng_fd_put(LTTNG_FD_APPS, 1);
3f2c5fcc
MD
1559 return -EINVAL;
1560 }
5b4a0ec0
DG
1561 lta = zmalloc(sizeof(struct ust_app));
1562 if (lta == NULL) {
1563 PERROR("malloc");
1564 return -ENOMEM;
1565 }
1566
1567 lta->ppid = msg->ppid;
1568 lta->uid = msg->uid;
1569 lta->gid = msg->gid;
e0c7ec2b 1570 lta->compatible = 0; /* Not compatible until proven */
7753dea8 1571 lta->bits_per_long = msg->bits_per_long;
5b4a0ec0
DG
1572 lta->v_major = msg->major;
1573 lta->v_minor = msg->minor;
1574 strncpy(lta->name, msg->name, sizeof(lta->name));
1575 lta->name[16] = '\0';
bec39940 1576 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
5b4a0ec0 1577
852d0037
DG
1578 lta->pid = msg->pid;
1579 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1580 lta->sock = sock;
1581 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
5b4a0ec0 1582
d42f20df
DG
1583 CDS_INIT_LIST_HEAD(&lta->teardown_head);
1584
5b4a0ec0 1585 rcu_read_lock();
852d0037
DG
1586
1587 /*
1588 * On a re-registration, we want to kick out the previous registration of
1589 * that pid
1590 */
1591 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1592
1593 /*
1594 * The socket _should_ be unique until _we_ call close. So, a add_unique
1595 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1596 * already in the table.
1597 */
1598 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1599
5b4a0ec0
DG
1600 rcu_read_unlock();
1601
1602 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
852d0037
DG
1603 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1604 lta->sock, lta->name, lta->v_major, lta->v_minor);
5b4a0ec0
DG
1605
1606 return 0;
1607}
1608
1609/*
1610 * Unregister app by removing it from the global traceable app list and freeing
1611 * the data struct.
1612 *
1613 * The socket is already closed at this point so no close to sock.
1614 */
1615void ust_app_unregister(int sock)
1616{
1617 struct ust_app *lta;
bec39940
DG
1618 struct lttng_ht_node_ulong *node;
1619 struct lttng_ht_iter iter;
d42f20df 1620 struct ust_app_session *ua_sess;
525b0740 1621 int ret;
5b4a0ec0
DG
1622
1623 rcu_read_lock();
886459c6 1624
5b4a0ec0 1625 /* Get the node reference for a call_rcu */
852d0037 1626 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1627 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0 1628 if (node == NULL) {
852d0037 1629 ERR("Unable to find app by sock %d", sock);
5b4a0ec0
DG
1630 goto error;
1631 }
284d8f55 1632
852d0037
DG
1633 lta = caa_container_of(node, struct ust_app, sock_n);
1634
1635 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1636
886459c6 1637 /* Remove application from PID hash table */
852d0037
DG
1638 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1639 assert(!ret);
1640
1641 /* Assign second node for deletion */
1642 iter.iter.node = &lta->pid_n.node;
1643
5b98a774
DG
1644 /*
1645 * Ignore return value since the node might have been removed before by an
1646 * add replace during app registration because the PID can be reassigned by
1647 * the OS.
1648 */
bec39940 1649 ret = lttng_ht_del(ust_app_ht, &iter);
5b98a774
DG
1650 if (ret) {
1651 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1652 lta->pid);
1653 }
852d0037 1654
d42f20df
DG
1655 /* Remove sessions so they are not visible during deletion.*/
1656 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
1657 node.node) {
1658 ret = lttng_ht_del(lta->sessions, &iter);
1659 if (ret) {
1660 /* The session was already removed so scheduled for teardown. */
1661 continue;
1662 }
1663
1664 /*
1665 * Add session to list for teardown. This is safe since at this point we
1666 * are the only one using this list.
1667 */
1668 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
1669 }
1670
852d0037
DG
1671 /* Free memory */
1672 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1673
284d8f55 1674error:
5b4a0ec0
DG
1675 rcu_read_unlock();
1676 return;
284d8f55
DG
1677}
1678
1679/*
5b4a0ec0 1680 * Return traceable_app_count
284d8f55 1681 */
5b4a0ec0 1682unsigned long ust_app_list_count(void)
284d8f55 1683{
5b4a0ec0 1684 unsigned long count;
284d8f55 1685
5b4a0ec0 1686 rcu_read_lock();
bec39940 1687 count = lttng_ht_get_count(ust_app_ht);
5b4a0ec0 1688 rcu_read_unlock();
284d8f55 1689
5b4a0ec0 1690 return count;
284d8f55
DG
1691}
1692
5b4a0ec0
DG
1693/*
1694 * Fill events array with all events name of all registered apps.
1695 */
1696int ust_app_list_events(struct lttng_event **events)
421cb601 1697{
5b4a0ec0
DG
1698 int ret, handle;
1699 size_t nbmem, count = 0;
bec39940 1700 struct lttng_ht_iter iter;
5b4a0ec0 1701 struct ust_app *app;
c617c0c6 1702 struct lttng_event *tmp_event;
421cb601 1703
5b4a0ec0 1704 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
1705 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
1706 if (tmp_event == NULL) {
5b4a0ec0
DG
1707 PERROR("zmalloc ust app events");
1708 ret = -ENOMEM;
421cb601
DG
1709 goto error;
1710 }
1711
5b4a0ec0 1712 rcu_read_lock();
421cb601 1713
852d0037 1714 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 1715 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 1716
840cb59c 1717 health_code_update();
86acf0da 1718
e0c7ec2b
DG
1719 if (!app->compatible) {
1720 /*
1721 * TODO: In time, we should notice the caller of this error by
1722 * telling him that this is a version error.
1723 */
1724 continue;
1725 }
852d0037 1726 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0
DG
1727 if (handle < 0) {
1728 ERR("UST app list events getting handle failed for app pid %d",
852d0037 1729 app->pid);
5b4a0ec0
DG
1730 continue;
1731 }
421cb601 1732
852d0037 1733 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 1734 &uiter)) != -LTTNG_UST_ERR_NOENT) {
840cb59c 1735 health_code_update();
815564d8 1736 if (count >= nbmem) {
d7b3776f 1737 /* In case the realloc fails, we free the memory */
c617c0c6
MD
1738 void *ptr;
1739
815564d8
MD
1740 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1741 2 * nbmem);
1742 nbmem *= 2;
c617c0c6
MD
1743 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event));
1744 if (ptr == NULL) {
5b4a0ec0 1745 PERROR("realloc ust app events");
c617c0c6 1746 free(tmp_event);
5b4a0ec0
DG
1747 ret = -ENOMEM;
1748 goto rcu_error;
1749 }
c617c0c6 1750 tmp_event = ptr;
5b4a0ec0 1751 }
c617c0c6
MD
1752 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1753 tmp_event[count].loglevel = uiter.loglevel;
1754 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1755 tmp_event[count].pid = app->pid;
1756 tmp_event[count].enabled = -1;
5b4a0ec0 1757 count++;
421cb601 1758 }
421cb601
DG
1759 }
1760
5b4a0ec0 1761 ret = count;
c617c0c6 1762 *events = tmp_event;
421cb601 1763
5b4a0ec0 1764 DBG2("UST app list events done (%zu events)", count);
421cb601 1765
5b4a0ec0
DG
1766rcu_error:
1767 rcu_read_unlock();
421cb601 1768error:
840cb59c 1769 health_code_update();
5b4a0ec0 1770 return ret;
421cb601
DG
1771}
1772
f37d259d
MD
1773/*
1774 * Fill events array with all events name of all registered apps.
1775 */
1776int ust_app_list_event_fields(struct lttng_event_field **fields)
1777{
1778 int ret, handle;
1779 size_t nbmem, count = 0;
1780 struct lttng_ht_iter iter;
1781 struct ust_app *app;
c617c0c6 1782 struct lttng_event_field *tmp_event;
f37d259d
MD
1783
1784 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
1785 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
1786 if (tmp_event == NULL) {
f37d259d
MD
1787 PERROR("zmalloc ust app event fields");
1788 ret = -ENOMEM;
1789 goto error;
1790 }
1791
1792 rcu_read_lock();
1793
1794 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1795 struct lttng_ust_field_iter uiter;
1796
840cb59c 1797 health_code_update();
86acf0da 1798
f37d259d
MD
1799 if (!app->compatible) {
1800 /*
1801 * TODO: In time, we should notice the caller of this error by
1802 * telling him that this is a version error.
1803 */
1804 continue;
1805 }
1806 handle = ustctl_tracepoint_field_list(app->sock);
1807 if (handle < 0) {
1808 ERR("UST app list event fields getting handle failed for app pid %d",
1809 app->pid);
1810 continue;
1811 }
1812
1813 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 1814 &uiter)) != -LTTNG_UST_ERR_NOENT) {
840cb59c 1815 health_code_update();
f37d259d 1816 if (count >= nbmem) {
d7b3776f 1817 /* In case the realloc fails, we free the memory */
c617c0c6
MD
1818 void *ptr;
1819
f37d259d
MD
1820 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
1821 2 * nbmem);
1822 nbmem *= 2;
c617c0c6
MD
1823 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field));
1824 if (ptr == NULL) {
f37d259d 1825 PERROR("realloc ust app event fields");
c617c0c6 1826 free(tmp_event);
f37d259d
MD
1827 ret = -ENOMEM;
1828 goto rcu_error;
1829 }
c617c0c6 1830 tmp_event = ptr;
f37d259d 1831 }
f37d259d 1832
c617c0c6
MD
1833 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
1834 tmp_event[count].type = uiter.type;
1835 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 1836
c617c0c6
MD
1837 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
1838 tmp_event[count].event.loglevel = uiter.loglevel;
1839 tmp_event[count].event.type = LTTNG_UST_TRACEPOINT;
1840 tmp_event[count].event.pid = app->pid;
1841 tmp_event[count].event.enabled = -1;
f37d259d
MD
1842 count++;
1843 }
1844 }
1845
1846 ret = count;
c617c0c6 1847 *fields = tmp_event;
f37d259d
MD
1848
1849 DBG2("UST app list event fields done (%zu events)", count);
1850
1851rcu_error:
1852 rcu_read_unlock();
1853error:
840cb59c 1854 health_code_update();
f37d259d
MD
1855 return ret;
1856}
1857
5b4a0ec0
DG
1858/*
1859 * Free and clean all traceable apps of the global list.
1860 */
1861void ust_app_clean_list(void)
421cb601 1862{
5b4a0ec0 1863 int ret;
659ed79f 1864 struct ust_app *app;
bec39940 1865 struct lttng_ht_iter iter;
421cb601 1866
5b4a0ec0 1867 DBG2("UST app cleaning registered apps hash table");
421cb601 1868
5b4a0ec0 1869 rcu_read_lock();
421cb601 1870
659ed79f 1871 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 1872 ret = lttng_ht_del(ust_app_ht, &iter);
525b0740 1873 assert(!ret);
659ed79f 1874 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
421cb601
DG
1875 }
1876
852d0037 1877 /* Cleanup socket hash table */
659ed79f
DG
1878 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
1879 sock_n.node) {
852d0037 1880 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
bec39940
DG
1881 assert(!ret);
1882 }
852d0037 1883
bec39940 1884 /* Destroy is done only when the ht is empty */
852d0037
DG
1885 lttng_ht_destroy(ust_app_ht);
1886 lttng_ht_destroy(ust_app_ht_by_sock);
421cb601 1887
5b4a0ec0
DG
1888 rcu_read_unlock();
1889}
1890
1891/*
1892 * Init UST app hash table.
1893 */
1894void ust_app_ht_alloc(void)
1895{
bec39940 1896 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
852d0037 1897 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
421cb601
DG
1898}
1899
78f0bacd
DG
1900/*
1901 * For a specific UST session, disable the channel for all registered apps.
1902 */
35a9059d 1903int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1904 struct ltt_ust_channel *uchan)
1905{
1906 int ret = 0;
bec39940
DG
1907 struct lttng_ht_iter iter;
1908 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
1909 struct ust_app *app;
1910 struct ust_app_session *ua_sess;
8535a6d9 1911 struct ust_app_channel *ua_chan;
78f0bacd
DG
1912
1913 if (usess == NULL || uchan == NULL) {
1914 ERR("Disabling UST global channel with NULL values");
1915 ret = -1;
1916 goto error;
1917 }
1918
a991f516
MD
1919 DBG2("UST app disabling channel %s from global domain for session id %d",
1920 uchan->name, usess->id);
78f0bacd
DG
1921
1922 rcu_read_lock();
1923
1924 /* For every registered applications */
852d0037 1925 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 1926 struct lttng_ht_iter uiter;
e0c7ec2b
DG
1927 if (!app->compatible) {
1928 /*
1929 * TODO: In time, we should notice the caller of this error by
1930 * telling him that this is a version error.
1931 */
1932 continue;
1933 }
78f0bacd
DG
1934 ua_sess = lookup_session_by_app(usess, app);
1935 if (ua_sess == NULL) {
1936 continue;
1937 }
1938
8535a6d9 1939 /* Get channel */
bec39940
DG
1940 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1941 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
1942 /* If the session if found for the app, the channel must be there */
1943 assert(ua_chan_node);
1944
1945 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1946 /* The channel must not be already disabled */
1947 assert(ua_chan->enabled == 1);
1948
1949 /* Disable channel onto application */
1950 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
1951 if (ret < 0) {
1952 /* XXX: We might want to report this error at some point... */
1953 continue;
1954 }
1955 }
1956
1957 rcu_read_unlock();
1958
1959error:
1960 return ret;
1961}
1962
1963/*
1964 * For a specific UST session, enable the channel for all registered apps.
1965 */
35a9059d 1966int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
1967 struct ltt_ust_channel *uchan)
1968{
1969 int ret = 0;
bec39940 1970 struct lttng_ht_iter iter;
78f0bacd
DG
1971 struct ust_app *app;
1972 struct ust_app_session *ua_sess;
1973
1974 if (usess == NULL || uchan == NULL) {
1975 ERR("Adding UST global channel to NULL values");
1976 ret = -1;
1977 goto error;
1978 }
1979
a991f516
MD
1980 DBG2("UST app enabling channel %s to global domain for session id %d",
1981 uchan->name, usess->id);
78f0bacd
DG
1982
1983 rcu_read_lock();
1984
1985 /* For every registered applications */
852d0037 1986 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
1987 if (!app->compatible) {
1988 /*
1989 * TODO: In time, we should notice the caller of this error by
1990 * telling him that this is a version error.
1991 */
1992 continue;
1993 }
78f0bacd
DG
1994 ua_sess = lookup_session_by_app(usess, app);
1995 if (ua_sess == NULL) {
1996 continue;
1997 }
1998
1999 /* Enable channel onto application */
2000 ret = enable_ust_app_channel(ua_sess, uchan, app);
2001 if (ret < 0) {
2002 /* XXX: We might want to report this error at some point... */
2003 continue;
2004 }
2005 }
2006
2007 rcu_read_unlock();
2008
2009error:
2010 return ret;
2011}
2012
b0a40d28
DG
2013/*
2014 * Disable an event in a channel and for a specific session.
2015 */
35a9059d
DG
2016int ust_app_disable_event_glb(struct ltt_ust_session *usess,
2017 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
2018{
2019 int ret = 0;
bec39940
DG
2020 struct lttng_ht_iter iter, uiter;
2021 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
b0a40d28
DG
2022 struct ust_app *app;
2023 struct ust_app_session *ua_sess;
2024 struct ust_app_channel *ua_chan;
2025 struct ust_app_event *ua_event;
2026
2027 DBG("UST app disabling event %s for all apps in channel "
a991f516 2028 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
2029
2030 rcu_read_lock();
2031
2032 /* For all registered applications */
852d0037 2033 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2034 if (!app->compatible) {
2035 /*
2036 * TODO: In time, we should notice the caller of this error by
2037 * telling him that this is a version error.
2038 */
2039 continue;
2040 }
b0a40d28
DG
2041 ua_sess = lookup_session_by_app(usess, app);
2042 if (ua_sess == NULL) {
2043 /* Next app */
2044 continue;
2045 }
2046
2047 /* Lookup channel in the ust app session */
bec39940
DG
2048 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2049 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 2050 if (ua_chan_node == NULL) {
a991f516 2051 DBG2("Channel %s not found in session id %d for app pid %d."
852d0037 2052 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
2053 continue;
2054 }
2055 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2056
bec39940
DG
2057 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2058 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28
DG
2059 if (ua_event_node == NULL) {
2060 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 2061 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
2062 continue;
2063 }
2064 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2065
7f79d3a1 2066 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
2067 if (ret < 0) {
2068 /* XXX: Report error someday... */
2069 continue;
2070 }
2071 }
2072
2073 rcu_read_unlock();
2074
2075 return ret;
2076}
2077
9730260e 2078/*
edb67388 2079 * For a specific UST session and UST channel, the event for all
9730260e
DG
2080 * registered apps.
2081 */
35a9059d 2082int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
9730260e
DG
2083 struct ltt_ust_channel *uchan)
2084{
2085 int ret = 0;
bec39940
DG
2086 struct lttng_ht_iter iter, uiter;
2087 struct lttng_ht_node_str *ua_chan_node;
9730260e
DG
2088 struct ust_app *app;
2089 struct ust_app_session *ua_sess;
2090 struct ust_app_channel *ua_chan;
2091 struct ust_app_event *ua_event;
2092
2093 DBG("UST app disabling all event for all apps in channel "
a991f516 2094 "%s for session id %d", uchan->name, usess->id);
9730260e
DG
2095
2096 rcu_read_lock();
2097
2098 /* For all registered applications */
852d0037 2099 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2100 if (!app->compatible) {
2101 /*
2102 * TODO: In time, we should notice the caller of this error by
2103 * telling him that this is a version error.
2104 */
2105 continue;
2106 }
9730260e 2107 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2108 if (!ua_sess) {
2109 /* The application has problem or is probably dead. */
2110 continue;
2111 }
9730260e
DG
2112
2113 /* Lookup channel in the ust app session */
bec39940
DG
2114 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2115 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2116 /* If the channel is not found, there is a code flow error */
2117 assert(ua_chan_node);
2118
9730260e
DG
2119 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2120
2121 /* Disable each events of channel */
bec39940
DG
2122 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2123 node.node) {
7f79d3a1 2124 ret = disable_ust_app_event(ua_sess, ua_event, app);
9730260e
DG
2125 if (ret < 0) {
2126 /* XXX: Report error someday... */
2127 continue;
2128 }
2129 }
2130 }
2131
2132 rcu_read_unlock();
2133
2134 return ret;
2135}
2136
421cb601 2137/*
5b4a0ec0 2138 * For a specific UST session, create the channel for all registered apps.
421cb601 2139 */
35a9059d 2140int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
2141 struct ltt_ust_channel *uchan)
2142{
3d8ca23b 2143 int ret = 0, created;
bec39940 2144 struct lttng_ht_iter iter;
48842b30 2145 struct ust_app *app;
3d8ca23b 2146 struct ust_app_session *ua_sess = NULL;
48842b30 2147
fc34caaa
DG
2148 /* Very wrong code flow */
2149 assert(usess);
2150 assert(uchan);
421cb601 2151
a991f516
MD
2152 DBG2("UST app adding channel %s to global domain for session id %d",
2153 uchan->name, usess->id);
48842b30
DG
2154
2155 rcu_read_lock();
421cb601 2156
5b4a0ec0 2157 /* For every registered applications */
852d0037 2158 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2159 if (!app->compatible) {
2160 /*
2161 * TODO: In time, we should notice the caller of this error by
2162 * telling him that this is a version error.
2163 */
2164 continue;
2165 }
edb67388
DG
2166 /*
2167 * Create session on the tracer side and add it to app session HT. Note
2168 * that if session exist, it will simply return a pointer to the ust
2169 * app session.
2170 */
3d8ca23b
DG
2171 ret = create_ust_app_session(usess, app, &ua_sess, &created);
2172 if (ret < 0) {
2173 switch (ret) {
2174 case -ENOTCONN:
2175 /*
2176 * The application's socket is not valid. Either a bad socket
2177 * or a timeout on it. We can't inform the caller that for a
2178 * specific app, the session failed so lets continue here.
2179 */
2180 continue;
2181 case -ENOMEM:
2182 default:
2183 goto error_rcu_unlock;
2184 }
48842b30 2185 }
3d8ca23b 2186 assert(ua_sess);
48842b30 2187
4d710ac2
DG
2188 /* Create channel onto application. We don't need the chan ref. */
2189 ret = create_ust_app_channel(ua_sess, uchan, app, NULL);
3d8ca23b
DG
2190 if (ret < 0) {
2191 if (ret == -ENOMEM) {
2192 /* No more memory is a fatal error. Stop right now. */
2193 goto error_rcu_unlock;
2194 }
2195 /* Cleanup the created session if it's the case. */
2196 if (created) {
2197 delete_ust_app_session(app->sock, ua_sess);
2198 }
48842b30 2199 }
48842b30 2200 }
5b4a0ec0 2201
95e047ff 2202error_rcu_unlock:
48842b30 2203 rcu_read_unlock();
3c14c33f 2204 return ret;
48842b30
DG
2205}
2206
5b4a0ec0 2207/*
edb67388 2208 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 2209 */
35a9059d 2210int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
2211 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2212{
2213 int ret = 0;
bec39940 2214 struct lttng_ht_iter iter, uiter;
18eace3b 2215 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
2216 struct ust_app *app;
2217 struct ust_app_session *ua_sess;
2218 struct ust_app_channel *ua_chan;
2219 struct ust_app_event *ua_event;
48842b30 2220
a991f516
MD
2221 DBG("UST app enabling event %s for all apps for session id %d",
2222 uevent->attr.name, usess->id);
48842b30 2223
edb67388
DG
2224 /*
2225 * NOTE: At this point, this function is called only if the session and
2226 * channel passed are already created for all apps. and enabled on the
2227 * tracer also.
2228 */
2229
48842b30 2230 rcu_read_lock();
421cb601
DG
2231
2232 /* For all registered applications */
852d0037 2233 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2234 if (!app->compatible) {
2235 /*
2236 * TODO: In time, we should notice the caller of this error by
2237 * telling him that this is a version error.
2238 */
2239 continue;
2240 }
edb67388 2241 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2242 if (!ua_sess) {
2243 /* The application has problem or is probably dead. */
2244 continue;
2245 }
ba767faf 2246
edb67388 2247 /* Lookup channel in the ust app session */
bec39940
DG
2248 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2249 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2250 /* If the channel is not found, there is a code flow error */
2251 assert(ua_chan_node);
2252
2253 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2254
18eace3b
DG
2255 /* Get event node */
2256 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2257 uevent->filter, uevent->attr.loglevel);
2258 if (ua_event == NULL) {
7f79d3a1 2259 DBG3("UST app enable event %s not found for app PID %d."
852d0037 2260 "Skipping app", uevent->attr.name, app->pid);
35a9059d
DG
2261 continue;
2262 }
35a9059d
DG
2263
2264 ret = enable_ust_app_event(ua_sess, ua_event, app);
2265 if (ret < 0) {
7f79d3a1 2266 goto error;
48842b30 2267 }
edb67388
DG
2268 }
2269
7f79d3a1 2270error:
edb67388 2271 rcu_read_unlock();
edb67388
DG
2272 return ret;
2273}
2274
2275/*
2276 * For a specific existing UST session and UST channel, creates the event for
2277 * all registered apps.
2278 */
35a9059d 2279int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
2280 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2281{
2282 int ret = 0;
bec39940
DG
2283 struct lttng_ht_iter iter, uiter;
2284 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
2285 struct ust_app *app;
2286 struct ust_app_session *ua_sess;
2287 struct ust_app_channel *ua_chan;
2288
a991f516
MD
2289 DBG("UST app creating event %s for all apps for session id %d",
2290 uevent->attr.name, usess->id);
edb67388 2291
edb67388
DG
2292 rcu_read_lock();
2293
2294 /* For all registered applications */
852d0037 2295 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2296 if (!app->compatible) {
2297 /*
2298 * TODO: In time, we should notice the caller of this error by
2299 * telling him that this is a version error.
2300 */
2301 continue;
2302 }
edb67388 2303 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2304 if (!ua_sess) {
2305 /* The application has problem or is probably dead. */
2306 continue;
2307 }
48842b30
DG
2308
2309 /* Lookup channel in the ust app session */
bec39940
DG
2310 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2311 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
2312 /* If the channel is not found, there is a code flow error */
2313 assert(ua_chan_node);
2314
48842b30
DG
2315 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2316
edb67388
DG
2317 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2318 if (ret < 0) {
49c336c1 2319 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
2320 /* Possible value at this point: -ENOMEM. If so, we stop! */
2321 break;
2322 }
2323 DBG2("UST app event %s already exist on app PID %d",
852d0037 2324 uevent->attr.name, app->pid);
5b4a0ec0 2325 continue;
48842b30 2326 }
48842b30 2327 }
5b4a0ec0 2328
48842b30
DG
2329 rcu_read_unlock();
2330
2331 return ret;
2332}
2333
5b4a0ec0
DG
2334/*
2335 * Start tracing for a specific UST session and app.
2336 */
421cb601 2337int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
2338{
2339 int ret = 0;
bec39940 2340 struct lttng_ht_iter iter;
48842b30
DG
2341 struct ust_app_session *ua_sess;
2342 struct ust_app_channel *ua_chan;
030a66fa 2343 struct ust_app_stream *ustream;
173af62f 2344 struct consumer_socket *socket;
48842b30 2345
852d0037 2346 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 2347
509cbaf8
MD
2348 rcu_read_lock();
2349
e0c7ec2b
DG
2350 if (!app->compatible) {
2351 goto end;
2352 }
2353
421cb601
DG
2354 ua_sess = lookup_session_by_app(usess, app);
2355 if (ua_sess == NULL) {
d42f20df
DG
2356 /* The session is in teardown process. Ignore and continue. */
2357 goto end;
421cb601 2358 }
48842b30 2359
aea829b3
DG
2360 /* Upon restart, we skip the setup, already done */
2361 if (ua_sess->started) {
8be98f9a 2362 goto skip_setup;
aea829b3 2363 }
8be98f9a 2364
a4b92340
DG
2365 /* Create directories if consumer is LOCAL and has a path defined. */
2366 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2367 strlen(usess->consumer->dst.trace_path) > 0) {
2368 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2369 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2370 if (ret < 0) {
2371 if (ret != -EEXIST) {
2372 ERR("Trace directory creation error");
2373 ret = -1;
2374 goto error_rcu_unlock;
2375 }
2376 }
2377 }
2378
421cb601
DG
2379 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
2380 if (ret < 0) {
f73fabfd 2381 ret = LTTNG_ERR_UST_META_FAIL;
509cbaf8 2382 goto error_rcu_unlock;
421cb601 2383 }
48842b30 2384
421cb601 2385 /* For each channel */
bec39940
DG
2386 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2387 node.node) {
421cb601
DG
2388 /* Create all streams */
2389 while (1) {
5b4a0ec0 2390 /* Create UST stream */
37f1c236 2391 ustream = alloc_ust_app_stream();
421cb601 2392 if (ustream == NULL) {
509cbaf8 2393 goto error_rcu_unlock;
421cb601 2394 }
48842b30 2395
840cb59c 2396 health_code_update();
86acf0da 2397
4f3ab6ee 2398 ret = create_ust_stream(app, ua_chan, ustream);
48842b30 2399 if (ret < 0) {
4f3ab6ee 2400 /* Free unused memory after this point. */
a2c0da86 2401 free(ustream);
495bbffb 2402 if (ret == -LTTNG_UST_ERR_NOENT) {
5548a381
DG
2403 /* Got all streams. Continue normal execution. */
2404 break;
2405 }
2406 /* Error at this point. Stop everything. */
f73fabfd 2407 ret = LTTNG_ERR_UST_STREAM_FAIL;
5548a381 2408 goto error_rcu_unlock;
48842b30
DG
2409 }
2410
840cb59c 2411 health_code_update();
86acf0da 2412
7f133705 2413 /* Order is important this is why a list is used. */
421cb601 2414 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
c30aaa51 2415 ua_chan->streams.count++;
7f133705 2416
00e2e675
DG
2417 DBG2("UST stream %d ready (handle: %d)", ua_chan->streams.count,
2418 ustream->handle);
421cb601 2419 }
86acf0da 2420
840cb59c 2421 health_code_update();
421cb601 2422 }
aba8e916 2423
7753dea8
MD
2424 switch (app->bits_per_long) {
2425 case 64:
173af62f
DG
2426 socket = consumer_find_socket(uatomic_read(&ust_consumerd64_fd),
2427 usess->consumer);
2428 if (socket == NULL) {
2429 goto skip_setup;
2430 }
7753dea8
MD
2431 break;
2432 case 32:
173af62f
DG
2433 socket = consumer_find_socket(uatomic_read(&ust_consumerd32_fd),
2434 usess->consumer);
2435 if (socket == NULL) {
2436 goto skip_setup;
2437 }
7753dea8
MD
2438 break;
2439 default:
2440 ret = -EINVAL;
2441 goto error_rcu_unlock;
2442 }
aea829b3 2443
421cb601 2444 /* Setup UST consumer socket and send fds to it */
173af62f 2445 ret = ust_consumer_send_session(ua_sess, usess->consumer, socket);
421cb601 2446 if (ret < 0) {
509cbaf8 2447 goto error_rcu_unlock;
421cb601 2448 }
48842b30 2449
840cb59c 2450 health_code_update();
86acf0da 2451
8be98f9a 2452skip_setup:
421cb601 2453 /* This start the UST tracing */
852d0037 2454 ret = ustctl_start_session(app->sock, ua_sess->handle);
421cb601 2455 if (ret < 0) {
5c3c99d7 2456 ERR("Error starting tracing for app pid: %d (ret: %d)", app->pid, ret);
509cbaf8 2457 goto error_rcu_unlock;
421cb601 2458 }
5b4a0ec0 2459
55c3953d
DG
2460 /* Indicate that the session has been started once */
2461 ua_sess->started = 1;
2462
840cb59c 2463 health_code_update();
86acf0da 2464
421cb601 2465 /* Quiescent wait after starting trace */
852d0037 2466 ustctl_wait_quiescent(app->sock);
48842b30 2467
e0c7ec2b
DG
2468end:
2469 rcu_read_unlock();
840cb59c 2470 health_code_update();
421cb601 2471 return 0;
48842b30 2472
509cbaf8
MD
2473error_rcu_unlock:
2474 rcu_read_unlock();
840cb59c 2475 health_code_update();
421cb601
DG
2476 return -1;
2477}
48842b30 2478
8be98f9a
MD
2479/*
2480 * Stop tracing for a specific UST session and app.
2481 */
2482int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2483{
2484 int ret = 0;
bec39940 2485 struct lttng_ht_iter iter;
8be98f9a 2486 struct ust_app_session *ua_sess;
6d3686da 2487 struct ust_app_channel *ua_chan;
8be98f9a 2488
852d0037 2489 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
2490
2491 rcu_read_lock();
2492
e0c7ec2b
DG
2493 if (!app->compatible) {
2494 goto end;
2495 }
2496
8be98f9a
MD
2497 ua_sess = lookup_session_by_app(usess, app);
2498 if (ua_sess == NULL) {
d42f20df 2499 goto end;
8be98f9a
MD
2500 }
2501
9bc07046
DG
2502 /*
2503 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
2504 * that was never started. It's possible since we can have a fail start
2505 * from either the application manager thread or the command thread. Simply
2506 * indicate that this is a stop error.
9bc07046 2507 */
f9dfc3d9 2508 if (!ua_sess->started) {
c45536e1
DG
2509 goto error_rcu_unlock;
2510 }
7db205b5 2511
840cb59c 2512 health_code_update();
86acf0da 2513
9d6c7d3f 2514 /* This inhibits UST tracing */
852d0037 2515 ret = ustctl_stop_session(app->sock, ua_sess->handle);
9d6c7d3f 2516 if (ret < 0) {
5c3c99d7 2517 ERR("Error stopping tracing for app pid: %d (ret: %d)", app->pid, ret);
9d6c7d3f
DG
2518 goto error_rcu_unlock;
2519 }
2520
840cb59c 2521 health_code_update();
86acf0da 2522
9d6c7d3f 2523 /* Quiescent wait after stopping trace */
852d0037 2524 ustctl_wait_quiescent(app->sock);
9d6c7d3f 2525
840cb59c 2526 health_code_update();
86acf0da 2527
9d6c7d3f 2528 /* Flushing buffers */
bec39940
DG
2529 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2530 node.node) {
840cb59c 2531 health_code_update();
852d0037 2532 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
8be98f9a 2533 if (ret < 0) {
7db205b5 2534 ERR("UST app PID %d channel %s flush failed with ret %d",
852d0037 2535 app->pid, ua_chan->name, ret);
6d3686da
DG
2536 /* Continuing flushing all buffers */
2537 continue;
8be98f9a
MD
2538 }
2539 }
8be98f9a 2540
840cb59c 2541 health_code_update();
86acf0da 2542
90d97d10 2543 /* Flush all buffers before stopping */
852d0037 2544 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
90d97d10 2545 if (ret < 0) {
852d0037 2546 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
7db205b5 2547 ret);
90d97d10
DG
2548 }
2549
7db205b5
DG
2550end:
2551 rcu_read_unlock();
840cb59c 2552 health_code_update();
8be98f9a
MD
2553 return 0;
2554
2555error_rcu_unlock:
2556 rcu_read_unlock();
840cb59c 2557 health_code_update();
8be98f9a
MD
2558 return -1;
2559}
2560
84cd17c6
MD
2561/*
2562 * Destroy a specific UST session in apps.
2563 */
3353de95 2564static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6
MD
2565{
2566 struct ust_app_session *ua_sess;
2567 struct lttng_ust_object_data obj;
bec39940
DG
2568 struct lttng_ht_iter iter;
2569 struct lttng_ht_node_ulong *node;
525b0740 2570 int ret;
84cd17c6 2571
852d0037 2572 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
2573
2574 rcu_read_lock();
2575
e0c7ec2b
DG
2576 if (!app->compatible) {
2577 goto end;
2578 }
2579
84cd17c6 2580 __lookup_session_by_app(usess, app, &iter);
bec39940 2581 node = lttng_ht_iter_get_node_ulong(&iter);
84cd17c6 2582 if (node == NULL) {
d42f20df
DG
2583 /* Session is being or is deleted. */
2584 goto end;
84cd17c6
MD
2585 }
2586 ua_sess = caa_container_of(node, struct ust_app_session, node);
bec39940 2587 ret = lttng_ht_del(app->sessions, &iter);
c4a1715b
DG
2588 if (ret) {
2589 /* Already scheduled for teardown. */
2590 goto end;
2591 }
2592
84cd17c6
MD
2593 obj.handle = ua_sess->handle;
2594 obj.shm_fd = -1;
2595 obj.wait_fd = -1;
2596 obj.memory_map_size = 0;
840cb59c 2597 health_code_update();
852d0037 2598 ustctl_release_object(app->sock, &obj);
84cd17c6 2599
840cb59c 2600 health_code_update();
852d0037 2601 delete_ust_app_session(app->sock, ua_sess);
7db205b5 2602
84cd17c6 2603 /* Quiescent wait after stopping trace */
852d0037 2604 ustctl_wait_quiescent(app->sock);
84cd17c6 2605
e0c7ec2b
DG
2606end:
2607 rcu_read_unlock();
840cb59c 2608 health_code_update();
84cd17c6 2609 return 0;
84cd17c6
MD
2610}
2611
5b4a0ec0
DG
2612/*
2613 * Start tracing for the UST session.
2614 */
421cb601
DG
2615int ust_app_start_trace_all(struct ltt_ust_session *usess)
2616{
2617 int ret = 0;
bec39940 2618 struct lttng_ht_iter iter;
421cb601 2619 struct ust_app *app;
48842b30 2620
421cb601
DG
2621 DBG("Starting all UST traces");
2622
2623 rcu_read_lock();
421cb601 2624
852d0037 2625 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
421cb601 2626 ret = ust_app_start_trace(usess, app);
48842b30 2627 if (ret < 0) {
5b4a0ec0
DG
2628 /* Continue to next apps even on error */
2629 continue;
48842b30 2630 }
48842b30 2631 }
5b4a0ec0 2632
48842b30
DG
2633 rcu_read_unlock();
2634
2635 return 0;
2636}
487cf67c 2637
8be98f9a
MD
2638/*
2639 * Start tracing for the UST session.
2640 */
2641int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2642{
2643 int ret = 0;
bec39940 2644 struct lttng_ht_iter iter;
8be98f9a
MD
2645 struct ust_app *app;
2646
2647 DBG("Stopping all UST traces");
2648
2649 rcu_read_lock();
2650
852d0037 2651 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
8be98f9a
MD
2652 ret = ust_app_stop_trace(usess, app);
2653 if (ret < 0) {
2654 /* Continue to next apps even on error */
2655 continue;
2656 }
2657 }
2658
2659 rcu_read_unlock();
2660
2661 return 0;
2662}
2663
84cd17c6
MD
2664/*
2665 * Destroy app UST session.
2666 */
2667int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2668{
2669 int ret = 0;
bec39940 2670 struct lttng_ht_iter iter;
84cd17c6
MD
2671 struct ust_app *app;
2672
2673 DBG("Destroy all UST traces");
2674
2675 rcu_read_lock();
2676
852d0037 2677 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 2678 ret = destroy_trace(usess, app);
84cd17c6
MD
2679 if (ret < 0) {
2680 /* Continue to next apps even on error */
2681 continue;
2682 }
2683 }
2684
2685 rcu_read_unlock();
2686
2687 return 0;
2688}
2689
5b4a0ec0
DG
2690/*
2691 * Add channels/events from UST global domain to registered apps at sock.
2692 */
487cf67c
DG
2693void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2694{
55c54cce 2695 int ret = 0;
727d5404 2696 struct lttng_ht_iter iter, uiter, iter_ctx;
487cf67c 2697 struct ust_app *app;
3d8ca23b 2698 struct ust_app_session *ua_sess = NULL;
487cf67c
DG
2699 struct ust_app_channel *ua_chan;
2700 struct ust_app_event *ua_event;
727d5404 2701 struct ust_app_ctx *ua_ctx;
1f3580c7 2702
95e047ff 2703 assert(usess);
1f3580c7 2704
a991f516
MD
2705 DBG2("UST app global update for app sock %d for session id %d", sock,
2706 usess->id);
487cf67c 2707
284d8f55
DG
2708 rcu_read_lock();
2709
487cf67c
DG
2710 app = find_app_by_sock(sock);
2711 if (app == NULL) {
2712 ERR("Failed to update app sock %d", sock);
2713 goto error;
2714 }
2715
e0c7ec2b
DG
2716 if (!app->compatible) {
2717 goto error;
2718 }
2719
3d8ca23b
DG
2720 ret = create_ust_app_session(usess, app, &ua_sess, NULL);
2721 if (ret < 0) {
2722 /* Tracer is probably gone or ENOMEM. */
487cf67c
DG
2723 goto error;
2724 }
3d8ca23b 2725 assert(ua_sess);
487cf67c 2726
284d8f55
DG
2727 /*
2728 * We can iterate safely here over all UST app session sicne the create ust
2729 * app session above made a shadow copy of the UST global domain from the
2730 * ltt ust session.
2731 */
bec39940
DG
2732 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2733 node.node) {
284d8f55
DG
2734 ret = create_ust_channel(app, ua_sess, ua_chan);
2735 if (ret < 0) {
2736 /* FIXME: Should we quit here or continue... */
2737 continue;
487cf67c
DG
2738 }
2739
727d5404
DG
2740 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2741 node.node) {
2742 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2743 if (ret < 0) {
2744 /* FIXME: Should we quit here or continue... */
2745 continue;
2746 }
2747 }
2748
2749
284d8f55 2750 /* For each events */
bec39940
DG
2751 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2752 node.node) {
284d8f55
DG
2753 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2754 if (ret < 0) {
2755 /* FIXME: Should we quit here or continue... */
2756 continue;
487cf67c 2757 }
36dc12cc 2758 }
487cf67c
DG
2759 }
2760
36dc12cc 2761 if (usess->start_trace) {
421cb601 2762 ret = ust_app_start_trace(usess, app);
36dc12cc 2763 if (ret < 0) {
36dc12cc
DG
2764 goto error;
2765 }
2766
852d0037 2767 DBG2("UST trace started for app pid %d", app->pid);
36dc12cc
DG
2768 }
2769
487cf67c
DG
2770error:
2771 rcu_read_unlock();
2772 return;
2773}
55cc08a6
DG
2774
2775/*
2776 * Add context to a specific channel for global UST domain.
2777 */
2778int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2779 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2780{
2781 int ret = 0;
bec39940
DG
2782 struct lttng_ht_node_str *ua_chan_node;
2783 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
2784 struct ust_app_channel *ua_chan = NULL;
2785 struct ust_app_session *ua_sess;
2786 struct ust_app *app;
2787
2788 rcu_read_lock();
2789
852d0037 2790 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
2791 if (!app->compatible) {
2792 /*
2793 * TODO: In time, we should notice the caller of this error by
2794 * telling him that this is a version error.
2795 */
2796 continue;
2797 }
55cc08a6
DG
2798 ua_sess = lookup_session_by_app(usess, app);
2799 if (ua_sess == NULL) {
2800 continue;
2801 }
2802
2803 /* Lookup channel in the ust app session */
bec39940
DG
2804 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2805 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6
DG
2806 if (ua_chan_node == NULL) {
2807 continue;
2808 }
2809 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2810 node);
2811
2812 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2813 if (ret < 0) {
2814 continue;
2815 }
2816 }
2817
55cc08a6
DG
2818 rcu_read_unlock();
2819 return ret;
2820}
2821
76d45b40
DG
2822/*
2823 * Enable event for a channel from a UST session for a specific PID.
2824 */
2825int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2826 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2827{
2828 int ret = 0;
bec39940 2829 struct lttng_ht_iter iter;
18eace3b 2830 struct lttng_ht_node_str *ua_chan_node;
76d45b40
DG
2831 struct ust_app *app;
2832 struct ust_app_session *ua_sess;
2833 struct ust_app_channel *ua_chan;
2834 struct ust_app_event *ua_event;
2835
2836 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2837
2838 rcu_read_lock();
2839
2840 app = ust_app_find_by_pid(pid);
2841 if (app == NULL) {
2842 ERR("UST app enable event per PID %d not found", pid);
2843 ret = -1;
2844 goto error;
2845 }
2846
e0c7ec2b
DG
2847 if (!app->compatible) {
2848 ret = 0;
2849 goto error;
2850 }
2851
76d45b40 2852 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2853 if (!ua_sess) {
2854 /* The application has problem or is probably dead. */
2855 goto error;
2856 }
76d45b40
DG
2857
2858 /* Lookup channel in the ust app session */
bec39940
DG
2859 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2860 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
2861 /* If the channel is not found, there is a code flow error */
2862 assert(ua_chan_node);
2863
2864 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2865
18eace3b
DG
2866 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2867 uevent->filter, uevent->attr.loglevel);
2868 if (ua_event == NULL) {
76d45b40
DG
2869 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2870 if (ret < 0) {
2871 goto error;
2872 }
2873 } else {
76d45b40
DG
2874 ret = enable_ust_app_event(ua_sess, ua_event, app);
2875 if (ret < 0) {
2876 goto error;
2877 }
2878 }
2879
2880error:
2881 rcu_read_unlock();
2882 return ret;
2883}
7f79d3a1
DG
2884
2885/*
2886 * Disable event for a channel from a UST session for a specific PID.
2887 */
2888int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2889 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2890{
2891 int ret = 0;
bec39940
DG
2892 struct lttng_ht_iter iter;
2893 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
7f79d3a1
DG
2894 struct ust_app *app;
2895 struct ust_app_session *ua_sess;
2896 struct ust_app_channel *ua_chan;
2897 struct ust_app_event *ua_event;
2898
2899 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2900
2901 rcu_read_lock();
2902
2903 app = ust_app_find_by_pid(pid);
2904 if (app == NULL) {
2905 ERR("UST app disable event per PID %d not found", pid);
2906 ret = -1;
2907 goto error;
2908 }
2909
e0c7ec2b
DG
2910 if (!app->compatible) {
2911 ret = 0;
2912 goto error;
2913 }
2914
7f79d3a1 2915 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
2916 if (!ua_sess) {
2917 /* The application has problem or is probably dead. */
2918 goto error;
2919 }
7f79d3a1
DG
2920
2921 /* Lookup channel in the ust app session */
bec39940
DG
2922 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2923 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2924 if (ua_chan_node == NULL) {
2925 /* Channel does not exist, skip disabling */
2926 goto error;
2927 }
2928 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2929
bec39940
DG
2930 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2931 ua_event_node = lttng_ht_iter_get_node_str(&iter);
7f79d3a1
DG
2932 if (ua_event_node == NULL) {
2933 /* Event does not exist, skip disabling */
2934 goto error;
2935 }
2936 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2937
2938 ret = disable_ust_app_event(ua_sess, ua_event, app);
2939 if (ret < 0) {
2940 goto error;
2941 }
2942
2943error:
2944 rcu_read_unlock();
2945 return ret;
2946}
e0c7ec2b
DG
2947
2948/*
2949 * Validate version of UST apps and set the compatible bit.
2950 */
2951int ust_app_validate_version(int sock)
2952{
2953 int ret;
2954 struct ust_app *app;
2955
2956 rcu_read_lock();
2957
2958 app = find_app_by_sock(sock);
2959 assert(app);
2960
840cb59c 2961 health_code_update();
86acf0da 2962
e0c7ec2b
DG
2963 ret = ustctl_tracer_version(sock, &app->version);
2964 if (ret < 0) {
2965 goto error;
2966 }
2967
2968 /* Validate version */
aee0cea0 2969 if (app->version.major != UST_APP_MAJOR_VERSION) {
e0c7ec2b
DG
2970 goto error;
2971 }
2972
68264071 2973 DBG2("UST app PID %d is compatible with internal major version %d "
aee0cea0 2974 "(supporting == %d)", app->pid, app->version.major,
e0c7ec2b
DG
2975 UST_APP_MAJOR_VERSION);
2976 app->compatible = 1;
2977 rcu_read_unlock();
840cb59c 2978 health_code_update();
e0c7ec2b
DG
2979 return 0;
2980
2981error:
68264071 2982 DBG2("UST app PID %d is not compatible with internal major version %d "
aee0cea0 2983 "(supporting == %d)", app->pid, app->version.major,
e0c7ec2b
DG
2984 UST_APP_MAJOR_VERSION);
2985 app->compatible = 0;
2986 rcu_read_unlock();
840cb59c 2987 health_code_update();
e0c7ec2b
DG
2988 return -1;
2989}
4466912f
DG
2990
2991/*
2992 * Calibrate registered applications.
2993 */
2994int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2995{
2996 int ret = 0;
2997 struct lttng_ht_iter iter;
2998 struct ust_app *app;
2999
3000 rcu_read_lock();
3001
852d0037 3002 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4466912f
DG
3003 if (!app->compatible) {
3004 /*
3005 * TODO: In time, we should notice the caller of this error by
3006 * telling him that this is a version error.
3007 */
3008 continue;
3009 }
3010
840cb59c 3011 health_code_update();
86acf0da 3012
852d0037 3013 ret = ustctl_calibrate(app->sock, calibrate);
4466912f
DG
3014 if (ret < 0) {
3015 switch (ret) {
3016 case -ENOSYS:
3017 /* Means that it's not implemented on the tracer side. */
3018 ret = 0;
3019 break;
3020 default:
3021 /* TODO: Report error to user */
3022 DBG2("Calibrate app PID %d returned with error %d",
852d0037 3023 app->pid, ret);
4466912f
DG
3024 break;
3025 }
3026 }
3027 }
3028
3029 DBG("UST app global domain calibration finished");
3030
3031 rcu_read_unlock();
3032
840cb59c 3033 health_code_update();
86acf0da 3034
4466912f
DG
3035 return ret;
3036}
This page took 0.355129 seconds and 4 git commands to generate.