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