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