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