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