Ensure UST channel output mode is LTTNG_UST_MMAP
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
CommitLineData
91d76f53
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
bdf64013 3 * Copyright (C) 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
91d76f53 4 *
d14d33bf
AM
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
91d76f53
DG
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
d14d33bf
AM
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
91d76f53
DG
17 */
18
6c1c0768 19#define _LGPL_SOURCE
91d76f53 20#include <errno.h>
7972aab2 21#include <inttypes.h>
91d76f53
DG
22#include <pthread.h>
23#include <stdio.h>
24#include <stdlib.h>
099e26bd 25#include <string.h>
aba8e916
DG
26#include <sys/stat.h>
27#include <sys/types.h>
099e26bd 28#include <unistd.h>
0df502fd 29#include <urcu/compiler.h>
fb54cdbf 30#include <lttng/ust-error.h>
331744e3 31#include <signal.h>
bec39940 32
990570ed 33#include <common/common.h>
86acf0da 34#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 35
7972aab2 36#include "buffer-registry.h"
86acf0da 37#include "fd-limit.h"
8782cc74 38#include "health-sessiond.h"
56fff090 39#include "ust-app.h"
48842b30 40#include "ust-consumer.h"
d80a6244 41#include "ust-ctl.h"
0b2dc8df 42#include "utils.h"
fb83fe64 43#include "session.h"
d80a6244 44
c4b88406
MD
45static
46int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
47
d9bf3ca4
MD
48/* Next available channel key. Access under next_channel_key_lock. */
49static uint64_t _next_channel_key;
50static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
51
52/* Next available session ID. Access under next_session_id_lock. */
53static uint64_t _next_session_id;
54static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
ffe60014
DG
55
56/*
d9bf3ca4 57 * Return the incremented value of next_channel_key.
ffe60014 58 */
d9bf3ca4 59static uint64_t get_next_channel_key(void)
ffe60014 60{
d9bf3ca4
MD
61 uint64_t ret;
62
63 pthread_mutex_lock(&next_channel_key_lock);
64 ret = ++_next_channel_key;
65 pthread_mutex_unlock(&next_channel_key_lock);
66 return ret;
ffe60014
DG
67}
68
69/*
7972aab2 70 * Return the atomically incremented value of next_session_id.
ffe60014 71 */
d9bf3ca4 72static uint64_t get_next_session_id(void)
ffe60014 73{
d9bf3ca4
MD
74 uint64_t ret;
75
76 pthread_mutex_lock(&next_session_id_lock);
77 ret = ++_next_session_id;
78 pthread_mutex_unlock(&next_session_id_lock);
79 return ret;
ffe60014
DG
80}
81
d65d2de8
DG
82static void copy_channel_attr_to_ustctl(
83 struct ustctl_consumer_channel_attr *attr,
84 struct lttng_ust_channel_attr *uattr)
85{
86 /* Copy event attributes since the layout is different. */
87 attr->subbuf_size = uattr->subbuf_size;
88 attr->num_subbuf = uattr->num_subbuf;
89 attr->overwrite = uattr->overwrite;
90 attr->switch_timer_interval = uattr->switch_timer_interval;
91 attr->read_timer_interval = uattr->read_timer_interval;
92 attr->output = uattr->output;
93}
94
025faf73
DG
95/*
96 * Match function for the hash table lookup.
97 *
98 * It matches an ust app event based on three attributes which are the event
99 * name, the filter bytecode and the loglevel.
100 */
18eace3b
DG
101static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
102{
103 struct ust_app_event *event;
104 const struct ust_app_ht_key *key;
2106efa0 105 int ev_loglevel_value;
18eace3b
DG
106
107 assert(node);
108 assert(_key);
109
110 event = caa_container_of(node, struct ust_app_event, node.node);
111 key = _key;
2106efa0 112 ev_loglevel_value = event->attr.loglevel;
18eace3b 113
1af53eb5 114 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
18eace3b
DG
115
116 /* Event name */
117 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
118 goto no_match;
119 }
120
121 /* Event loglevel. */
2106efa0 122 if (ev_loglevel_value != key->loglevel_type) {
025faf73 123 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
2106efa0
PP
124 && key->loglevel_type == 0 &&
125 ev_loglevel_value == -1) {
025faf73
DG
126 /*
127 * Match is accepted. This is because on event creation, the
128 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
129 * -1 are accepted for this loglevel type since 0 is the one set by
130 * the API when receiving an enable event.
131 */
132 } else {
133 goto no_match;
134 }
18eace3b
DG
135 }
136
137 /* One of the filters is NULL, fail. */
138 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
139 goto no_match;
140 }
141
025faf73
DG
142 if (key->filter && event->filter) {
143 /* Both filters exists, check length followed by the bytecode. */
144 if (event->filter->len != key->filter->len ||
145 memcmp(event->filter->data, key->filter->data,
146 event->filter->len) != 0) {
147 goto no_match;
148 }
18eace3b
DG
149 }
150
1af53eb5
JI
151 /* One of the exclusions is NULL, fail. */
152 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
153 goto no_match;
154 }
155
156 if (key->exclusion && event->exclusion) {
157 /* Both exclusions exists, check count followed by the names. */
158 if (event->exclusion->count != key->exclusion->count ||
159 memcmp(event->exclusion->names, key->exclusion->names,
160 event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) {
161 goto no_match;
162 }
163 }
164
165
025faf73 166 /* Match. */
18eace3b
DG
167 return 1;
168
169no_match:
170 return 0;
18eace3b
DG
171}
172
025faf73
DG
173/*
174 * Unique add of an ust app event in the given ht. This uses the custom
175 * ht_match_ust_app_event match function and the event name as hash.
176 */
d0b96690 177static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
18eace3b
DG
178 struct ust_app_event *event)
179{
180 struct cds_lfht_node *node_ptr;
181 struct ust_app_ht_key key;
d0b96690 182 struct lttng_ht *ht;
18eace3b 183
d0b96690
DG
184 assert(ua_chan);
185 assert(ua_chan->events);
18eace3b
DG
186 assert(event);
187
d0b96690 188 ht = ua_chan->events;
18eace3b
DG
189 key.name = event->attr.name;
190 key.filter = event->filter;
2106efa0 191 key.loglevel_type = event->attr.loglevel;
91c89f23 192 key.exclusion = event->exclusion;
18eace3b
DG
193
194 node_ptr = cds_lfht_add_unique(ht->ht,
195 ht->hash_fct(event->node.key, lttng_ht_seed),
196 ht_match_ust_app_event, &key, &event->node.node);
197 assert(node_ptr == &event->node.node);
198}
199
d88aee68
DG
200/*
201 * Close the notify socket from the given RCU head object. This MUST be called
202 * through a call_rcu().
203 */
204static void close_notify_sock_rcu(struct rcu_head *head)
205{
206 int ret;
207 struct ust_app_notify_sock_obj *obj =
208 caa_container_of(head, struct ust_app_notify_sock_obj, head);
209
210 /* Must have a valid fd here. */
211 assert(obj->fd >= 0);
212
213 ret = close(obj->fd);
214 if (ret) {
215 ERR("close notify sock %d RCU", obj->fd);
216 }
217 lttng_fd_put(LTTNG_FD_APPS, 1);
218
219 free(obj);
220}
221
7972aab2
DG
222/*
223 * Return the session registry according to the buffer type of the given
224 * session.
225 *
226 * A registry per UID object MUST exists before calling this function or else
227 * it assert() if not found. RCU read side lock must be acquired.
228 */
229static struct ust_registry_session *get_session_registry(
230 struct ust_app_session *ua_sess)
231{
232 struct ust_registry_session *registry = NULL;
233
234 assert(ua_sess);
235
236 switch (ua_sess->buffer_type) {
237 case LTTNG_BUFFER_PER_PID:
238 {
239 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
240 if (!reg_pid) {
241 goto error;
242 }
243 registry = reg_pid->registry->reg.ust;
244 break;
245 }
246 case LTTNG_BUFFER_PER_UID:
247 {
248 struct buffer_reg_uid *reg_uid = buffer_reg_uid_find(
249 ua_sess->tracing_id, ua_sess->bits_per_long, ua_sess->uid);
250 if (!reg_uid) {
251 goto error;
252 }
253 registry = reg_uid->registry->reg.ust;
254 break;
255 }
256 default:
257 assert(0);
258 };
259
260error:
261 return registry;
262}
263
55cc08a6
DG
264/*
265 * Delete ust context safely. RCU read lock must be held before calling
266 * this function.
267 */
268static
fb45065e
MD
269void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx,
270 struct ust_app *app)
55cc08a6 271{
ffe60014
DG
272 int ret;
273
274 assert(ua_ctx);
275
55cc08a6 276 if (ua_ctx->obj) {
fb45065e 277 pthread_mutex_lock(&app->sock_lock);
ffe60014 278 ret = ustctl_release_object(sock, ua_ctx->obj);
fb45065e 279 pthread_mutex_unlock(&app->sock_lock);
d0b96690
DG
280 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
281 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
282 sock, ua_ctx->obj->handle, ret);
ffe60014 283 }
55cc08a6
DG
284 free(ua_ctx->obj);
285 }
286 free(ua_ctx);
287}
288
d80a6244
DG
289/*
290 * Delete ust app event safely. RCU read lock must be held before calling
291 * this function.
292 */
8b366481 293static
fb45065e
MD
294void delete_ust_app_event(int sock, struct ust_app_event *ua_event,
295 struct ust_app *app)
d80a6244 296{
ffe60014
DG
297 int ret;
298
299 assert(ua_event);
300
53a80697 301 free(ua_event->filter);
951f0b71
JI
302 if (ua_event->exclusion != NULL)
303 free(ua_event->exclusion);
edb67388 304 if (ua_event->obj != NULL) {
fb45065e 305 pthread_mutex_lock(&app->sock_lock);
ffe60014 306 ret = ustctl_release_object(sock, ua_event->obj);
fb45065e 307 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
308 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
309 ERR("UST app sock %d release event obj failed with ret %d",
310 sock, ret);
311 }
edb67388
DG
312 free(ua_event->obj);
313 }
d80a6244
DG
314 free(ua_event);
315}
316
317/*
7972aab2
DG
318 * Release ust data object of the given stream.
319 *
320 * Return 0 on success or else a negative value.
d80a6244 321 */
fb45065e
MD
322static int release_ust_app_stream(int sock, struct ust_app_stream *stream,
323 struct ust_app *app)
d80a6244 324{
7972aab2 325 int ret = 0;
ffe60014
DG
326
327 assert(stream);
328
8b366481 329 if (stream->obj) {
fb45065e 330 pthread_mutex_lock(&app->sock_lock);
ffe60014 331 ret = ustctl_release_object(sock, stream->obj);
fb45065e 332 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
333 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
334 ERR("UST app sock %d release stream obj failed with ret %d",
335 sock, ret);
336 }
4063050c 337 lttng_fd_put(LTTNG_FD_APPS, 2);
8b366481
DG
338 free(stream->obj);
339 }
7972aab2
DG
340
341 return ret;
342}
343
344/*
345 * Delete ust app stream safely. RCU read lock must be held before calling
346 * this function.
347 */
348static
fb45065e
MD
349void delete_ust_app_stream(int sock, struct ust_app_stream *stream,
350 struct ust_app *app)
7972aab2
DG
351{
352 assert(stream);
353
fb45065e 354 (void) release_ust_app_stream(sock, stream, app);
84cd17c6 355 free(stream);
d80a6244
DG
356}
357
36b588ed
MD
358/*
359 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
360 * section and outside of call_rcu thread, so we postpone its execution
361 * using ht_cleanup_push. It is simpler than to change the semantic of
362 * the many callers of delete_ust_app_session().
36b588ed
MD
363 */
364static
365void delete_ust_app_channel_rcu(struct rcu_head *head)
366{
367 struct ust_app_channel *ua_chan =
368 caa_container_of(head, struct ust_app_channel, rcu_head);
369
0b2dc8df
MD
370 ht_cleanup_push(ua_chan->ctx);
371 ht_cleanup_push(ua_chan->events);
36b588ed
MD
372 free(ua_chan);
373}
374
fb83fe64
JD
375/*
376 * Extract the lost packet or discarded events counter when the channel is
377 * being deleted and store the value in the parent channel so we can
378 * access it from lttng list and at stop/destroy.
379 */
380static
381void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
382{
383 uint64_t discarded = 0, lost = 0;
384 struct ltt_session *session;
385 struct ltt_ust_channel *uchan;
386
387 if (ua_chan->attr.type != LTTNG_UST_CHAN_PER_CPU) {
388 return;
389 }
390
391 rcu_read_lock();
392 session = session_find_by_id(ua_chan->session->tracing_id);
393 if (!session) {
394 ERR("Missing LTT session to get discarded events");
395 goto end;
396 }
397 if (!session->ust_session) {
398 ERR("Missing UST session to get discarded events");
399 goto end;
400 }
401
402 if (ua_chan->attr.overwrite) {
403 consumer_get_lost_packets(ua_chan->session->tracing_id,
404 ua_chan->key, session->ust_session->consumer,
405 &lost);
406 } else {
407 consumer_get_discarded_events(ua_chan->session->tracing_id,
408 ua_chan->key, session->ust_session->consumer,
409 &discarded);
410 }
411 uchan = trace_ust_find_channel_by_name(
412 session->ust_session->domain_global.channels,
413 ua_chan->name);
414 if (!uchan) {
415 ERR("Missing UST channel to store discarded counters");
416 goto end;
417 }
418
419 uchan->per_pid_closed_app_discarded += discarded;
420 uchan->per_pid_closed_app_lost += lost;
421
422end:
423 rcu_read_unlock();
424}
425
d80a6244
DG
426/*
427 * Delete ust app channel safely. RCU read lock must be held before calling
428 * this function.
429 */
8b366481 430static
d0b96690
DG
431void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
432 struct ust_app *app)
d80a6244
DG
433{
434 int ret;
bec39940 435 struct lttng_ht_iter iter;
d80a6244 436 struct ust_app_event *ua_event;
55cc08a6 437 struct ust_app_ctx *ua_ctx;
030a66fa 438 struct ust_app_stream *stream, *stmp;
7972aab2 439 struct ust_registry_session *registry;
d80a6244 440
ffe60014
DG
441 assert(ua_chan);
442
443 DBG3("UST app deleting channel %s", ua_chan->name);
444
55cc08a6 445 /* Wipe stream */
d80a6244 446 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
84cd17c6 447 cds_list_del(&stream->list);
fb45065e 448 delete_ust_app_stream(sock, stream, app);
d80a6244
DG
449 }
450
55cc08a6 451 /* Wipe context */
bec39940 452 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
31746f93 453 cds_list_del(&ua_ctx->list);
bec39940 454 ret = lttng_ht_del(ua_chan->ctx, &iter);
55cc08a6 455 assert(!ret);
fb45065e 456 delete_ust_app_ctx(sock, ua_ctx, app);
55cc08a6 457 }
d80a6244 458
55cc08a6 459 /* Wipe events */
bec39940
DG
460 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
461 node.node) {
462 ret = lttng_ht_del(ua_chan->events, &iter);
525b0740 463 assert(!ret);
fb45065e 464 delete_ust_app_event(sock, ua_event, app);
d80a6244 465 }
edb67388 466
c8335706
MD
467 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
468 /* Wipe and free registry from session registry. */
469 registry = get_session_registry(ua_chan->session);
470 if (registry) {
471 ust_registry_channel_del_free(registry, ua_chan->key);
472 }
fb83fe64 473 save_per_pid_lost_discarded_counters(ua_chan);
7972aab2 474 }
d0b96690 475
edb67388 476 if (ua_chan->obj != NULL) {
d0b96690
DG
477 /* Remove channel from application UST object descriptor. */
478 iter.iter.node = &ua_chan->ust_objd_node.node;
c6e62271
DG
479 ret = lttng_ht_del(app->ust_objd, &iter);
480 assert(!ret);
fb45065e 481 pthread_mutex_lock(&app->sock_lock);
ffe60014 482 ret = ustctl_release_object(sock, ua_chan->obj);
fb45065e 483 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
484 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
485 ERR("UST app sock %d release channel obj failed with ret %d",
486 sock, ret);
487 }
7972aab2 488 lttng_fd_put(LTTNG_FD_APPS, 1);
edb67388
DG
489 free(ua_chan->obj);
490 }
36b588ed 491 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
d80a6244
DG
492}
493
fb45065e
MD
494int ust_app_register_done(struct ust_app *app)
495{
496 int ret;
497
498 pthread_mutex_lock(&app->sock_lock);
499 ret = ustctl_register_done(app->sock);
500 pthread_mutex_unlock(&app->sock_lock);
501 return ret;
502}
503
504int ust_app_release_object(struct ust_app *app, struct lttng_ust_object_data *data)
505{
506 int ret, sock;
507
508 if (app) {
509 pthread_mutex_lock(&app->sock_lock);
510 sock = app->sock;
511 } else {
512 sock = -1;
513 }
514 ret = ustctl_release_object(sock, data);
515 if (app) {
516 pthread_mutex_unlock(&app->sock_lock);
517 }
518 return ret;
519}
520
331744e3 521/*
1b532a60
DG
522 * Push metadata to consumer socket.
523 *
dc2bbdae
MD
524 * RCU read-side lock must be held to guarantee existance of socket.
525 * Must be called with the ust app session lock held.
526 * Must be called with the registry lock held.
331744e3
JD
527 *
528 * On success, return the len of metadata pushed or else a negative value.
2c57e06d
MD
529 * Returning a -EPIPE return value means we could not send the metadata,
530 * but it can be caused by recoverable errors (e.g. the application has
531 * terminated concurrently).
331744e3
JD
532 */
533ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
534 struct consumer_socket *socket, int send_zero_data)
535{
536 int ret;
537 char *metadata_str = NULL;
c585821b 538 size_t len, offset, new_metadata_len_sent;
331744e3 539 ssize_t ret_val;
c585821b 540 uint64_t metadata_key;
331744e3
JD
541
542 assert(registry);
543 assert(socket);
1b532a60 544
c585821b
MD
545 metadata_key = registry->metadata_key;
546
ce34fcd0 547 /*
dc2bbdae
MD
548 * Means that no metadata was assigned to the session. This can
549 * happens if no start has been done previously.
ce34fcd0 550 */
c585821b 551 if (!metadata_key) {
ce34fcd0
MD
552 return 0;
553 }
554
1b532a60 555 /*
dc2bbdae
MD
556 * On a push metadata error either the consumer is dead or the
557 * metadata channel has been destroyed because its endpoint
2c57e06d
MD
558 * might have died (e.g: relayd), or because the application has
559 * exited. If so, the metadata closed flag is set to 1 so we
560 * deny pushing metadata again which is not valid anymore on the
561 * consumer side.
1b532a60
DG
562 */
563 if (registry->metadata_closed) {
564 return -EPIPE;
565 }
331744e3 566
331744e3
JD
567 offset = registry->metadata_len_sent;
568 len = registry->metadata_len - registry->metadata_len_sent;
c585821b 569 new_metadata_len_sent = registry->metadata_len;
331744e3
JD
570 if (len == 0) {
571 DBG3("No metadata to push for metadata key %" PRIu64,
572 registry->metadata_key);
573 ret_val = len;
574 if (send_zero_data) {
575 DBG("No metadata to push");
576 goto push_data;
577 }
578 goto end;
579 }
580
581 /* Allocate only what we have to send. */
582 metadata_str = zmalloc(len);
583 if (!metadata_str) {
584 PERROR("zmalloc ust app metadata string");
585 ret_val = -ENOMEM;
586 goto error;
587 }
c585821b 588 /* Copy what we haven't sent out. */
331744e3 589 memcpy(metadata_str, registry->metadata + offset, len);
331744e3
JD
590
591push_data:
c585821b
MD
592 pthread_mutex_unlock(&registry->lock);
593 /*
594 * We need to unlock the registry while we push metadata to
595 * break a circular dependency between the consumerd metadata
596 * lock and the sessiond registry lock. Indeed, pushing metadata
597 * to the consumerd awaits that it gets pushed all the way to
598 * relayd, but doing so requires grabbing the metadata lock. If
599 * a concurrent metadata request is being performed by
600 * consumerd, this can try to grab the registry lock on the
601 * sessiond while holding the metadata lock on the consumer
602 * daemon. Those push and pull schemes are performed on two
603 * different bidirectionnal communication sockets.
604 */
605 ret = consumer_push_metadata(socket, metadata_key,
331744e3 606 metadata_str, len, offset);
c585821b 607 pthread_mutex_lock(&registry->lock);
331744e3 608 if (ret < 0) {
000baf6a 609 /*
dc2bbdae
MD
610 * There is an acceptable race here between the registry
611 * metadata key assignment and the creation on the
612 * consumer. The session daemon can concurrently push
613 * metadata for this registry while being created on the
614 * consumer since the metadata key of the registry is
615 * assigned *before* it is setup to avoid the consumer
616 * to ask for metadata that could possibly be not found
617 * in the session daemon.
000baf6a 618 *
dc2bbdae
MD
619 * The metadata will get pushed either by the session
620 * being stopped or the consumer requesting metadata if
621 * that race is triggered.
000baf6a
DG
622 */
623 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
624 ret = 0;
c585821b
MD
625 } else {
626 ERR("Error pushing metadata to consumer");
000baf6a 627 }
331744e3
JD
628 ret_val = ret;
629 goto error_push;
c585821b
MD
630 } else {
631 /*
632 * Metadata may have been concurrently pushed, since
633 * we're not holding the registry lock while pushing to
634 * consumer. This is handled by the fact that we send
635 * the metadata content, size, and the offset at which
636 * that metadata belongs. This may arrive out of order
637 * on the consumer side, and the consumer is able to
638 * deal with overlapping fragments. The consumer
639 * supports overlapping fragments, which must be
640 * contiguous starting from offset 0. We keep the
641 * largest metadata_len_sent value of the concurrent
642 * send.
643 */
644 registry->metadata_len_sent =
645 max_t(size_t, registry->metadata_len_sent,
646 new_metadata_len_sent);
331744e3 647 }
331744e3
JD
648 free(metadata_str);
649 return len;
650
651end:
652error:
ce34fcd0
MD
653 if (ret_val) {
654 /*
dc2bbdae
MD
655 * On error, flag the registry that the metadata is
656 * closed. We were unable to push anything and this
657 * means that either the consumer is not responding or
658 * the metadata cache has been destroyed on the
659 * consumer.
ce34fcd0
MD
660 */
661 registry->metadata_closed = 1;
662 }
331744e3
JD
663error_push:
664 free(metadata_str);
665 return ret_val;
666}
667
d88aee68 668/*
ce34fcd0 669 * For a given application and session, push metadata to consumer.
331744e3
JD
670 * Either sock or consumer is required : if sock is NULL, the default
671 * socket to send the metadata is retrieved from consumer, if sock
672 * is not NULL we use it to send the metadata.
ce34fcd0 673 * RCU read-side lock must be held while calling this function,
dc2bbdae
MD
674 * therefore ensuring existance of registry. It also ensures existance
675 * of socket throughout this function.
d88aee68
DG
676 *
677 * Return 0 on success else a negative error.
2c57e06d
MD
678 * Returning a -EPIPE return value means we could not send the metadata,
679 * but it can be caused by recoverable errors (e.g. the application has
680 * terminated concurrently).
d88aee68 681 */
7972aab2
DG
682static int push_metadata(struct ust_registry_session *registry,
683 struct consumer_output *consumer)
d88aee68 684{
331744e3
JD
685 int ret_val;
686 ssize_t ret;
d88aee68
DG
687 struct consumer_socket *socket;
688
7972aab2
DG
689 assert(registry);
690 assert(consumer);
691
ce34fcd0 692 pthread_mutex_lock(&registry->lock);
ce34fcd0 693 if (registry->metadata_closed) {
dc2bbdae
MD
694 ret_val = -EPIPE;
695 goto error;
d88aee68
DG
696 }
697
d88aee68 698 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
699 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
700 consumer);
d88aee68 701 if (!socket) {
331744e3 702 ret_val = -1;
ce34fcd0 703 goto error;
d88aee68
DG
704 }
705
331744e3 706 ret = ust_app_push_metadata(registry, socket, 0);
d88aee68 707 if (ret < 0) {
331744e3 708 ret_val = ret;
ce34fcd0 709 goto error;
d88aee68 710 }
dc2bbdae 711 pthread_mutex_unlock(&registry->lock);
d88aee68
DG
712 return 0;
713
ce34fcd0 714error:
dc2bbdae 715 pthread_mutex_unlock(&registry->lock);
331744e3 716 return ret_val;
d88aee68
DG
717}
718
719/*
720 * Send to the consumer a close metadata command for the given session. Once
721 * done, the metadata channel is deleted and the session metadata pointer is
dc2bbdae 722 * nullified. The session lock MUST be held unless the application is
d88aee68
DG
723 * in the destroy path.
724 *
725 * Return 0 on success else a negative value.
726 */
7972aab2
DG
727static int close_metadata(struct ust_registry_session *registry,
728 struct consumer_output *consumer)
d88aee68
DG
729{
730 int ret;
731 struct consumer_socket *socket;
732
7972aab2
DG
733 assert(registry);
734 assert(consumer);
d88aee68 735
7972aab2
DG
736 rcu_read_lock();
737
ce34fcd0
MD
738 pthread_mutex_lock(&registry->lock);
739
7972aab2 740 if (!registry->metadata_key || registry->metadata_closed) {
d88aee68 741 ret = 0;
1b532a60 742 goto end;
d88aee68
DG
743 }
744
d88aee68 745 /* Get consumer socket to use to push the metadata.*/
7972aab2
DG
746 socket = consumer_find_socket_by_bitness(registry->bits_per_long,
747 consumer);
d88aee68
DG
748 if (!socket) {
749 ret = -1;
7972aab2 750 goto error;
d88aee68
DG
751 }
752
7972aab2 753 ret = consumer_close_metadata(socket, registry->metadata_key);
d88aee68 754 if (ret < 0) {
7972aab2 755 goto error;
d88aee68
DG
756 }
757
d88aee68 758error:
1b532a60
DG
759 /*
760 * Metadata closed. Even on error this means that the consumer is not
761 * responding or not found so either way a second close should NOT be emit
762 * for this registry.
763 */
764 registry->metadata_closed = 1;
765end:
ce34fcd0 766 pthread_mutex_unlock(&registry->lock);
7972aab2 767 rcu_read_unlock();
d88aee68
DG
768 return ret;
769}
770
36b588ed
MD
771/*
772 * We need to execute ht_destroy outside of RCU read-side critical
0b2dc8df
MD
773 * section and outside of call_rcu thread, so we postpone its execution
774 * using ht_cleanup_push. It is simpler than to change the semantic of
775 * the many callers of delete_ust_app_session().
36b588ed
MD
776 */
777static
778void delete_ust_app_session_rcu(struct rcu_head *head)
779{
780 struct ust_app_session *ua_sess =
781 caa_container_of(head, struct ust_app_session, rcu_head);
782
0b2dc8df 783 ht_cleanup_push(ua_sess->channels);
36b588ed
MD
784 free(ua_sess);
785}
786
d80a6244
DG
787/*
788 * Delete ust app session safely. RCU read lock must be held before calling
789 * this function.
790 */
8b366481 791static
d0b96690
DG
792void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
793 struct ust_app *app)
d80a6244
DG
794{
795 int ret;
bec39940 796 struct lttng_ht_iter iter;
d80a6244 797 struct ust_app_channel *ua_chan;
7972aab2 798 struct ust_registry_session *registry;
d80a6244 799
d88aee68
DG
800 assert(ua_sess);
801
1b532a60
DG
802 pthread_mutex_lock(&ua_sess->lock);
803
b161602a
MD
804 assert(!ua_sess->deleted);
805 ua_sess->deleted = true;
806
7972aab2 807 registry = get_session_registry(ua_sess);
ce34fcd0 808 if (registry) {
d88aee68 809 /* Push metadata for application before freeing the application. */
7972aab2 810 (void) push_metadata(registry, ua_sess->consumer);
d88aee68 811
7972aab2
DG
812 /*
813 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
814 * metadata only on destroy trace session in this case. Also, the
815 * previous push metadata could have flag the metadata registry to
816 * close so don't send a close command if closed.
7972aab2 817 */
ce34fcd0 818 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
819 /* And ask to close it for this session registry. */
820 (void) close_metadata(registry, ua_sess->consumer);
821 }
d80a6244
DG
822 }
823
bec39940
DG
824 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
825 node.node) {
826 ret = lttng_ht_del(ua_sess->channels, &iter);
525b0740 827 assert(!ret);
d0b96690 828 delete_ust_app_channel(sock, ua_chan, app);
d80a6244 829 }
d80a6244 830
7972aab2
DG
831 /* In case of per PID, the registry is kept in the session. */
832 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
833 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
834 if (reg_pid) {
835 buffer_reg_pid_remove(reg_pid);
836 buffer_reg_pid_destroy(reg_pid);
837 }
838 }
d0b96690 839
aee6bafd 840 if (ua_sess->handle != -1) {
fb45065e 841 pthread_mutex_lock(&app->sock_lock);
ffe60014 842 ret = ustctl_release_handle(sock, ua_sess->handle);
fb45065e 843 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
844 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
845 ERR("UST app sock %d release session handle failed with ret %d",
846 sock, ret);
847 }
10b56aef
MD
848 /* Remove session from application UST object descriptor. */
849 iter.iter.node = &ua_sess->ust_objd_node.node;
850 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
851 assert(!ret);
aee6bafd 852 }
10b56aef 853
1b532a60
DG
854 pthread_mutex_unlock(&ua_sess->lock);
855
6addfa37
MD
856 consumer_output_put(ua_sess->consumer);
857
36b588ed 858 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
d80a6244 859}
91d76f53
DG
860
861/*
284d8f55
DG
862 * Delete a traceable application structure from the global list. Never call
863 * this function outside of a call_rcu call.
36b588ed
MD
864 *
865 * RCU read side lock should _NOT_ be held when calling this function.
91d76f53 866 */
8b366481
DG
867static
868void delete_ust_app(struct ust_app *app)
91d76f53 869{
8b366481 870 int ret, sock;
d42f20df 871 struct ust_app_session *ua_sess, *tmp_ua_sess;
44d3bd01 872
d80a6244 873 /* Delete ust app sessions info */
852d0037
DG
874 sock = app->sock;
875 app->sock = -1;
d80a6244 876
8b366481 877 /* Wipe sessions */
d42f20df
DG
878 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
879 teardown_node) {
880 /* Free every object in the session and the session. */
36b588ed 881 rcu_read_lock();
d0b96690 882 delete_ust_app_session(sock, ua_sess, app);
36b588ed 883 rcu_read_unlock();
d80a6244 884 }
36b588ed 885
0b2dc8df 886 ht_cleanup_push(app->sessions);
10b56aef 887 ht_cleanup_push(app->ust_sessions_objd);
0b2dc8df 888 ht_cleanup_push(app->ust_objd);
d80a6244 889
6414a713 890 /*
852d0037
DG
891 * Wait until we have deleted the application from the sock hash table
892 * before closing this socket, otherwise an application could re-use the
893 * socket ID and race with the teardown, using the same hash table entry.
894 *
895 * It's OK to leave the close in call_rcu. We want it to stay unique for
896 * all RCU readers that could run concurrently with unregister app,
897 * therefore we _need_ to only close that socket after a grace period. So
898 * it should stay in this RCU callback.
899 *
900 * This close() is a very important step of the synchronization model so
901 * every modification to this function must be carefully reviewed.
6414a713 902 */
799e2c4f
MD
903 ret = close(sock);
904 if (ret) {
905 PERROR("close");
906 }
4063050c 907 lttng_fd_put(LTTNG_FD_APPS, 1);
d80a6244 908
852d0037 909 DBG2("UST app pid %d deleted", app->pid);
284d8f55 910 free(app);
099e26bd
DG
911}
912
913/*
f6a9efaa 914 * URCU intermediate call to delete an UST app.
099e26bd 915 */
8b366481
DG
916static
917void delete_ust_app_rcu(struct rcu_head *head)
099e26bd 918{
bec39940
DG
919 struct lttng_ht_node_ulong *node =
920 caa_container_of(head, struct lttng_ht_node_ulong, head);
f6a9efaa 921 struct ust_app *app =
852d0037 922 caa_container_of(node, struct ust_app, pid_n);
f6a9efaa 923
852d0037 924 DBG3("Call RCU deleting app PID %d", app->pid);
f6a9efaa 925 delete_ust_app(app);
099e26bd
DG
926}
927
ffe60014
DG
928/*
929 * Delete the session from the application ht and delete the data structure by
930 * freeing every object inside and releasing them.
931 */
d0b96690 932static void destroy_app_session(struct ust_app *app,
ffe60014
DG
933 struct ust_app_session *ua_sess)
934{
935 int ret;
936 struct lttng_ht_iter iter;
937
938 assert(app);
939 assert(ua_sess);
940
941 iter.iter.node = &ua_sess->node.node;
942 ret = lttng_ht_del(app->sessions, &iter);
943 if (ret) {
944 /* Already scheduled for teardown. */
945 goto end;
946 }
947
948 /* Once deleted, free the data structure. */
d0b96690 949 delete_ust_app_session(app->sock, ua_sess, app);
ffe60014
DG
950
951end:
952 return;
953}
954
8b366481
DG
955/*
956 * Alloc new UST app session.
957 */
958static
d0b96690 959struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
8b366481
DG
960{
961 struct ust_app_session *ua_sess;
962
963 /* Init most of the default value by allocating and zeroing */
964 ua_sess = zmalloc(sizeof(struct ust_app_session));
965 if (ua_sess == NULL) {
966 PERROR("malloc");
ffe60014 967 goto error_free;
8b366481
DG
968 }
969
970 ua_sess->handle = -1;
bec39940 971 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
ad7a9107 972 ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA;
84ad93e8 973 pthread_mutex_init(&ua_sess->lock, NULL);
ad7a9107 974
8b366481
DG
975 return ua_sess;
976
ffe60014 977error_free:
8b366481
DG
978 return NULL;
979}
980
981/*
982 * Alloc new UST app channel.
983 */
984static
985struct ust_app_channel *alloc_ust_app_channel(char *name,
d0b96690 986 struct ust_app_session *ua_sess,
ffe60014 987 struct lttng_ust_channel_attr *attr)
8b366481
DG
988{
989 struct ust_app_channel *ua_chan;
990
991 /* Init most of the default value by allocating and zeroing */
992 ua_chan = zmalloc(sizeof(struct ust_app_channel));
993 if (ua_chan == NULL) {
994 PERROR("malloc");
995 goto error;
996 }
997
998 /* Setup channel name */
999 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1000 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1001
1002 ua_chan->enabled = 1;
1003 ua_chan->handle = -1;
45893984 1004 ua_chan->session = ua_sess;
ffe60014 1005 ua_chan->key = get_next_channel_key();
bec39940
DG
1006 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1007 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1008 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
8b366481
DG
1009
1010 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
31746f93 1011 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
8b366481
DG
1012
1013 /* Copy attributes */
1014 if (attr) {
ffe60014 1015 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
2fe6e7f5
DG
1016 ua_chan->attr.subbuf_size = attr->subbuf_size;
1017 ua_chan->attr.num_subbuf = attr->num_subbuf;
1018 ua_chan->attr.overwrite = attr->overwrite;
1019 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1020 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1021 ua_chan->attr.output = attr->output;
8b366481 1022 }
ffe60014
DG
1023 /* By default, the channel is a per cpu channel. */
1024 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
8b366481
DG
1025
1026 DBG3("UST app channel %s allocated", ua_chan->name);
1027
1028 return ua_chan;
1029
1030error:
1031 return NULL;
1032}
1033
37f1c236
DG
1034/*
1035 * Allocate and initialize a UST app stream.
1036 *
1037 * Return newly allocated stream pointer or NULL on error.
1038 */
ffe60014 1039struct ust_app_stream *ust_app_alloc_stream(void)
37f1c236
DG
1040{
1041 struct ust_app_stream *stream = NULL;
1042
1043 stream = zmalloc(sizeof(*stream));
1044 if (stream == NULL) {
1045 PERROR("zmalloc ust app stream");
1046 goto error;
1047 }
1048
1049 /* Zero could be a valid value for a handle so flag it to -1. */
1050 stream->handle = -1;
1051
1052error:
1053 return stream;
1054}
1055
8b366481
DG
1056/*
1057 * Alloc new UST app event.
1058 */
1059static
1060struct ust_app_event *alloc_ust_app_event(char *name,
1061 struct lttng_ust_event *attr)
1062{
1063 struct ust_app_event *ua_event;
1064
1065 /* Init most of the default value by allocating and zeroing */
1066 ua_event = zmalloc(sizeof(struct ust_app_event));
1067 if (ua_event == NULL) {
1068 PERROR("malloc");
1069 goto error;
1070 }
1071
1072 ua_event->enabled = 1;
1073 strncpy(ua_event->name, name, sizeof(ua_event->name));
1074 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
bec39940 1075 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
8b366481
DG
1076
1077 /* Copy attributes */
1078 if (attr) {
1079 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1080 }
1081
1082 DBG3("UST app event %s allocated", ua_event->name);
1083
1084 return ua_event;
1085
1086error:
1087 return NULL;
1088}
1089
1090/*
1091 * Alloc new UST app context.
1092 */
1093static
bdf64013 1094struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
8b366481
DG
1095{
1096 struct ust_app_ctx *ua_ctx;
1097
1098 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
1099 if (ua_ctx == NULL) {
1100 goto error;
1101 }
1102
31746f93
DG
1103 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1104
8b366481
DG
1105 if (uctx) {
1106 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
bdf64013
JG
1107 if (uctx->ctx == LTTNG_UST_CONTEXT_APP_CONTEXT) {
1108 char *provider_name = NULL, *ctx_name = NULL;
1109
1110 provider_name = strdup(uctx->u.app_ctx.provider_name);
1111 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1112 if (!provider_name || !ctx_name) {
1113 free(provider_name);
1114 free(ctx_name);
1115 goto error;
1116 }
1117
1118 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1119 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1120 }
8b366481
DG
1121 }
1122
1123 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
8b366481 1124 return ua_ctx;
bdf64013
JG
1125error:
1126 free(ua_ctx);
1127 return NULL;
8b366481
DG
1128}
1129
025faf73
DG
1130/*
1131 * Allocate a filter and copy the given original filter.
1132 *
1133 * Return allocated filter or NULL on error.
1134 */
51755dc8
JG
1135static struct lttng_filter_bytecode *copy_filter_bytecode(
1136 struct lttng_filter_bytecode *orig_f)
025faf73 1137{
51755dc8 1138 struct lttng_filter_bytecode *filter = NULL;
025faf73
DG
1139
1140 /* Copy filter bytecode */
1141 filter = zmalloc(sizeof(*filter) + orig_f->len);
1142 if (!filter) {
51755dc8 1143 PERROR("zmalloc alloc filter bytecode");
025faf73
DG
1144 goto error;
1145 }
1146
1147 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1148
1149error:
1150 return filter;
1151}
1152
51755dc8
JG
1153/*
1154 * Create a liblttng-ust filter bytecode from given bytecode.
1155 *
1156 * Return allocated filter or NULL on error.
1157 */
1158static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode(
1159 struct lttng_filter_bytecode *orig_f)
1160{
1161 struct lttng_ust_filter_bytecode *filter = NULL;
1162
1163 /* Copy filter bytecode */
1164 filter = zmalloc(sizeof(*filter) + orig_f->len);
1165 if (!filter) {
1166 PERROR("zmalloc alloc ust filter bytecode");
1167 goto error;
1168 }
1169
1170 assert(sizeof(struct lttng_filter_bytecode) ==
1171 sizeof(struct lttng_ust_filter_bytecode));
1172 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1173error:
1174 return filter;
1175}
1176
099e26bd 1177/*
421cb601
DG
1178 * Find an ust_app using the sock and return it. RCU read side lock must be
1179 * held before calling this helper function.
099e26bd 1180 */
f20baf8e 1181struct ust_app *ust_app_find_by_sock(int sock)
099e26bd 1182{
bec39940 1183 struct lttng_ht_node_ulong *node;
bec39940 1184 struct lttng_ht_iter iter;
f6a9efaa 1185
852d0037 1186 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
bec39940 1187 node = lttng_ht_iter_get_node_ulong(&iter);
f6a9efaa
DG
1188 if (node == NULL) {
1189 DBG2("UST app find by sock %d not found", sock);
f6a9efaa
DG
1190 goto error;
1191 }
852d0037
DG
1192
1193 return caa_container_of(node, struct ust_app, sock_n);
f6a9efaa
DG
1194
1195error:
1196 return NULL;
099e26bd
DG
1197}
1198
d0b96690
DG
1199/*
1200 * Find an ust_app using the notify sock and return it. RCU read side lock must
1201 * be held before calling this helper function.
1202 */
1203static struct ust_app *find_app_by_notify_sock(int sock)
1204{
1205 struct lttng_ht_node_ulong *node;
1206 struct lttng_ht_iter iter;
1207
1208 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
1209 &iter);
1210 node = lttng_ht_iter_get_node_ulong(&iter);
1211 if (node == NULL) {
1212 DBG2("UST app find by notify sock %d not found", sock);
1213 goto error;
1214 }
1215
1216 return caa_container_of(node, struct ust_app, notify_sock_n);
1217
1218error:
1219 return NULL;
1220}
1221
025faf73
DG
1222/*
1223 * Lookup for an ust app event based on event name, filter bytecode and the
1224 * event loglevel.
1225 *
1226 * Return an ust_app_event object or NULL on error.
1227 */
18eace3b 1228static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
2106efa0
PP
1229 char *name, struct lttng_filter_bytecode *filter,
1230 int loglevel_value,
39c5a3a7 1231 const struct lttng_event_exclusion *exclusion)
18eace3b
DG
1232{
1233 struct lttng_ht_iter iter;
1234 struct lttng_ht_node_str *node;
1235 struct ust_app_event *event = NULL;
1236 struct ust_app_ht_key key;
18eace3b
DG
1237
1238 assert(name);
1239 assert(ht);
1240
1241 /* Setup key for event lookup. */
1242 key.name = name;
1243 key.filter = filter;
2106efa0 1244 key.loglevel_type = loglevel_value;
39c5a3a7 1245 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
51755dc8 1246 key.exclusion = exclusion;
18eace3b 1247
025faf73
DG
1248 /* Lookup using the event name as hash and a custom match fct. */
1249 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
1250 ht_match_ust_app_event, &key, &iter.iter);
18eace3b
DG
1251 node = lttng_ht_iter_get_node_str(&iter);
1252 if (node == NULL) {
1253 goto end;
1254 }
1255
1256 event = caa_container_of(node, struct ust_app_event, node);
1257
1258end:
18eace3b
DG
1259 return event;
1260}
1261
55cc08a6
DG
1262/*
1263 * Create the channel context on the tracer.
d0b96690
DG
1264 *
1265 * Called with UST app session lock held.
55cc08a6
DG
1266 */
1267static
1268int create_ust_channel_context(struct ust_app_channel *ua_chan,
1269 struct ust_app_ctx *ua_ctx, struct ust_app *app)
1270{
1271 int ret;
1272
840cb59c 1273 health_code_update();
86acf0da 1274
fb45065e 1275 pthread_mutex_lock(&app->sock_lock);
852d0037 1276 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
55cc08a6 1277 ua_chan->obj, &ua_ctx->obj);
fb45065e 1278 pthread_mutex_unlock(&app->sock_lock);
55cc08a6 1279 if (ret < 0) {
ffe60014
DG
1280 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1281 ERR("UST app create channel context failed for app (pid: %d) "
1282 "with ret %d", app->pid, ret);
1283 } else {
3757b385
DG
1284 /*
1285 * This is normal behavior, an application can die during the
1286 * creation process. Don't report an error so the execution can
1287 * continue normally.
1288 */
1289 ret = 0;
ffe60014
DG
1290 DBG3("UST app disable event failed. Application is dead.");
1291 }
55cc08a6
DG
1292 goto error;
1293 }
1294
1295 ua_ctx->handle = ua_ctx->obj->handle;
1296
d0b96690
DG
1297 DBG2("UST app context handle %d created successfully for channel %s",
1298 ua_ctx->handle, ua_chan->name);
55cc08a6
DG
1299
1300error:
840cb59c 1301 health_code_update();
55cc08a6
DG
1302 return ret;
1303}
1304
53a80697
MD
1305/*
1306 * Set the filter on the tracer.
1307 */
1308static
1309int set_ust_event_filter(struct ust_app_event *ua_event,
1310 struct ust_app *app)
1311{
1312 int ret;
51755dc8 1313 struct lttng_ust_filter_bytecode *ust_bytecode = NULL;
53a80697 1314
840cb59c 1315 health_code_update();
86acf0da 1316
53a80697 1317 if (!ua_event->filter) {
86acf0da
DG
1318 ret = 0;
1319 goto error;
53a80697
MD
1320 }
1321
51755dc8
JG
1322 ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter);
1323 if (!ust_bytecode) {
1324 ret = -LTTNG_ERR_NOMEM;
1325 goto error;
1326 }
fb45065e 1327 pthread_mutex_lock(&app->sock_lock);
51755dc8 1328 ret = ustctl_set_filter(app->sock, ust_bytecode,
53a80697 1329 ua_event->obj);
fb45065e 1330 pthread_mutex_unlock(&app->sock_lock);
53a80697 1331 if (ret < 0) {
ffe60014
DG
1332 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1333 ERR("UST app event %s filter failed for app (pid: %d) "
1334 "with ret %d", ua_event->attr.name, app->pid, ret);
1335 } else {
3757b385
DG
1336 /*
1337 * This is normal behavior, an application can die during the
1338 * creation process. Don't report an error so the execution can
1339 * continue normally.
1340 */
1341 ret = 0;
ffe60014
DG
1342 DBG3("UST app filter event failed. Application is dead.");
1343 }
53a80697
MD
1344 goto error;
1345 }
1346
1347 DBG2("UST filter set successfully for event %s", ua_event->name);
1348
1349error:
840cb59c 1350 health_code_update();
51755dc8 1351 free(ust_bytecode);
53a80697
MD
1352 return ret;
1353}
1354
51755dc8
JG
1355static
1356struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion(
1357 struct lttng_event_exclusion *exclusion)
1358{
1359 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
1360 size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) +
1361 LTTNG_UST_SYM_NAME_LEN * exclusion->count;
1362
1363 ust_exclusion = zmalloc(exclusion_alloc_size);
1364 if (!ust_exclusion) {
1365 PERROR("malloc");
1366 goto end;
1367 }
1368
1369 assert(sizeof(struct lttng_event_exclusion) ==
1370 sizeof(struct lttng_ust_event_exclusion));
1371 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1372end:
1373 return ust_exclusion;
1374}
1375
7cc9a73c
JI
1376/*
1377 * Set event exclusions on the tracer.
1378 */
1379static
1380int set_ust_event_exclusion(struct ust_app_event *ua_event,
1381 struct ust_app *app)
1382{
1383 int ret;
51755dc8 1384 struct lttng_ust_event_exclusion *ust_exclusion = NULL;
7cc9a73c
JI
1385
1386 health_code_update();
1387
1388 if (!ua_event->exclusion || !ua_event->exclusion->count) {
1389 ret = 0;
1390 goto error;
1391 }
1392
51755dc8
JG
1393 ust_exclusion = create_ust_exclusion_from_exclusion(
1394 ua_event->exclusion);
1395 if (!ust_exclusion) {
1396 ret = -LTTNG_ERR_NOMEM;
1397 goto error;
1398 }
fb45065e 1399 pthread_mutex_lock(&app->sock_lock);
51755dc8 1400 ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj);
fb45065e 1401 pthread_mutex_unlock(&app->sock_lock);
7cc9a73c
JI
1402 if (ret < 0) {
1403 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1404 ERR("UST app event %s exclusions failed for app (pid: %d) "
1405 "with ret %d", ua_event->attr.name, app->pid, ret);
1406 } else {
1407 /*
1408 * This is normal behavior, an application can die during the
1409 * creation process. Don't report an error so the execution can
1410 * continue normally.
1411 */
1412 ret = 0;
1413 DBG3("UST app event exclusion failed. Application is dead.");
1414 }
1415 goto error;
1416 }
1417
1418 DBG2("UST exclusion set successfully for event %s", ua_event->name);
1419
1420error:
1421 health_code_update();
51755dc8 1422 free(ust_exclusion);
7cc9a73c
JI
1423 return ret;
1424}
1425
9730260e
DG
1426/*
1427 * Disable the specified event on to UST tracer for the UST session.
1428 */
1429static int disable_ust_event(struct ust_app *app,
1430 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1431{
1432 int ret;
1433
840cb59c 1434 health_code_update();
86acf0da 1435
fb45065e 1436 pthread_mutex_lock(&app->sock_lock);
852d0037 1437 ret = ustctl_disable(app->sock, ua_event->obj);
fb45065e 1438 pthread_mutex_unlock(&app->sock_lock);
9730260e 1439 if (ret < 0) {
ffe60014
DG
1440 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1441 ERR("UST app event %s disable failed for app (pid: %d) "
1442 "and session handle %d with ret %d",
1443 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1444 } else {
3757b385
DG
1445 /*
1446 * This is normal behavior, an application can die during the
1447 * creation process. Don't report an error so the execution can
1448 * continue normally.
1449 */
1450 ret = 0;
ffe60014
DG
1451 DBG3("UST app disable event failed. Application is dead.");
1452 }
9730260e
DG
1453 goto error;
1454 }
1455
1456 DBG2("UST app event %s disabled successfully for app (pid: %d)",
852d0037 1457 ua_event->attr.name, app->pid);
9730260e
DG
1458
1459error:
840cb59c 1460 health_code_update();
9730260e
DG
1461 return ret;
1462}
1463
78f0bacd
DG
1464/*
1465 * Disable the specified channel on to UST tracer for the UST session.
1466 */
1467static int disable_ust_channel(struct ust_app *app,
1468 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1469{
1470 int ret;
1471
840cb59c 1472 health_code_update();
86acf0da 1473
fb45065e 1474 pthread_mutex_lock(&app->sock_lock);
852d0037 1475 ret = ustctl_disable(app->sock, ua_chan->obj);
fb45065e 1476 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1477 if (ret < 0) {
ffe60014
DG
1478 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1479 ERR("UST app channel %s disable failed for app (pid: %d) "
1480 "and session handle %d with ret %d",
1481 ua_chan->name, app->pid, ua_sess->handle, ret);
1482 } else {
3757b385
DG
1483 /*
1484 * This is normal behavior, an application can die during the
1485 * creation process. Don't report an error so the execution can
1486 * continue normally.
1487 */
1488 ret = 0;
ffe60014
DG
1489 DBG3("UST app disable channel failed. Application is dead.");
1490 }
78f0bacd
DG
1491 goto error;
1492 }
1493
78f0bacd 1494 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
852d0037 1495 ua_chan->name, app->pid);
78f0bacd
DG
1496
1497error:
840cb59c 1498 health_code_update();
78f0bacd
DG
1499 return ret;
1500}
1501
1502/*
1503 * Enable the specified channel on to UST tracer for the UST session.
1504 */
1505static int enable_ust_channel(struct ust_app *app,
1506 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
1507{
1508 int ret;
1509
840cb59c 1510 health_code_update();
86acf0da 1511
fb45065e 1512 pthread_mutex_lock(&app->sock_lock);
852d0037 1513 ret = ustctl_enable(app->sock, ua_chan->obj);
fb45065e 1514 pthread_mutex_unlock(&app->sock_lock);
78f0bacd 1515 if (ret < 0) {
ffe60014
DG
1516 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1517 ERR("UST app channel %s enable failed for app (pid: %d) "
1518 "and session handle %d with ret %d",
1519 ua_chan->name, app->pid, ua_sess->handle, ret);
1520 } else {
3757b385
DG
1521 /*
1522 * This is normal behavior, an application can die during the
1523 * creation process. Don't report an error so the execution can
1524 * continue normally.
1525 */
1526 ret = 0;
ffe60014
DG
1527 DBG3("UST app enable channel failed. Application is dead.");
1528 }
78f0bacd
DG
1529 goto error;
1530 }
1531
1532 ua_chan->enabled = 1;
1533
1534 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
852d0037 1535 ua_chan->name, app->pid);
78f0bacd
DG
1536
1537error:
840cb59c 1538 health_code_update();
78f0bacd
DG
1539 return ret;
1540}
1541
edb67388
DG
1542/*
1543 * Enable the specified event on to UST tracer for the UST session.
1544 */
1545static int enable_ust_event(struct ust_app *app,
1546 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
1547{
1548 int ret;
1549
840cb59c 1550 health_code_update();
86acf0da 1551
fb45065e 1552 pthread_mutex_lock(&app->sock_lock);
852d0037 1553 ret = ustctl_enable(app->sock, ua_event->obj);
fb45065e 1554 pthread_mutex_unlock(&app->sock_lock);
edb67388 1555 if (ret < 0) {
ffe60014
DG
1556 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1557 ERR("UST app event %s enable failed for app (pid: %d) "
1558 "and session handle %d with ret %d",
1559 ua_event->attr.name, app->pid, ua_sess->handle, ret);
1560 } else {
3757b385
DG
1561 /*
1562 * This is normal behavior, an application can die during the
1563 * creation process. Don't report an error so the execution can
1564 * continue normally.
1565 */
1566 ret = 0;
ffe60014
DG
1567 DBG3("UST app enable event failed. Application is dead.");
1568 }
edb67388
DG
1569 goto error;
1570 }
1571
1572 DBG2("UST app event %s enabled successfully for app (pid: %d)",
852d0037 1573 ua_event->attr.name, app->pid);
edb67388
DG
1574
1575error:
840cb59c 1576 health_code_update();
edb67388
DG
1577 return ret;
1578}
1579
099e26bd 1580/*
7972aab2 1581 * Send channel and stream buffer to application.
4f3ab6ee 1582 *
ffe60014 1583 * Return 0 on success. On error, a negative value is returned.
4f3ab6ee 1584 */
7972aab2
DG
1585static int send_channel_pid_to_ust(struct ust_app *app,
1586 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
4f3ab6ee
DG
1587{
1588 int ret;
ffe60014 1589 struct ust_app_stream *stream, *stmp;
4f3ab6ee
DG
1590
1591 assert(app);
ffe60014 1592 assert(ua_sess);
4f3ab6ee 1593 assert(ua_chan);
4f3ab6ee 1594
840cb59c 1595 health_code_update();
4f3ab6ee 1596
7972aab2
DG
1597 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name,
1598 app->sock);
86acf0da 1599
ffe60014
DG
1600 /* Send channel to the application. */
1601 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
1602 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1603 ret = -ENOTCONN; /* Caused by app exiting. */
1604 goto error;
1605 } else if (ret < 0) {
b551a063
DG
1606 goto error;
1607 }
1608
d88aee68
DG
1609 health_code_update();
1610
ffe60014
DG
1611 /* Send all streams to application. */
1612 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
1613 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
a7169585
MD
1614 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1615 ret = -ENOTCONN; /* Caused by app exiting. */
1616 goto error;
1617 } else if (ret < 0) {
ffe60014
DG
1618 goto error;
1619 }
1620 /* We don't need the stream anymore once sent to the tracer. */
1621 cds_list_del(&stream->list);
fb45065e 1622 delete_ust_app_stream(-1, stream, app);
ffe60014 1623 }
ffe60014
DG
1624 /* Flag the channel that it is sent to the application. */
1625 ua_chan->is_sent = 1;
ffe60014 1626
b551a063 1627error:
840cb59c 1628 health_code_update();
b551a063
DG
1629 return ret;
1630}
1631
91d76f53 1632/*
5b4a0ec0 1633 * Create the specified event onto the UST tracer for a UST session.
d0b96690
DG
1634 *
1635 * Should be called with session mutex held.
91d76f53 1636 */
edb67388
DG
1637static
1638int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1639 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
91d76f53 1640{
5b4a0ec0 1641 int ret = 0;
284d8f55 1642
840cb59c 1643 health_code_update();
86acf0da 1644
5b4a0ec0 1645 /* Create UST event on tracer */
fb45065e 1646 pthread_mutex_lock(&app->sock_lock);
852d0037 1647 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
5b4a0ec0 1648 &ua_event->obj);
fb45065e 1649 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0 1650 if (ret < 0) {
ffe60014
DG
1651 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1652 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1653 ua_event->attr.name, app->pid, ret);
1654 } else {
3757b385
DG
1655 /*
1656 * This is normal behavior, an application can die during the
1657 * creation process. Don't report an error so the execution can
1658 * continue normally.
1659 */
1660 ret = 0;
ffe60014
DG
1661 DBG3("UST app create event failed. Application is dead.");
1662 }
5b4a0ec0 1663 goto error;
91d76f53 1664 }
f6a9efaa 1665
5b4a0ec0 1666 ua_event->handle = ua_event->obj->handle;
284d8f55 1667
5b4a0ec0 1668 DBG2("UST app event %s created successfully for pid:%d",
852d0037 1669 ua_event->attr.name, app->pid);
f6a9efaa 1670
840cb59c 1671 health_code_update();
86acf0da 1672
025faf73
DG
1673 /* Set filter if one is present. */
1674 if (ua_event->filter) {
1675 ret = set_ust_event_filter(ua_event, app);
1676 if (ret < 0) {
1677 goto error;
1678 }
1679 }
1680
7cc9a73c
JI
1681 /* Set exclusions for the event */
1682 if (ua_event->exclusion) {
1683 ret = set_ust_event_exclusion(ua_event, app);
1684 if (ret < 0) {
1685 goto error;
1686 }
1687 }
1688
8535a6d9 1689 /* If event not enabled, disable it on the tracer */
40113787
MD
1690 if (ua_event->enabled) {
1691 /*
1692 * We now need to explicitly enable the event, since it
1693 * is now disabled at creation.
1694 */
1695 ret = enable_ust_event(app, ua_sess, ua_event);
1696 if (ret < 0) {
1697 /*
1698 * If we hit an EPERM, something is wrong with our enable call. If
1699 * we get an EEXIST, there is a problem on the tracer side since we
1700 * just created it.
1701 */
1702 switch (ret) {
1703 case -LTTNG_UST_ERR_PERM:
1704 /* Code flow problem */
1705 assert(0);
1706 case -LTTNG_UST_ERR_EXIST:
1707 /* It's OK for our use case. */
1708 ret = 0;
1709 break;
1710 default:
1711 break;
1712 }
1713 goto error;
1714 }
8535a6d9
DG
1715 }
1716
5b4a0ec0 1717error:
840cb59c 1718 health_code_update();
5b4a0ec0 1719 return ret;
91d76f53 1720}
48842b30 1721
5b4a0ec0
DG
1722/*
1723 * Copy data between an UST app event and a LTT event.
1724 */
421cb601 1725static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
1726 struct ltt_ust_event *uevent)
1727{
b4ffad32
JI
1728 size_t exclusion_alloc_size;
1729
48842b30
DG
1730 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1731 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1732
fc34caaa
DG
1733 ua_event->enabled = uevent->enabled;
1734
5b4a0ec0
DG
1735 /* Copy event attributes */
1736 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1737
53a80697
MD
1738 /* Copy filter bytecode */
1739 if (uevent->filter) {
51755dc8 1740 ua_event->filter = copy_filter_bytecode(uevent->filter);
025faf73 1741 /* Filter might be NULL here in case of ENONEM. */
53a80697 1742 }
b4ffad32
JI
1743
1744 /* Copy exclusion data */
1745 if (uevent->exclusion) {
51755dc8 1746 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
b4ffad32
JI
1747 LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count;
1748 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
1749 if (ua_event->exclusion == NULL) {
1750 PERROR("malloc");
1751 } else {
1752 memcpy(ua_event->exclusion, uevent->exclusion,
1753 exclusion_alloc_size);
b4ffad32
JI
1754 }
1755 }
48842b30
DG
1756}
1757
5b4a0ec0
DG
1758/*
1759 * Copy data between an UST app channel and a LTT channel.
1760 */
421cb601 1761static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
1762 struct ltt_ust_channel *uchan)
1763{
bec39940 1764 struct lttng_ht_iter iter;
48842b30 1765 struct ltt_ust_event *uevent;
55cc08a6 1766 struct ltt_ust_context *uctx;
48842b30
DG
1767 struct ust_app_event *ua_event;
1768
fc34caaa 1769 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
1770
1771 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1772 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 1773
1624d5b7
JD
1774 ua_chan->tracefile_size = uchan->tracefile_size;
1775 ua_chan->tracefile_count = uchan->tracefile_count;
1776
ffe60014
DG
1777 /* Copy event attributes since the layout is different. */
1778 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1779 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1780 ua_chan->attr.overwrite = uchan->attr.overwrite;
1781 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1782 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1783 ua_chan->attr.output = uchan->attr.output;
1784 /*
1785 * Note that the attribute channel type is not set since the channel on the
1786 * tracing registry side does not have this information.
1787 */
48842b30 1788
fc34caaa 1789 ua_chan->enabled = uchan->enabled;
7972aab2 1790 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 1791
31746f93 1792 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
bdf64013
JG
1793 struct ust_app_ctx *ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1794
55cc08a6
DG
1795 if (ua_ctx == NULL) {
1796 continue;
1797 }
bec39940
DG
1798 lttng_ht_node_init_ulong(&ua_ctx->node,
1799 (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 1800 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 1801 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
55cc08a6 1802 }
48842b30 1803
421cb601 1804 /* Copy all events from ltt ust channel to ust app channel */
bec39940 1805 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
18eace3b 1806 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 1807 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 1808 if (ua_event == NULL) {
421cb601 1809 DBG2("UST event %s not found on shadow copy channel",
48842b30 1810 uevent->attr.name);
284d8f55 1811 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
48842b30 1812 if (ua_event == NULL) {
5b4a0ec0 1813 continue;
48842b30 1814 }
421cb601 1815 shadow_copy_event(ua_event, uevent);
d0b96690 1816 add_unique_ust_app_event(ua_chan, ua_event);
48842b30 1817 }
48842b30
DG
1818 }
1819
fc34caaa 1820 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
1821}
1822
5b4a0ec0
DG
1823/*
1824 * Copy data between a UST app session and a regular LTT session.
1825 */
421cb601 1826static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 1827 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 1828{
bec39940
DG
1829 struct lttng_ht_node_str *ua_chan_node;
1830 struct lttng_ht_iter iter;
48842b30
DG
1831 struct ltt_ust_channel *uchan;
1832 struct ust_app_channel *ua_chan;
477d7741
MD
1833 time_t rawtime;
1834 struct tm *timeinfo;
1835 char datetime[16];
1836 int ret;
d7ba1388 1837 char tmp_shm_path[PATH_MAX];
477d7741
MD
1838
1839 /* Get date and time for unique app path */
1840 time(&rawtime);
1841 timeinfo = localtime(&rawtime);
1842 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 1843
421cb601 1844 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 1845
7972aab2
DG
1846 ua_sess->tracing_id = usess->id;
1847 ua_sess->id = get_next_session_id();
1848 ua_sess->uid = app->uid;
1849 ua_sess->gid = app->gid;
1850 ua_sess->euid = usess->uid;
1851 ua_sess->egid = usess->gid;
1852 ua_sess->buffer_type = usess->buffer_type;
1853 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 1854
7972aab2 1855 /* There is only one consumer object per session possible. */
6addfa37 1856 consumer_output_get(usess->consumer);
7972aab2 1857 ua_sess->consumer = usess->consumer;
6addfa37 1858
2bba9e53 1859 ua_sess->output_traces = usess->output_traces;
ecc48a90 1860 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
1861 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
1862 &usess->metadata_attr);
7972aab2
DG
1863
1864 switch (ua_sess->buffer_type) {
1865 case LTTNG_BUFFER_PER_PID:
1866 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 1867 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
1868 datetime);
1869 break;
1870 case LTTNG_BUFFER_PER_UID:
1871 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
1872 DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long);
1873 break;
1874 default:
1875 assert(0);
1876 goto error;
1877 }
477d7741
MD
1878 if (ret < 0) {
1879 PERROR("asprintf UST shadow copy session");
477d7741 1880 assert(0);
7972aab2 1881 goto error;
477d7741
MD
1882 }
1883
3d071855
MD
1884 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
1885 sizeof(ua_sess->root_shm_path));
1886 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
1887 strncpy(ua_sess->shm_path, usess->shm_path,
1888 sizeof(ua_sess->shm_path));
1889 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1890 if (ua_sess->shm_path[0]) {
1891 switch (ua_sess->buffer_type) {
1892 case LTTNG_BUFFER_PER_PID:
1893 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1894 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
1895 app->name, app->pid, datetime);
1896 break;
1897 case LTTNG_BUFFER_PER_UID:
1898 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
1899 DEFAULT_UST_TRACE_UID_PATH,
1900 app->uid, app->bits_per_long);
1901 break;
1902 default:
1903 assert(0);
1904 goto error;
1905 }
1906 if (ret < 0) {
1907 PERROR("sprintf UST shadow copy session");
1908 assert(0);
1909 goto error;
1910 }
1911 strncat(ua_sess->shm_path, tmp_shm_path,
1912 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
1913 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
1914 }
1915
48842b30 1916 /* Iterate over all channels in global domain. */
bec39940
DG
1917 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1918 uchan, node.node) {
1919 struct lttng_ht_iter uiter;
ba767faf 1920
bec39940
DG
1921 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1922 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5b4a0ec0 1923 if (ua_chan_node != NULL) {
fc34caaa 1924 /* Session exist. Contiuing. */
5b4a0ec0
DG
1925 continue;
1926 }
421cb601 1927
5b4a0ec0
DG
1928 DBG2("Channel %s not found on shadow session copy, creating it",
1929 uchan->name);
fb83fe64
JD
1930 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess,
1931 &uchan->attr);
5b4a0ec0 1932 if (ua_chan == NULL) {
fc34caaa 1933 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
5b4a0ec0 1934 continue;
48842b30 1935 }
5b4a0ec0 1936 shadow_copy_channel(ua_chan, uchan);
ffe60014
DG
1937 /*
1938 * The concept of metadata channel does not exist on the tracing
1939 * registry side of the session daemon so this can only be a per CPU
1940 * channel and not metadata.
1941 */
1942 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1943
bec39940 1944 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
48842b30 1945 }
6addfa37 1946 return;
7972aab2
DG
1947
1948error:
6addfa37 1949 consumer_output_put(ua_sess->consumer);
48842b30
DG
1950}
1951
78f0bacd
DG
1952/*
1953 * Lookup sesison wrapper.
1954 */
84cd17c6
MD
1955static
1956void __lookup_session_by_app(struct ltt_ust_session *usess,
bec39940 1957 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
1958{
1959 /* Get right UST app session from app */
d9bf3ca4 1960 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
1961}
1962
421cb601
DG
1963/*
1964 * Return ust app session from the app session hashtable using the UST session
a991f516 1965 * id.
421cb601 1966 */
48842b30
DG
1967static struct ust_app_session *lookup_session_by_app(
1968 struct ltt_ust_session *usess, struct ust_app *app)
1969{
bec39940 1970 struct lttng_ht_iter iter;
d9bf3ca4 1971 struct lttng_ht_node_u64 *node;
48842b30 1972
84cd17c6 1973 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 1974 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
1975 if (node == NULL) {
1976 goto error;
1977 }
1978
1979 return caa_container_of(node, struct ust_app_session, node);
1980
1981error:
1982 return NULL;
1983}
1984
7972aab2
DG
1985/*
1986 * Setup buffer registry per PID for the given session and application. If none
1987 * is found, a new one is created, added to the global registry and
1988 * initialized. If regp is valid, it's set with the newly created object.
1989 *
1990 * Return 0 on success or else a negative value.
1991 */
1992static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
1993 struct ust_app *app, struct buffer_reg_pid **regp)
1994{
1995 int ret = 0;
1996 struct buffer_reg_pid *reg_pid;
1997
1998 assert(ua_sess);
1999 assert(app);
2000
2001 rcu_read_lock();
2002
2003 reg_pid = buffer_reg_pid_find(ua_sess->id);
2004 if (!reg_pid) {
2005 /*
2006 * This is the create channel path meaning that if there is NO
2007 * registry available, we have to create one for this session.
2008 */
d7ba1388 2009 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 2010 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2011 if (ret < 0) {
2012 goto error;
2013 }
7972aab2
DG
2014 } else {
2015 goto end;
2016 }
2017
2018 /* Initialize registry. */
2019 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
2020 app->bits_per_long, app->uint8_t_alignment,
2021 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2022 app->uint64_t_alignment, app->long_alignment,
2023 app->byte_order, app->version.major,
3d071855
MD
2024 app->version.minor, reg_pid->root_shm_path,
2025 reg_pid->shm_path,
d7ba1388 2026 ua_sess->euid, ua_sess->egid);
7972aab2 2027 if (ret < 0) {
286c991a
MD
2028 /*
2029 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2030 * destroy the buffer registry, because it is always expected
2031 * that if the buffer registry can be found, its ust registry is
2032 * non-NULL.
2033 */
2034 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
2035 goto error;
2036 }
2037
286c991a
MD
2038 buffer_reg_pid_add(reg_pid);
2039
7972aab2
DG
2040 DBG3("UST app buffer registry per PID created successfully");
2041
2042end:
2043 if (regp) {
2044 *regp = reg_pid;
2045 }
2046error:
2047 rcu_read_unlock();
2048 return ret;
2049}
2050
2051/*
2052 * Setup buffer registry per UID for the given session and application. If none
2053 * is found, a new one is created, added to the global registry and
2054 * initialized. If regp is valid, it's set with the newly created object.
2055 *
2056 * Return 0 on success or else a negative value.
2057 */
2058static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 2059 struct ust_app_session *ua_sess,
7972aab2
DG
2060 struct ust_app *app, struct buffer_reg_uid **regp)
2061{
2062 int ret = 0;
2063 struct buffer_reg_uid *reg_uid;
2064
2065 assert(usess);
2066 assert(app);
2067
2068 rcu_read_lock();
2069
2070 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2071 if (!reg_uid) {
2072 /*
2073 * This is the create channel path meaning that if there is NO
2074 * registry available, we have to create one for this session.
2075 */
2076 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
2077 LTTNG_DOMAIN_UST, &reg_uid,
2078 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2079 if (ret < 0) {
2080 goto error;
2081 }
7972aab2
DG
2082 } else {
2083 goto end;
2084 }
2085
2086 /* Initialize registry. */
af6142cf 2087 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
2088 app->bits_per_long, app->uint8_t_alignment,
2089 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2090 app->uint64_t_alignment, app->long_alignment,
2091 app->byte_order, app->version.major,
3d071855
MD
2092 app->version.minor, reg_uid->root_shm_path,
2093 reg_uid->shm_path, usess->uid, usess->gid);
7972aab2 2094 if (ret < 0) {
286c991a
MD
2095 /*
2096 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2097 * destroy the buffer registry, because it is always expected
2098 * that if the buffer registry can be found, its ust registry is
2099 * non-NULL.
2100 */
2101 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
2102 goto error;
2103 }
2104 /* Add node to teardown list of the session. */
2105 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2106
286c991a 2107 buffer_reg_uid_add(reg_uid);
7972aab2 2108
286c991a 2109 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
2110end:
2111 if (regp) {
2112 *regp = reg_uid;
2113 }
2114error:
2115 rcu_read_unlock();
2116 return ret;
2117}
2118
421cb601 2119/*
3d8ca23b 2120 * Create a session on the tracer side for the given app.
421cb601 2121 *
3d8ca23b
DG
2122 * On success, ua_sess_ptr is populated with the session pointer or else left
2123 * untouched. If the session was created, is_created is set to 1. On error,
2124 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2125 * be NULL.
2126 *
2127 * Returns 0 on success or else a negative code which is either -ENOMEM or
2128 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 2129 */
3d8ca23b
DG
2130static int create_ust_app_session(struct ltt_ust_session *usess,
2131 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2132 int *is_created)
421cb601 2133{
3d8ca23b 2134 int ret, created = 0;
421cb601
DG
2135 struct ust_app_session *ua_sess;
2136
3d8ca23b
DG
2137 assert(usess);
2138 assert(app);
2139 assert(ua_sess_ptr);
2140
840cb59c 2141 health_code_update();
86acf0da 2142
421cb601
DG
2143 ua_sess = lookup_session_by_app(usess, app);
2144 if (ua_sess == NULL) {
d9bf3ca4 2145 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2146 app->pid, usess->id);
d0b96690 2147 ua_sess = alloc_ust_app_session(app);
421cb601
DG
2148 if (ua_sess == NULL) {
2149 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2150 ret = -ENOMEM;
2151 goto error;
421cb601 2152 }
477d7741 2153 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2154 created = 1;
421cb601
DG
2155 }
2156
7972aab2
DG
2157 switch (usess->buffer_type) {
2158 case LTTNG_BUFFER_PER_PID:
2159 /* Init local registry. */
2160 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2161 if (ret < 0) {
e64207cf 2162 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2163 goto error;
2164 }
2165 break;
2166 case LTTNG_BUFFER_PER_UID:
2167 /* Look for a global registry. If none exists, create one. */
d7ba1388 2168 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2169 if (ret < 0) {
e64207cf 2170 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2171 goto error;
2172 }
2173 break;
2174 default:
2175 assert(0);
2176 ret = -EINVAL;
2177 goto error;
2178 }
2179
2180 health_code_update();
2181
2182 if (ua_sess->handle == -1) {
fb45065e 2183 pthread_mutex_lock(&app->sock_lock);
7972aab2 2184 ret = ustctl_create_session(app->sock);
fb45065e 2185 pthread_mutex_unlock(&app->sock_lock);
7972aab2
DG
2186 if (ret < 0) {
2187 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2188 ERR("Creating session for app pid %d with ret %d",
ffe60014
DG
2189 app->pid, ret);
2190 } else {
2191 DBG("UST app creating session failed. Application is dead");
3757b385
DG
2192 /*
2193 * This is normal behavior, an application can die during the
2194 * creation process. Don't report an error so the execution can
2195 * continue normally. This will get flagged ENOTCONN and the
2196 * caller will handle it.
2197 */
2198 ret = 0;
ffe60014 2199 }
d0b96690 2200 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2201 if (ret != -ENOMEM) {
2202 /*
2203 * Tracer is probably gone or got an internal error so let's
2204 * behave like it will soon unregister or not usable.
2205 */
2206 ret = -ENOTCONN;
2207 }
2208 goto error;
421cb601
DG
2209 }
2210
7972aab2
DG
2211 ua_sess->handle = ret;
2212
2213 /* Add ust app session to app's HT */
d9bf3ca4
MD
2214 lttng_ht_node_init_u64(&ua_sess->node,
2215 ua_sess->tracing_id);
2216 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
10b56aef
MD
2217 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2218 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2219 &ua_sess->ust_objd_node);
7972aab2
DG
2220
2221 DBG2("UST app session created successfully with handle %d", ret);
2222 }
2223
2224 *ua_sess_ptr = ua_sess;
2225 if (is_created) {
2226 *is_created = created;
2227 }
2228
2229 /* Everything went well. */
2230 ret = 0;
2231
2232error:
2233 health_code_update();
2234 return ret;
2235}
2236
6a6b2068
JG
2237/*
2238 * Match function for a hash table lookup of ust_app_ctx.
2239 *
2240 * It matches an ust app context based on the context type and, in the case
2241 * of perf counters, their name.
2242 */
2243static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2244{
2245 struct ust_app_ctx *ctx;
bdf64013 2246 const struct lttng_ust_context_attr *key;
6a6b2068
JG
2247
2248 assert(node);
2249 assert(_key);
2250
2251 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2252 key = _key;
2253
2254 /* Context type */
2255 if (ctx->ctx.ctx != key->ctx) {
2256 goto no_match;
2257 }
2258
bdf64013
JG
2259 switch(key->ctx) {
2260 case LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER:
6a6b2068 2261 if (strncmp(key->u.perf_counter.name,
bdf64013
JG
2262 ctx->ctx.u.perf_counter.name,
2263 sizeof(key->u.perf_counter.name))) {
2264 goto no_match;
2265 }
2266 break;
2267 case LTTNG_UST_CONTEXT_APP_CONTEXT:
2268 if (strcmp(key->u.app_ctx.provider_name,
2269 ctx->ctx.u.app_ctx.provider_name) ||
2270 strcmp(key->u.app_ctx.ctx_name,
2271 ctx->ctx.u.app_ctx.ctx_name)) {
6a6b2068
JG
2272 goto no_match;
2273 }
bdf64013
JG
2274 break;
2275 default:
2276 break;
6a6b2068
JG
2277 }
2278
2279 /* Match. */
2280 return 1;
2281
2282no_match:
2283 return 0;
2284}
2285
2286/*
2287 * Lookup for an ust app context from an lttng_ust_context.
2288 *
be184a0f 2289 * Must be called while holding RCU read side lock.
6a6b2068
JG
2290 * Return an ust_app_ctx object or NULL on error.
2291 */
2292static
2293struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
bdf64013 2294 struct lttng_ust_context_attr *uctx)
6a6b2068
JG
2295{
2296 struct lttng_ht_iter iter;
2297 struct lttng_ht_node_ulong *node;
2298 struct ust_app_ctx *app_ctx = NULL;
2299
2300 assert(uctx);
2301 assert(ht);
2302
2303 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2304 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2305 ht_match_ust_app_ctx, uctx, &iter.iter);
2306 node = lttng_ht_iter_get_node_ulong(&iter);
2307 if (!node) {
2308 goto end;
2309 }
2310
2311 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2312
2313end:
2314 return app_ctx;
2315}
2316
7972aab2
DG
2317/*
2318 * Create a context for the channel on the tracer.
2319 *
2320 * Called with UST app session lock held and a RCU read side lock.
2321 */
2322static
2323int create_ust_app_channel_context(struct ust_app_session *ua_sess,
bdf64013
JG
2324 struct ust_app_channel *ua_chan,
2325 struct lttng_ust_context_attr *uctx,
7972aab2
DG
2326 struct ust_app *app)
2327{
2328 int ret = 0;
7972aab2
DG
2329 struct ust_app_ctx *ua_ctx;
2330
2331 DBG2("UST app adding context to channel %s", ua_chan->name);
2332
6a6b2068
JG
2333 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2334 if (ua_ctx) {
7972aab2
DG
2335 ret = -EEXIST;
2336 goto error;
2337 }
2338
2339 ua_ctx = alloc_ust_app_ctx(uctx);
2340 if (ua_ctx == NULL) {
2341 /* malloc failed */
2342 ret = -1;
2343 goto error;
2344 }
2345
2346 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2347 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2348 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2349
2350 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2351 if (ret < 0) {
2352 goto error;
2353 }
2354
2355error:
2356 return ret;
2357}
2358
2359/*
2360 * Enable on the tracer side a ust app event for the session and channel.
2361 *
2362 * Called with UST app session lock held.
2363 */
2364static
2365int enable_ust_app_event(struct ust_app_session *ua_sess,
2366 struct ust_app_event *ua_event, struct ust_app *app)
2367{
2368 int ret;
2369
2370 ret = enable_ust_event(app, ua_sess, ua_event);
2371 if (ret < 0) {
2372 goto error;
2373 }
2374
2375 ua_event->enabled = 1;
2376
2377error:
2378 return ret;
2379}
2380
2381/*
2382 * Disable on the tracer side a ust app event for the session and channel.
2383 */
2384static int disable_ust_app_event(struct ust_app_session *ua_sess,
2385 struct ust_app_event *ua_event, struct ust_app *app)
2386{
2387 int ret;
2388
2389 ret = disable_ust_event(app, ua_sess, ua_event);
2390 if (ret < 0) {
2391 goto error;
2392 }
2393
2394 ua_event->enabled = 0;
2395
2396error:
2397 return ret;
2398}
2399
2400/*
2401 * Lookup ust app channel for session and disable it on the tracer side.
2402 */
2403static
2404int disable_ust_app_channel(struct ust_app_session *ua_sess,
2405 struct ust_app_channel *ua_chan, struct ust_app *app)
2406{
2407 int ret;
2408
2409 ret = disable_ust_channel(app, ua_sess, ua_chan);
2410 if (ret < 0) {
2411 goto error;
2412 }
2413
2414 ua_chan->enabled = 0;
2415
2416error:
2417 return ret;
2418}
2419
2420/*
2421 * Lookup ust app channel for session and enable it on the tracer side. This
2422 * MUST be called with a RCU read side lock acquired.
2423 */
2424static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2425 struct ltt_ust_channel *uchan, struct ust_app *app)
2426{
2427 int ret = 0;
2428 struct lttng_ht_iter iter;
2429 struct lttng_ht_node_str *ua_chan_node;
2430 struct ust_app_channel *ua_chan;
2431
2432 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2433 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2434 if (ua_chan_node == NULL) {
d9bf3ca4 2435 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2436 uchan->name, ua_sess->tracing_id);
2437 goto error;
2438 }
2439
2440 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2441
2442 ret = enable_ust_channel(app, ua_sess, ua_chan);
2443 if (ret < 0) {
2444 goto error;
2445 }
2446
2447error:
2448 return ret;
2449}
2450
2451/*
2452 * Ask the consumer to create a channel and get it if successful.
2453 *
2454 * Return 0 on success or else a negative value.
2455 */
2456static int do_consumer_create_channel(struct ltt_ust_session *usess,
2457 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
2458 int bitness, struct ust_registry_session *registry)
2459{
2460 int ret;
2461 unsigned int nb_fd = 0;
2462 struct consumer_socket *socket;
2463
2464 assert(usess);
2465 assert(ua_sess);
2466 assert(ua_chan);
2467 assert(registry);
2468
2469 rcu_read_lock();
2470 health_code_update();
2471
2472 /* Get the right consumer socket for the application. */
2473 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2474 if (!socket) {
2475 ret = -EINVAL;
2476 goto error;
2477 }
2478
2479 health_code_update();
2480
2481 /* Need one fd for the channel. */
2482 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2483 if (ret < 0) {
2484 ERR("Exhausted number of available FD upon create channel");
2485 goto error;
2486 }
2487
2488 /*
2489 * Ask consumer to create channel. The consumer will return the number of
2490 * stream we have to expect.
2491 */
2492 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
2493 registry);
2494 if (ret < 0) {
2495 goto error_ask;
2496 }
2497
2498 /*
2499 * Compute the number of fd needed before receiving them. It must be 2 per
2500 * stream (2 being the default value here).
2501 */
2502 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2503
2504 /* Reserve the amount of file descriptor we need. */
2505 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2506 if (ret < 0) {
2507 ERR("Exhausted number of available FD upon create channel");
2508 goto error_fd_get_stream;
2509 }
2510
2511 health_code_update();
2512
2513 /*
2514 * Now get the channel from the consumer. This call wil populate the stream
2515 * list of that channel and set the ust objects.
2516 */
d9078d0c
DG
2517 if (usess->consumer->enabled) {
2518 ret = ust_consumer_get_channel(socket, ua_chan);
2519 if (ret < 0) {
2520 goto error_destroy;
2521 }
7972aab2
DG
2522 }
2523
2524 rcu_read_unlock();
2525 return 0;
2526
2527error_destroy:
2528 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2529error_fd_get_stream:
2530 /*
2531 * Initiate a destroy channel on the consumer since we had an error
2532 * handling it on our side. The return value is of no importance since we
2533 * already have a ret value set by the previous error that we need to
2534 * return.
2535 */
2536 (void) ust_consumer_destroy_channel(socket, ua_chan);
2537error_ask:
2538 lttng_fd_put(LTTNG_FD_APPS, 1);
2539error:
2540 health_code_update();
2541 rcu_read_unlock();
2542 return ret;
2543}
2544
2545/*
2546 * Duplicate the ust data object of the ust app stream and save it in the
2547 * buffer registry stream.
2548 *
2549 * Return 0 on success or else a negative value.
2550 */
2551static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2552 struct ust_app_stream *stream)
2553{
2554 int ret;
2555
2556 assert(reg_stream);
2557 assert(stream);
2558
2559 /* Reserve the amount of file descriptor we need. */
2560 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2561 if (ret < 0) {
2562 ERR("Exhausted number of available FD upon duplicate stream");
2563 goto error;
2564 }
2565
2566 /* Duplicate object for stream once the original is in the registry. */
2567 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2568 reg_stream->obj.ust);
2569 if (ret < 0) {
2570 ERR("Duplicate stream obj from %p to %p failed with ret %d",
2571 reg_stream->obj.ust, stream->obj, ret);
2572 lttng_fd_put(LTTNG_FD_APPS, 2);
2573 goto error;
2574 }
2575 stream->handle = stream->obj->handle;
2576
2577error:
2578 return ret;
2579}
2580
2581/*
2582 * Duplicate the ust data object of the ust app. channel and save it in the
2583 * buffer registry channel.
2584 *
2585 * Return 0 on success or else a negative value.
2586 */
2587static int duplicate_channel_object(struct buffer_reg_channel *reg_chan,
2588 struct ust_app_channel *ua_chan)
2589{
2590 int ret;
2591
2592 assert(reg_chan);
2593 assert(ua_chan);
2594
2595 /* Need two fds for the channel. */
2596 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2597 if (ret < 0) {
2598 ERR("Exhausted number of available FD upon duplicate channel");
2599 goto error_fd_get;
2600 }
2601
2602 /* Duplicate object for stream once the original is in the registry. */
2603 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust);
2604 if (ret < 0) {
2605 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
2606 reg_chan->obj.ust, ua_chan->obj, ret);
2607 goto error;
2608 }
2609 ua_chan->handle = ua_chan->obj->handle;
2610
2611 return 0;
2612
2613error:
2614 lttng_fd_put(LTTNG_FD_APPS, 1);
2615error_fd_get:
2616 return ret;
2617}
2618
2619/*
2620 * For a given channel buffer registry, setup all streams of the given ust
2621 * application channel.
2622 *
2623 * Return 0 on success or else a negative value.
2624 */
2625static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan,
fb45065e
MD
2626 struct ust_app_channel *ua_chan,
2627 struct ust_app *app)
7972aab2
DG
2628{
2629 int ret = 0;
2630 struct ust_app_stream *stream, *stmp;
2631
2632 assert(reg_chan);
2633 assert(ua_chan);
2634
2635 DBG2("UST app setup buffer registry stream");
2636
2637 /* Send all streams to application. */
2638 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
2639 struct buffer_reg_stream *reg_stream;
2640
2641 ret = buffer_reg_stream_create(&reg_stream);
2642 if (ret < 0) {
2643 goto error;
2644 }
2645
2646 /*
2647 * Keep original pointer and nullify it in the stream so the delete
2648 * stream call does not release the object.
2649 */
2650 reg_stream->obj.ust = stream->obj;
2651 stream->obj = NULL;
2652 buffer_reg_stream_add(reg_stream, reg_chan);
421cb601 2653
7972aab2
DG
2654 /* We don't need the streams anymore. */
2655 cds_list_del(&stream->list);
fb45065e 2656 delete_ust_app_stream(-1, stream, app);
7972aab2 2657 }
421cb601 2658
7972aab2
DG
2659error:
2660 return ret;
2661}
2662
2663/*
2664 * Create a buffer registry channel for the given session registry and
2665 * application channel object. If regp pointer is valid, it's set with the
2666 * created object. Important, the created object is NOT added to the session
2667 * registry hash table.
2668 *
2669 * Return 0 on success else a negative value.
2670 */
2671static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
2672 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
2673{
2674 int ret;
2675 struct buffer_reg_channel *reg_chan = NULL;
2676
2677 assert(reg_sess);
2678 assert(ua_chan);
2679
2680 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
2681
2682 /* Create buffer registry channel. */
2683 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &reg_chan);
2684 if (ret < 0) {
2685 goto error_create;
421cb601 2686 }
7972aab2
DG
2687 assert(reg_chan);
2688 reg_chan->consumer_key = ua_chan->key;
8c924c7b 2689 reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
d07ceecd 2690 reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 2691
7972aab2
DG
2692 /* Create and add a channel registry to session. */
2693 ret = ust_registry_channel_add(reg_sess->reg.ust,
2694 ua_chan->tracing_channel_id);
2695 if (ret < 0) {
2696 goto error;
d88aee68 2697 }
7972aab2 2698 buffer_reg_channel_add(reg_sess, reg_chan);
d88aee68 2699
7972aab2
DG
2700 if (regp) {
2701 *regp = reg_chan;
3d8ca23b 2702 }
d88aee68 2703
7972aab2 2704 return 0;
3d8ca23b
DG
2705
2706error:
7972aab2
DG
2707 /* Safe because the registry channel object was not added to any HT. */
2708 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
2709error_create:
3d8ca23b 2710 return ret;
421cb601
DG
2711}
2712
55cc08a6 2713/*
7972aab2
DG
2714 * Setup buffer registry channel for the given session registry and application
2715 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 2716 *
7972aab2 2717 * Return 0 on success else a negative value.
55cc08a6 2718 */
7972aab2 2719static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
fb45065e
MD
2720 struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan,
2721 struct ust_app *app)
55cc08a6 2722{
7972aab2 2723 int ret;
55cc08a6 2724
7972aab2
DG
2725 assert(reg_sess);
2726 assert(reg_chan);
2727 assert(ua_chan);
2728 assert(ua_chan->obj);
55cc08a6 2729
7972aab2 2730 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 2731
7972aab2 2732 /* Setup all streams for the registry. */
fb45065e 2733 ret = setup_buffer_reg_streams(reg_chan, ua_chan, app);
7972aab2 2734 if (ret < 0) {
55cc08a6
DG
2735 goto error;
2736 }
2737
7972aab2
DG
2738 reg_chan->obj.ust = ua_chan->obj;
2739 ua_chan->obj = NULL;
55cc08a6 2740
7972aab2 2741 return 0;
55cc08a6
DG
2742
2743error:
7972aab2
DG
2744 buffer_reg_channel_remove(reg_sess, reg_chan);
2745 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
2746 return ret;
2747}
2748
edb67388 2749/*
7972aab2 2750 * Send buffer registry channel to the application.
d0b96690 2751 *
7972aab2 2752 * Return 0 on success else a negative value.
edb67388 2753 */
7972aab2
DG
2754static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan,
2755 struct ust_app *app, struct ust_app_session *ua_sess,
2756 struct ust_app_channel *ua_chan)
edb67388
DG
2757{
2758 int ret;
7972aab2 2759 struct buffer_reg_stream *reg_stream;
edb67388 2760
7972aab2
DG
2761 assert(reg_chan);
2762 assert(app);
2763 assert(ua_sess);
2764 assert(ua_chan);
2765
2766 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
2767
2768 ret = duplicate_channel_object(reg_chan, ua_chan);
edb67388
DG
2769 if (ret < 0) {
2770 goto error;
2771 }
2772
7972aab2
DG
2773 /* Send channel to the application. */
2774 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
2775 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2776 ret = -ENOTCONN; /* Caused by app exiting. */
2777 goto error;
2778 } else if (ret < 0) {
7972aab2
DG
2779 goto error;
2780 }
2781
2782 health_code_update();
2783
2784 /* Send all streams to application. */
2785 pthread_mutex_lock(&reg_chan->stream_list_lock);
2786 cds_list_for_each_entry(reg_stream, &reg_chan->streams, lnode) {
2787 struct ust_app_stream stream;
2788
2789 ret = duplicate_stream_object(reg_stream, &stream);
2790 if (ret < 0) {
2791 goto error_stream_unlock;
2792 }
2793
2794 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
2795 if (ret < 0) {
fb45065e 2796 (void) release_ust_app_stream(-1, &stream, app);
a7169585
MD
2797 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2798 ret = -ENOTCONN; /* Caused by app exiting. */
2799 goto error_stream_unlock;
2800 } else if (ret < 0) {
2801 goto error_stream_unlock;
2802 }
7972aab2
DG
2803 goto error_stream_unlock;
2804 }
edb67388 2805
7972aab2
DG
2806 /*
2807 * The return value is not important here. This function will output an
2808 * error if needed.
2809 */
fb45065e 2810 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
2811 }
2812 ua_chan->is_sent = 1;
2813
2814error_stream_unlock:
2815 pthread_mutex_unlock(&reg_chan->stream_list_lock);
edb67388
DG
2816error:
2817 return ret;
2818}
2819
9730260e 2820/*
7972aab2
DG
2821 * Create and send to the application the created buffers with per UID buffers.
2822 *
2823 * Return 0 on success else a negative value.
9730260e 2824 */
7972aab2
DG
2825static int create_channel_per_uid(struct ust_app *app,
2826 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2827 struct ust_app_channel *ua_chan)
9730260e
DG
2828{
2829 int ret;
7972aab2
DG
2830 struct buffer_reg_uid *reg_uid;
2831 struct buffer_reg_channel *reg_chan;
9730260e 2832
7972aab2
DG
2833 assert(app);
2834 assert(usess);
2835 assert(ua_sess);
2836 assert(ua_chan);
2837
2838 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
2839
2840 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2841 /*
2842 * The session creation handles the creation of this global registry
2843 * object. If none can be find, there is a code flow problem or a
2844 * teardown race.
2845 */
2846 assert(reg_uid);
2847
2848 reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
2849 reg_uid);
2850 if (!reg_chan) {
2851 /* Create the buffer registry channel object. */
2852 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &reg_chan);
2853 if (ret < 0) {
f14256d6
MD
2854 ERR("Error creating the UST channel \"%s\" registry instance",
2855 ua_chan->name);
7972aab2
DG
2856 goto error;
2857 }
2858 assert(reg_chan);
2859
2860 /*
2861 * Create the buffers on the consumer side. This call populates the
2862 * ust app channel object with all streams and data object.
2863 */
2864 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2865 app->bits_per_long, reg_uid->registry->reg.ust);
2866 if (ret < 0) {
f14256d6
MD
2867 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2868 ua_chan->name);
2869
07d2ae95
DG
2870 /*
2871 * Let's remove the previously created buffer registry channel so
2872 * it's not visible anymore in the session registry.
2873 */
2874 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
2875 ua_chan->tracing_channel_id);
2876 buffer_reg_channel_remove(reg_uid->registry, reg_chan);
2877 buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST);
7972aab2
DG
2878 goto error;
2879 }
2880
2881 /*
2882 * Setup the streams and add it to the session registry.
2883 */
fb45065e
MD
2884 ret = setup_buffer_reg_channel(reg_uid->registry,
2885 ua_chan, reg_chan, app);
7972aab2 2886 if (ret < 0) {
f14256d6
MD
2887 ERR("Error setting up UST channel \"%s\"",
2888 ua_chan->name);
7972aab2
DG
2889 goto error;
2890 }
2891
2892 }
2893
2894 /* Send buffers to the application. */
2895 ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan);
9730260e 2896 if (ret < 0) {
a7169585
MD
2897 if (ret != -ENOTCONN) {
2898 ERR("Error sending channel to application");
2899 }
9730260e
DG
2900 goto error;
2901 }
2902
9730260e
DG
2903error:
2904 return ret;
2905}
2906
78f0bacd 2907/*
7972aab2
DG
2908 * Create and send to the application the created buffers with per PID buffers.
2909 *
2910 * Return 0 on success else a negative value.
78f0bacd 2911 */
7972aab2
DG
2912static int create_channel_per_pid(struct ust_app *app,
2913 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2914 struct ust_app_channel *ua_chan)
78f0bacd 2915{
8535a6d9 2916 int ret;
7972aab2 2917 struct ust_registry_session *registry;
78f0bacd 2918
7972aab2
DG
2919 assert(app);
2920 assert(usess);
2921 assert(ua_sess);
2922 assert(ua_chan);
2923
2924 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
2925
2926 rcu_read_lock();
2927
2928 registry = get_session_registry(ua_sess);
2929 assert(registry);
2930
2931 /* Create and add a new channel registry to session. */
2932 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 2933 if (ret < 0) {
f14256d6
MD
2934 ERR("Error creating the UST channel \"%s\" registry instance",
2935 ua_chan->name);
78f0bacd
DG
2936 goto error;
2937 }
2938
7972aab2
DG
2939 /* Create and get channel on the consumer side. */
2940 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
2941 app->bits_per_long, registry);
2942 if (ret < 0) {
f14256d6
MD
2943 ERR("Error creating UST channel \"%s\" on the consumer daemon",
2944 ua_chan->name);
7972aab2
DG
2945 goto error;
2946 }
2947
2948 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
2949 if (ret < 0) {
a7169585
MD
2950 if (ret != -ENOTCONN) {
2951 ERR("Error sending channel to application");
2952 }
7972aab2
DG
2953 goto error;
2954 }
8535a6d9 2955
78f0bacd 2956error:
7972aab2 2957 rcu_read_unlock();
78f0bacd
DG
2958 return ret;
2959}
2960
2961/*
7972aab2
DG
2962 * From an already allocated ust app channel, create the channel buffers if
2963 * need and send it to the application. This MUST be called with a RCU read
2964 * side lock acquired.
2965 *
a7169585
MD
2966 * Return 0 on success or else a negative value. Returns -ENOTCONN if
2967 * the application exited concurrently.
78f0bacd 2968 */
7972aab2
DG
2969static int do_create_channel(struct ust_app *app,
2970 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
2971 struct ust_app_channel *ua_chan)
78f0bacd 2972{
7972aab2 2973 int ret;
78f0bacd 2974
7972aab2
DG
2975 assert(app);
2976 assert(usess);
2977 assert(ua_sess);
2978 assert(ua_chan);
2979
2980 /* Handle buffer type before sending the channel to the application. */
2981 switch (usess->buffer_type) {
2982 case LTTNG_BUFFER_PER_UID:
2983 {
2984 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
2985 if (ret < 0) {
2986 goto error;
2987 }
2988 break;
2989 }
2990 case LTTNG_BUFFER_PER_PID:
2991 {
2992 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
2993 if (ret < 0) {
2994 goto error;
2995 }
2996 break;
2997 }
2998 default:
2999 assert(0);
3000 ret = -EINVAL;
78f0bacd
DG
3001 goto error;
3002 }
3003
7972aab2
DG
3004 /* Initialize ust objd object using the received handle and add it. */
3005 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3006 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 3007
7972aab2
DG
3008 /* If channel is not enabled, disable it on the tracer */
3009 if (!ua_chan->enabled) {
3010 ret = disable_ust_channel(app, ua_sess, ua_chan);
3011 if (ret < 0) {
3012 goto error;
3013 }
78f0bacd
DG
3014 }
3015
3016error:
3017 return ret;
3018}
3019
284d8f55 3020/*
4d710ac2
DG
3021 * Create UST app channel and create it on the tracer. Set ua_chanp of the
3022 * newly created channel if not NULL.
d0b96690 3023 *
36b588ed 3024 * Called with UST app session lock and RCU read-side lock held.
7972aab2 3025 *
a7169585
MD
3026 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3027 * the application exited concurrently.
284d8f55 3028 */
4d710ac2
DG
3029static int create_ust_app_channel(struct ust_app_session *ua_sess,
3030 struct ltt_ust_channel *uchan, struct ust_app *app,
7972aab2 3031 enum lttng_ust_chan_type type, struct ltt_ust_session *usess,
4d710ac2 3032 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
3033{
3034 int ret = 0;
bec39940
DG
3035 struct lttng_ht_iter iter;
3036 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
3037 struct ust_app_channel *ua_chan;
3038
3039 /* Lookup channel in the ust app session */
bec39940
DG
3040 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3041 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 3042 if (ua_chan_node != NULL) {
5b4a0ec0 3043 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 3044 goto end;
5b4a0ec0
DG
3045 }
3046
d0b96690 3047 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
3048 if (ua_chan == NULL) {
3049 /* Only malloc can fail here */
4d710ac2 3050 ret = -ENOMEM;
094d1690 3051 goto error_alloc;
fc34caaa
DG
3052 }
3053 shadow_copy_channel(ua_chan, uchan);
3054
ffe60014
DG
3055 /* Set channel type. */
3056 ua_chan->attr.type = type;
3057
7972aab2 3058 ret = do_create_channel(app, usess, ua_sess, ua_chan);
5b4a0ec0
DG
3059 if (ret < 0) {
3060 goto error;
3061 }
3062
fc34caaa 3063 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
852d0037 3064 app->pid);
fc34caaa 3065
d0b96690
DG
3066 /* Only add the channel if successful on the tracer side. */
3067 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3068
fc34caaa 3069end:
4d710ac2
DG
3070 if (ua_chanp) {
3071 *ua_chanp = ua_chan;
3072 }
3073
3074 /* Everything went well. */
3075 return 0;
5b4a0ec0
DG
3076
3077error:
d0b96690 3078 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
094d1690 3079error_alloc:
4d710ac2 3080 return ret;
5b4a0ec0
DG
3081}
3082
3083/*
3084 * Create UST app event and create it on the tracer side.
d0b96690
DG
3085 *
3086 * Called with ust app session mutex held.
5b4a0ec0 3087 */
edb67388
DG
3088static
3089int create_ust_app_event(struct ust_app_session *ua_sess,
3090 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3091 struct ust_app *app)
284d8f55 3092{
edb67388 3093 int ret = 0;
5b4a0ec0 3094 struct ust_app_event *ua_event;
284d8f55 3095
5b4a0ec0 3096 /* Get event node */
18eace3b 3097 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 3098 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 3099 if (ua_event != NULL) {
fc34caaa 3100 ret = -EEXIST;
edb67388
DG
3101 goto end;
3102 }
5b4a0ec0 3103
edb67388
DG
3104 /* Does not exist so create one */
3105 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3106 if (ua_event == NULL) {
3107 /* Only malloc can failed so something is really wrong */
3108 ret = -ENOMEM;
fc34caaa 3109 goto end;
5b4a0ec0 3110 }
edb67388 3111 shadow_copy_event(ua_event, uevent);
5b4a0ec0 3112
edb67388 3113 /* Create it on the tracer side */
5b4a0ec0 3114 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 3115 if (ret < 0) {
fc34caaa 3116 /* Not found previously means that it does not exist on the tracer */
76f66f63 3117 assert(ret != -LTTNG_UST_ERR_EXIST);
284d8f55
DG
3118 goto error;
3119 }
3120
d0b96690 3121 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 3122
fc34caaa 3123 DBG2("UST app create event %s for PID %d completed", ua_event->name,
852d0037 3124 app->pid);
7f79d3a1 3125
edb67388 3126end:
fc34caaa
DG
3127 return ret;
3128
5b4a0ec0 3129error:
fc34caaa 3130 /* Valid. Calling here is already in a read side lock */
fb45065e 3131 delete_ust_app_event(-1, ua_event, app);
edb67388 3132 return ret;
5b4a0ec0
DG
3133}
3134
3135/*
3136 * Create UST metadata and open it on the tracer side.
d0b96690 3137 *
7972aab2 3138 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
3139 */
3140static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 3141 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
3142{
3143 int ret = 0;
ffe60014 3144 struct ust_app_channel *metadata;
d88aee68 3145 struct consumer_socket *socket;
7972aab2 3146 struct ust_registry_session *registry;
5b4a0ec0 3147
ffe60014
DG
3148 assert(ua_sess);
3149 assert(app);
d88aee68 3150 assert(consumer);
5b4a0ec0 3151
7972aab2
DG
3152 registry = get_session_registry(ua_sess);
3153 assert(registry);
3154
ce34fcd0
MD
3155 pthread_mutex_lock(&registry->lock);
3156
1b532a60
DG
3157 /* Metadata already exists for this registry or it was closed previously */
3158 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3159 ret = 0;
3160 goto error;
5b4a0ec0
DG
3161 }
3162
ffe60014 3163 /* Allocate UST metadata */
d0b96690 3164 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3165 if (!metadata) {
3166 /* malloc() failed */
3167 ret = -ENOMEM;
3168 goto error;
3169 }
5b4a0ec0 3170
ad7a9107 3171 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3172
7972aab2
DG
3173 /* Need one fd for the channel. */
3174 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3175 if (ret < 0) {
3176 ERR("Exhausted number of available FD upon create metadata");
3177 goto error;
3178 }
3179
4dc3dfc5
DG
3180 /* Get the right consumer socket for the application. */
3181 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3182 if (!socket) {
3183 ret = -EINVAL;
3184 goto error_consumer;
3185 }
3186
331744e3
JD
3187 /*
3188 * Keep metadata key so we can identify it on the consumer side. Assign it
3189 * to the registry *before* we ask the consumer so we avoid the race of the
3190 * consumer requesting the metadata and the ask_channel call on our side
3191 * did not returned yet.
3192 */
3193 registry->metadata_key = metadata->key;
3194
d88aee68
DG
3195 /*
3196 * Ask the metadata channel creation to the consumer. The metadata object
3197 * will be created by the consumer and kept their. However, the stream is
3198 * never added or monitored until we do a first push metadata to the
3199 * consumer.
3200 */
7972aab2
DG
3201 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3202 registry);
d88aee68 3203 if (ret < 0) {
f2a444f1
DG
3204 /* Nullify the metadata key so we don't try to close it later on. */
3205 registry->metadata_key = 0;
d88aee68
DG
3206 goto error_consumer;
3207 }
3208
3209 /*
3210 * The setup command will make the metadata stream be sent to the relayd,
3211 * if applicable, and the thread managing the metadatas. This is important
3212 * because after this point, if an error occurs, the only way the stream
3213 * can be deleted is to be monitored in the consumer.
3214 */
7972aab2 3215 ret = consumer_setup_metadata(socket, metadata->key);
ffe60014 3216 if (ret < 0) {
f2a444f1
DG
3217 /* Nullify the metadata key so we don't try to close it later on. */
3218 registry->metadata_key = 0;
d88aee68 3219 goto error_consumer;
5b4a0ec0
DG
3220 }
3221
7972aab2
DG
3222 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3223 metadata->key, app->pid);
5b4a0ec0 3224
d88aee68 3225error_consumer:
b80f0b6c 3226 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 3227 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 3228error:
ce34fcd0 3229 pthread_mutex_unlock(&registry->lock);
ffe60014 3230 return ret;
5b4a0ec0
DG
3231}
3232
5b4a0ec0 3233/*
d88aee68
DG
3234 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3235 * acquired before calling this function.
5b4a0ec0
DG
3236 */
3237struct ust_app *ust_app_find_by_pid(pid_t pid)
3238{
d88aee68 3239 struct ust_app *app = NULL;
bec39940
DG
3240 struct lttng_ht_node_ulong *node;
3241 struct lttng_ht_iter iter;
5b4a0ec0 3242
bec39940
DG
3243 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3244 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
3245 if (node == NULL) {
3246 DBG2("UST app no found with pid %d", pid);
3247 goto error;
3248 }
5b4a0ec0
DG
3249
3250 DBG2("Found UST app by pid %d", pid);
3251
d88aee68 3252 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
3253
3254error:
d88aee68 3255 return app;
5b4a0ec0
DG
3256}
3257
d88aee68
DG
3258/*
3259 * Allocate and init an UST app object using the registration information and
3260 * the command socket. This is called when the command socket connects to the
3261 * session daemon.
3262 *
3263 * The object is returned on success or else NULL.
3264 */
d0b96690 3265struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 3266{
d0b96690
DG
3267 struct ust_app *lta = NULL;
3268
3269 assert(msg);
3270 assert(sock >= 0);
3271
3272 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 3273
173af62f
DG
3274 if ((msg->bits_per_long == 64 &&
3275 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3276 || (msg->bits_per_long == 32 &&
3277 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 3278 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
3279 "%d-bit long, but no consumerd for this size is available.\n",
3280 msg->name, msg->pid, msg->bits_per_long);
3281 goto error;
3f2c5fcc 3282 }
d0b96690 3283
5b4a0ec0
DG
3284 lta = zmalloc(sizeof(struct ust_app));
3285 if (lta == NULL) {
3286 PERROR("malloc");
d0b96690 3287 goto error;
5b4a0ec0
DG
3288 }
3289
3290 lta->ppid = msg->ppid;
3291 lta->uid = msg->uid;
3292 lta->gid = msg->gid;
d0b96690 3293
7753dea8 3294 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
3295 lta->uint8_t_alignment = msg->uint8_t_alignment;
3296 lta->uint16_t_alignment = msg->uint16_t_alignment;
3297 lta->uint32_t_alignment = msg->uint32_t_alignment;
3298 lta->uint64_t_alignment = msg->uint64_t_alignment;
3299 lta->long_alignment = msg->long_alignment;
3300 lta->byte_order = msg->byte_order;
3301
5b4a0ec0
DG
3302 lta->v_major = msg->major;
3303 lta->v_minor = msg->minor;
d9bf3ca4 3304 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d0b96690 3305 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
10b56aef 3306 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690 3307 lta->notify_sock = -1;
d88aee68
DG
3308
3309 /* Copy name and make sure it's NULL terminated. */
3310 strncpy(lta->name, msg->name, sizeof(lta->name));
3311 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3312
3313 /*
3314 * Before this can be called, when receiving the registration information,
3315 * the application compatibility is checked. So, at this point, the
3316 * application can work with this session daemon.
3317 */
d0b96690 3318 lta->compatible = 1;
5b4a0ec0 3319
852d0037 3320 lta->pid = msg->pid;
d0b96690 3321 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 3322 lta->sock = sock;
fb45065e 3323 pthread_mutex_init(&lta->sock_lock, NULL);
d0b96690 3324 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 3325
d42f20df 3326 CDS_INIT_LIST_HEAD(&lta->teardown_head);
d0b96690
DG
3327error:
3328 return lta;
3329}
3330
d88aee68
DG
3331/*
3332 * For a given application object, add it to every hash table.
3333 */
d0b96690
DG
3334void ust_app_add(struct ust_app *app)
3335{
3336 assert(app);
3337 assert(app->notify_sock >= 0);
3338
5b4a0ec0 3339 rcu_read_lock();
852d0037
DG
3340
3341 /*
3342 * On a re-registration, we want to kick out the previous registration of
3343 * that pid
3344 */
d0b96690 3345 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
3346
3347 /*
3348 * The socket _should_ be unique until _we_ call close. So, a add_unique
3349 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3350 * already in the table.
3351 */
d0b96690 3352 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 3353
d0b96690
DG
3354 /* Add application to the notify socket hash table. */
3355 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3356 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 3357
d0b96690 3358 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
d88aee68
DG
3359 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3360 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3361 app->v_minor);
5b4a0ec0 3362
d0b96690
DG
3363 rcu_read_unlock();
3364}
3365
d88aee68
DG
3366/*
3367 * Set the application version into the object.
3368 *
3369 * Return 0 on success else a negative value either an errno code or a
3370 * LTTng-UST error code.
3371 */
d0b96690
DG
3372int ust_app_version(struct ust_app *app)
3373{
d88aee68
DG
3374 int ret;
3375
d0b96690 3376 assert(app);
d88aee68 3377
fb45065e 3378 pthread_mutex_lock(&app->sock_lock);
d88aee68 3379 ret = ustctl_tracer_version(app->sock, &app->version);
fb45065e 3380 pthread_mutex_unlock(&app->sock_lock);
d88aee68
DG
3381 if (ret < 0) {
3382 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
5368d366 3383 ERR("UST app %d version failed with ret %d", app->sock, ret);
d88aee68 3384 } else {
5368d366 3385 DBG3("UST app %d version failed. Application is dead", app->sock);
d88aee68
DG
3386 }
3387 }
3388
3389 return ret;
5b4a0ec0
DG
3390}
3391
3392/*
3393 * Unregister app by removing it from the global traceable app list and freeing
3394 * the data struct.
3395 *
3396 * The socket is already closed at this point so no close to sock.
3397 */
3398void ust_app_unregister(int sock)
3399{
3400 struct ust_app *lta;
bec39940 3401 struct lttng_ht_node_ulong *node;
c4b88406 3402 struct lttng_ht_iter ust_app_sock_iter;
bec39940 3403 struct lttng_ht_iter iter;
d42f20df 3404 struct ust_app_session *ua_sess;
525b0740 3405 int ret;
5b4a0ec0
DG
3406
3407 rcu_read_lock();
886459c6 3408
5b4a0ec0 3409 /* Get the node reference for a call_rcu */
c4b88406
MD
3410 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
3411 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
d0b96690 3412 assert(node);
284d8f55 3413
852d0037 3414 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
3415 DBG("PID %d unregistering with sock %d", lta->pid, sock);
3416
d88aee68 3417 /*
ce34fcd0
MD
3418 * For per-PID buffers, perform "push metadata" and flush all
3419 * application streams before removing app from hash tables,
3420 * ensuring proper behavior of data_pending check.
c4b88406 3421 * Remove sessions so they are not visible during deletion.
d88aee68 3422 */
d42f20df
DG
3423 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
3424 node.node) {
7972aab2
DG
3425 struct ust_registry_session *registry;
3426
d42f20df
DG
3427 ret = lttng_ht_del(lta->sessions, &iter);
3428 if (ret) {
3429 /* The session was already removed so scheduled for teardown. */
3430 continue;
3431 }
3432
ce34fcd0
MD
3433 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
3434 (void) ust_app_flush_app_session(lta, ua_sess);
3435 }
c4b88406 3436
d42f20df
DG
3437 /*
3438 * Add session to list for teardown. This is safe since at this point we
3439 * are the only one using this list.
3440 */
d88aee68
DG
3441 pthread_mutex_lock(&ua_sess->lock);
3442
b161602a
MD
3443 if (ua_sess->deleted) {
3444 pthread_mutex_unlock(&ua_sess->lock);
3445 continue;
3446 }
3447
d88aee68
DG
3448 /*
3449 * Normally, this is done in the delete session process which is
3450 * executed in the call rcu below. However, upon registration we can't
3451 * afford to wait for the grace period before pushing data or else the
3452 * data pending feature can race between the unregistration and stop
3453 * command where the data pending command is sent *before* the grace
3454 * period ended.
3455 *
3456 * The close metadata below nullifies the metadata pointer in the
3457 * session so the delete session will NOT push/close a second time.
3458 */
7972aab2 3459 registry = get_session_registry(ua_sess);
ce34fcd0 3460 if (registry) {
7972aab2
DG
3461 /* Push metadata for application before freeing the application. */
3462 (void) push_metadata(registry, ua_sess->consumer);
3463
3464 /*
3465 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
3466 * metadata only on destroy trace session in this case. Also, the
3467 * previous push metadata could have flag the metadata registry to
3468 * close so don't send a close command if closed.
7972aab2 3469 */
ce34fcd0 3470 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
3471 /* And ask to close it for this session registry. */
3472 (void) close_metadata(registry, ua_sess->consumer);
3473 }
3474 }
d42f20df 3475 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
c4b88406 3476
d88aee68 3477 pthread_mutex_unlock(&ua_sess->lock);
d42f20df
DG
3478 }
3479
c4b88406
MD
3480 /* Remove application from PID hash table */
3481 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
3482 assert(!ret);
3483
3484 /*
3485 * Remove application from notify hash table. The thread handling the
3486 * notify socket could have deleted the node so ignore on error because
3487 * either way it's valid. The close of that socket is handled by the other
3488 * thread.
3489 */
3490 iter.iter.node = &lta->notify_sock_n.node;
3491 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3492
3493 /*
3494 * Ignore return value since the node might have been removed before by an
3495 * add replace during app registration because the PID can be reassigned by
3496 * the OS.
3497 */
3498 iter.iter.node = &lta->pid_n.node;
3499 ret = lttng_ht_del(ust_app_ht, &iter);
3500 if (ret) {
3501 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
3502 lta->pid);
3503 }
3504
852d0037
DG
3505 /* Free memory */
3506 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
3507
5b4a0ec0
DG
3508 rcu_read_unlock();
3509 return;
284d8f55
DG
3510}
3511
5b4a0ec0
DG
3512/*
3513 * Fill events array with all events name of all registered apps.
3514 */
3515int ust_app_list_events(struct lttng_event **events)
421cb601 3516{
5b4a0ec0
DG
3517 int ret, handle;
3518 size_t nbmem, count = 0;
bec39940 3519 struct lttng_ht_iter iter;
5b4a0ec0 3520 struct ust_app *app;
c617c0c6 3521 struct lttng_event *tmp_event;
421cb601 3522
5b4a0ec0 3523 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3524 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
3525 if (tmp_event == NULL) {
5b4a0ec0
DG
3526 PERROR("zmalloc ust app events");
3527 ret = -ENOMEM;
421cb601
DG
3528 goto error;
3529 }
3530
5b4a0ec0 3531 rcu_read_lock();
421cb601 3532
852d0037 3533 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
90eaa0d2 3534 struct lttng_ust_tracepoint_iter uiter;
ac3bd9c0 3535
840cb59c 3536 health_code_update();
86acf0da 3537
e0c7ec2b
DG
3538 if (!app->compatible) {
3539 /*
3540 * TODO: In time, we should notice the caller of this error by
3541 * telling him that this is a version error.
3542 */
3543 continue;
3544 }
fb45065e 3545 pthread_mutex_lock(&app->sock_lock);
852d0037 3546 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0 3547 if (handle < 0) {
ffe60014
DG
3548 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3549 ERR("UST app list events getting handle failed for app pid %d",
3550 app->pid);
3551 }
fb45065e 3552 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3553 continue;
3554 }
421cb601 3555
852d0037 3556 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 3557 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3558 /* Handle ustctl error. */
3559 if (ret < 0) {
fb45065e
MD
3560 int release_ret;
3561
a2ba1ab0 3562 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3563 ERR("UST app tp list get failed for app %d with ret %d",
3564 app->sock, ret);
3565 } else {
3566 DBG3("UST app tp list get failed. Application is dead");
3757b385
DG
3567 /*
3568 * This is normal behavior, an application can die during the
3569 * creation process. Don't report an error so the execution can
3570 * continue normally. Continue normal execution.
3571 */
3572 break;
ffe60014 3573 }
98f595d4 3574 free(tmp_event);
fb45065e 3575 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3576 if (release_ret < 0 &&
3577 release_ret != -LTTNG_UST_ERR_EXITING &&
3578 release_ret != -EPIPE) {
fb45065e
MD
3579 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3580 }
3581 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
3582 goto rcu_error;
3583 }
3584
840cb59c 3585 health_code_update();
815564d8 3586 if (count >= nbmem) {
d7b3776f 3587 /* In case the realloc fails, we free the memory */
53efb85a
MD
3588 struct lttng_event *new_tmp_event;
3589 size_t new_nbmem;
3590
3591 new_nbmem = nbmem << 1;
3592 DBG2("Reallocating event list from %zu to %zu entries",
3593 nbmem, new_nbmem);
3594 new_tmp_event = realloc(tmp_event,
3595 new_nbmem * sizeof(struct lttng_event));
3596 if (new_tmp_event == NULL) {
fb45065e
MD
3597 int release_ret;
3598
5b4a0ec0 3599 PERROR("realloc ust app events");
c617c0c6 3600 free(tmp_event);
5b4a0ec0 3601 ret = -ENOMEM;
fb45065e 3602 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
3603 if (release_ret < 0 &&
3604 release_ret != -LTTNG_UST_ERR_EXITING &&
3605 release_ret != -EPIPE) {
fb45065e
MD
3606 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3607 }
3608 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
3609 goto rcu_error;
3610 }
53efb85a
MD
3611 /* Zero the new memory */
3612 memset(new_tmp_event + nbmem, 0,
3613 (new_nbmem - nbmem) * sizeof(struct lttng_event));
3614 nbmem = new_nbmem;
3615 tmp_event = new_tmp_event;
5b4a0ec0 3616 }
c617c0c6
MD
3617 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
3618 tmp_event[count].loglevel = uiter.loglevel;
3619 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
3620 tmp_event[count].pid = app->pid;
3621 tmp_event[count].enabled = -1;
5b4a0ec0 3622 count++;
421cb601 3623 }
fb45065e
MD
3624 ret = ustctl_release_handle(app->sock, handle);
3625 pthread_mutex_unlock(&app->sock_lock);
68313703 3626 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
fb45065e
MD
3627 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3628 }
421cb601
DG
3629 }
3630
5b4a0ec0 3631 ret = count;
c617c0c6 3632 *events = tmp_event;
421cb601 3633
5b4a0ec0 3634 DBG2("UST app list events done (%zu events)", count);
421cb601 3635
5b4a0ec0
DG
3636rcu_error:
3637 rcu_read_unlock();
421cb601 3638error:
840cb59c 3639 health_code_update();
5b4a0ec0 3640 return ret;
421cb601
DG
3641}
3642
f37d259d
MD
3643/*
3644 * Fill events array with all events name of all registered apps.
3645 */
3646int ust_app_list_event_fields(struct lttng_event_field **fields)
3647{
3648 int ret, handle;
3649 size_t nbmem, count = 0;
3650 struct lttng_ht_iter iter;
3651 struct ust_app *app;
c617c0c6 3652 struct lttng_event_field *tmp_event;
f37d259d
MD
3653
3654 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
3655 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
3656 if (tmp_event == NULL) {
f37d259d
MD
3657 PERROR("zmalloc ust app event fields");
3658 ret = -ENOMEM;
3659 goto error;
3660 }
3661
3662 rcu_read_lock();
3663
3664 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3665 struct lttng_ust_field_iter uiter;
3666
840cb59c 3667 health_code_update();
86acf0da 3668
f37d259d
MD
3669 if (!app->compatible) {
3670 /*
3671 * TODO: In time, we should notice the caller of this error by
3672 * telling him that this is a version error.
3673 */
3674 continue;
3675 }
fb45065e 3676 pthread_mutex_lock(&app->sock_lock);
f37d259d
MD
3677 handle = ustctl_tracepoint_field_list(app->sock);
3678 if (handle < 0) {
ffe60014
DG
3679 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
3680 ERR("UST app list field getting handle failed for app pid %d",
3681 app->pid);
3682 }
fb45065e 3683 pthread_mutex_unlock(&app->sock_lock);
f37d259d
MD
3684 continue;
3685 }
3686
3687 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 3688 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
3689 /* Handle ustctl error. */
3690 if (ret < 0) {
fb45065e
MD
3691 int release_ret;
3692
a2ba1ab0 3693 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
3694 ERR("UST app tp list field failed for app %d with ret %d",
3695 app->sock, ret);
3696 } else {
3697 DBG3("UST app tp list field failed. Application is dead");
3757b385
DG
3698 /*
3699 * This is normal behavior, an application can die during the
3700 * creation process. Don't report an error so the execution can
98f595d4 3701 * continue normally. Reset list and count for next app.
3757b385
DG
3702 */
3703 break;
ffe60014 3704 }
98f595d4 3705 free(tmp_event);
fb45065e
MD
3706 release_ret = ustctl_release_handle(app->sock, handle);
3707 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3708 if (release_ret < 0 &&
3709 release_ret != -LTTNG_UST_ERR_EXITING &&
3710 release_ret != -EPIPE) {
fb45065e
MD
3711 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3712 }
ffe60014
DG
3713 goto rcu_error;
3714 }
3715
840cb59c 3716 health_code_update();
f37d259d 3717 if (count >= nbmem) {
d7b3776f 3718 /* In case the realloc fails, we free the memory */
53efb85a
MD
3719 struct lttng_event_field *new_tmp_event;
3720 size_t new_nbmem;
3721
3722 new_nbmem = nbmem << 1;
3723 DBG2("Reallocating event field list from %zu to %zu entries",
3724 nbmem, new_nbmem);
3725 new_tmp_event = realloc(tmp_event,
3726 new_nbmem * sizeof(struct lttng_event_field));
3727 if (new_tmp_event == NULL) {
fb45065e
MD
3728 int release_ret;
3729
f37d259d 3730 PERROR("realloc ust app event fields");
c617c0c6 3731 free(tmp_event);
f37d259d 3732 ret = -ENOMEM;
fb45065e
MD
3733 release_ret = ustctl_release_handle(app->sock, handle);
3734 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3735 if (release_ret &&
3736 release_ret != -LTTNG_UST_ERR_EXITING &&
3737 release_ret != -EPIPE) {
fb45065e
MD
3738 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
3739 }
f37d259d
MD
3740 goto rcu_error;
3741 }
53efb85a
MD
3742 /* Zero the new memory */
3743 memset(new_tmp_event + nbmem, 0,
3744 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
3745 nbmem = new_nbmem;
3746 tmp_event = new_tmp_event;
f37d259d 3747 }
f37d259d 3748
c617c0c6 3749 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
2e84128e
DG
3750 /* Mapping between these enums matches 1 to 1. */
3751 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
c617c0c6 3752 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 3753
c617c0c6
MD
3754 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
3755 tmp_event[count].event.loglevel = uiter.loglevel;
2e84128e 3756 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
c617c0c6
MD
3757 tmp_event[count].event.pid = app->pid;
3758 tmp_event[count].event.enabled = -1;
f37d259d
MD
3759 count++;
3760 }
fb45065e
MD
3761 ret = ustctl_release_handle(app->sock, handle);
3762 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
3763 if (ret < 0 &&
3764 ret != -LTTNG_UST_ERR_EXITING &&
3765 ret != -EPIPE) {
fb45065e
MD
3766 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
3767 }
f37d259d
MD
3768 }
3769
3770 ret = count;
c617c0c6 3771 *fields = tmp_event;
f37d259d
MD
3772
3773 DBG2("UST app list event fields done (%zu events)", count);
3774
3775rcu_error:
3776 rcu_read_unlock();
3777error:
840cb59c 3778 health_code_update();
f37d259d
MD
3779 return ret;
3780}
3781
5b4a0ec0
DG
3782/*
3783 * Free and clean all traceable apps of the global list.
36b588ed
MD
3784 *
3785 * Should _NOT_ be called with RCU read-side lock held.
5b4a0ec0
DG
3786 */
3787void ust_app_clean_list(void)
421cb601 3788{
5b4a0ec0 3789 int ret;
659ed79f 3790 struct ust_app *app;
bec39940 3791 struct lttng_ht_iter iter;
421cb601 3792
5b4a0ec0 3793 DBG2("UST app cleaning registered apps hash table");
421cb601 3794
5b4a0ec0 3795 rcu_read_lock();
421cb601 3796
f1b711c4
MD
3797 if (ust_app_ht) {
3798 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3799 ret = lttng_ht_del(ust_app_ht, &iter);
3800 assert(!ret);
3801 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
3802 }
421cb601
DG
3803 }
3804
852d0037 3805 /* Cleanup socket hash table */
f1b711c4
MD
3806 if (ust_app_ht_by_sock) {
3807 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
3808 sock_n.node) {
3809 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
3810 assert(!ret);
3811 }
bec39940 3812 }
852d0037 3813
d88aee68 3814 /* Cleanup notify socket hash table */
f1b711c4
MD
3815 if (ust_app_ht_by_notify_sock) {
3816 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
3817 notify_sock_n.node) {
3818 ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
3819 assert(!ret);
3820 }
d88aee68 3821 }
36b588ed 3822 rcu_read_unlock();
d88aee68 3823
bec39940 3824 /* Destroy is done only when the ht is empty */
f1b711c4
MD
3825 if (ust_app_ht) {
3826 ht_cleanup_push(ust_app_ht);
3827 }
3828 if (ust_app_ht_by_sock) {
3829 ht_cleanup_push(ust_app_ht_by_sock);
3830 }
3831 if (ust_app_ht_by_notify_sock) {
3832 ht_cleanup_push(ust_app_ht_by_notify_sock);
3833 }
5b4a0ec0
DG
3834}
3835
3836/*
3837 * Init UST app hash table.
3838 */
57703f6e 3839int ust_app_ht_alloc(void)
5b4a0ec0 3840{
bec39940 3841 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
3842 if (!ust_app_ht) {
3843 return -1;
3844 }
852d0037 3845 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
3846 if (!ust_app_ht_by_sock) {
3847 return -1;
3848 }
d0b96690 3849 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
3850 if (!ust_app_ht_by_notify_sock) {
3851 return -1;
3852 }
3853 return 0;
421cb601
DG
3854}
3855
78f0bacd
DG
3856/*
3857 * For a specific UST session, disable the channel for all registered apps.
3858 */
35a9059d 3859int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
3860 struct ltt_ust_channel *uchan)
3861{
3862 int ret = 0;
bec39940
DG
3863 struct lttng_ht_iter iter;
3864 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
3865 struct ust_app *app;
3866 struct ust_app_session *ua_sess;
8535a6d9 3867 struct ust_app_channel *ua_chan;
78f0bacd
DG
3868
3869 if (usess == NULL || uchan == NULL) {
3870 ERR("Disabling UST global channel with NULL values");
3871 ret = -1;
3872 goto error;
3873 }
3874
d9bf3ca4 3875 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
a991f516 3876 uchan->name, usess->id);
78f0bacd
DG
3877
3878 rcu_read_lock();
3879
3880 /* For every registered applications */
852d0037 3881 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 3882 struct lttng_ht_iter uiter;
e0c7ec2b
DG
3883 if (!app->compatible) {
3884 /*
3885 * TODO: In time, we should notice the caller of this error by
3886 * telling him that this is a version error.
3887 */
3888 continue;
3889 }
78f0bacd
DG
3890 ua_sess = lookup_session_by_app(usess, app);
3891 if (ua_sess == NULL) {
3892 continue;
3893 }
3894
8535a6d9 3895 /* Get channel */
bec39940
DG
3896 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
3897 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
3898 /* If the session if found for the app, the channel must be there */
3899 assert(ua_chan_node);
3900
3901 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3902 /* The channel must not be already disabled */
3903 assert(ua_chan->enabled == 1);
3904
3905 /* Disable channel onto application */
3906 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
3907 if (ret < 0) {
3908 /* XXX: We might want to report this error at some point... */
3909 continue;
3910 }
3911 }
3912
3913 rcu_read_unlock();
3914
3915error:
3916 return ret;
3917}
3918
3919/*
3920 * For a specific UST session, enable the channel for all registered apps.
3921 */
35a9059d 3922int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
3923 struct ltt_ust_channel *uchan)
3924{
3925 int ret = 0;
bec39940 3926 struct lttng_ht_iter iter;
78f0bacd
DG
3927 struct ust_app *app;
3928 struct ust_app_session *ua_sess;
3929
3930 if (usess == NULL || uchan == NULL) {
3931 ERR("Adding UST global channel to NULL values");
3932 ret = -1;
3933 goto error;
3934 }
3935
d9bf3ca4 3936 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
a991f516 3937 uchan->name, usess->id);
78f0bacd
DG
3938
3939 rcu_read_lock();
3940
3941 /* For every registered applications */
852d0037 3942 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
3943 if (!app->compatible) {
3944 /*
3945 * TODO: In time, we should notice the caller of this error by
3946 * telling him that this is a version error.
3947 */
3948 continue;
3949 }
78f0bacd
DG
3950 ua_sess = lookup_session_by_app(usess, app);
3951 if (ua_sess == NULL) {
3952 continue;
3953 }
3954
3955 /* Enable channel onto application */
3956 ret = enable_ust_app_channel(ua_sess, uchan, app);
3957 if (ret < 0) {
3958 /* XXX: We might want to report this error at some point... */
3959 continue;
3960 }
3961 }
3962
3963 rcu_read_unlock();
3964
3965error:
3966 return ret;
3967}
3968
b0a40d28
DG
3969/*
3970 * Disable an event in a channel and for a specific session.
3971 */
35a9059d
DG
3972int ust_app_disable_event_glb(struct ltt_ust_session *usess,
3973 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
3974{
3975 int ret = 0;
bec39940 3976 struct lttng_ht_iter iter, uiter;
700c5a9d 3977 struct lttng_ht_node_str *ua_chan_node;
b0a40d28
DG
3978 struct ust_app *app;
3979 struct ust_app_session *ua_sess;
3980 struct ust_app_channel *ua_chan;
3981 struct ust_app_event *ua_event;
3982
3983 DBG("UST app disabling event %s for all apps in channel "
d9bf3ca4
MD
3984 "%s for session id %" PRIu64,
3985 uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
3986
3987 rcu_read_lock();
3988
3989 /* For all registered applications */
852d0037 3990 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
3991 if (!app->compatible) {
3992 /*
3993 * TODO: In time, we should notice the caller of this error by
3994 * telling him that this is a version error.
3995 */
3996 continue;
3997 }
b0a40d28
DG
3998 ua_sess = lookup_session_by_app(usess, app);
3999 if (ua_sess == NULL) {
4000 /* Next app */
4001 continue;
4002 }
4003
4004 /* Lookup channel in the ust app session */
bec39940
DG
4005 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4006 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 4007 if (ua_chan_node == NULL) {
d9bf3ca4 4008 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
852d0037 4009 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
4010 continue;
4011 }
4012 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4013
700c5a9d
JR
4014 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4015 uevent->filter, uevent->attr.loglevel,
4016 uevent->exclusion);
4017 if (ua_event == NULL) {
b0a40d28 4018 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 4019 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
4020 continue;
4021 }
b0a40d28 4022
7f79d3a1 4023 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
4024 if (ret < 0) {
4025 /* XXX: Report error someday... */
4026 continue;
4027 }
4028 }
4029
4030 rcu_read_unlock();
4031
4032 return ret;
4033}
4034
421cb601 4035/*
5b4a0ec0 4036 * For a specific UST session, create the channel for all registered apps.
421cb601 4037 */
35a9059d 4038int ust_app_create_channel_glb(struct ltt_ust_session *usess,
48842b30
DG
4039 struct ltt_ust_channel *uchan)
4040{
3d8ca23b 4041 int ret = 0, created;
bec39940 4042 struct lttng_ht_iter iter;
48842b30 4043 struct ust_app *app;
3d8ca23b 4044 struct ust_app_session *ua_sess = NULL;
48842b30 4045
fc34caaa
DG
4046 /* Very wrong code flow */
4047 assert(usess);
4048 assert(uchan);
421cb601 4049
d9bf3ca4 4050 DBG2("UST app adding channel %s to UST domain for session id %" PRIu64,
a991f516 4051 uchan->name, usess->id);
48842b30
DG
4052
4053 rcu_read_lock();
421cb601 4054
5b4a0ec0 4055 /* For every registered applications */
852d0037 4056 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4057 if (!app->compatible) {
4058 /*
4059 * TODO: In time, we should notice the caller of this error by
4060 * telling him that this is a version error.
4061 */
4062 continue;
4063 }
a9ad0c8f
MD
4064 if (!trace_ust_pid_tracker_lookup(usess, app->pid)) {
4065 /* Skip. */
4066 continue;
4067 }
4068
edb67388
DG
4069 /*
4070 * Create session on the tracer side and add it to app session HT. Note
4071 * that if session exist, it will simply return a pointer to the ust
4072 * app session.
4073 */
3d8ca23b
DG
4074 ret = create_ust_app_session(usess, app, &ua_sess, &created);
4075 if (ret < 0) {
4076 switch (ret) {
4077 case -ENOTCONN:
4078 /*
4079 * The application's socket is not valid. Either a bad socket
4080 * or a timeout on it. We can't inform the caller that for a
4081 * specific app, the session failed so lets continue here.
4082 */
a7169585 4083 ret = 0; /* Not an error. */
3d8ca23b
DG
4084 continue;
4085 case -ENOMEM:
4086 default:
4087 goto error_rcu_unlock;
4088 }
48842b30 4089 }
3d8ca23b 4090 assert(ua_sess);
48842b30 4091
d0b96690 4092 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4093
4094 if (ua_sess->deleted) {
4095 pthread_mutex_unlock(&ua_sess->lock);
4096 continue;
4097 }
4098
d65d2de8
DG
4099 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4100 sizeof(uchan->name))) {
ad7a9107
DG
4101 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr);
4102 ret = 0;
d65d2de8
DG
4103 } else {
4104 /* Create channel onto application. We don't need the chan ref. */
4105 ret = create_ust_app_channel(ua_sess, uchan, app,
4106 LTTNG_UST_CHAN_PER_CPU, usess, NULL);
4107 }
d0b96690 4108 pthread_mutex_unlock(&ua_sess->lock);
3d8ca23b 4109 if (ret < 0) {
3d8ca23b
DG
4110 /* Cleanup the created session if it's the case. */
4111 if (created) {
d0b96690 4112 destroy_app_session(app, ua_sess);
3d8ca23b 4113 }
a7169585
MD
4114 switch (ret) {
4115 case -ENOTCONN:
4116 /*
4117 * The application's socket is not valid. Either a bad socket
4118 * or a timeout on it. We can't inform the caller that for a
4119 * specific app, the session failed so lets continue here.
4120 */
4121 ret = 0; /* Not an error. */
4122 continue;
4123 case -ENOMEM:
4124 default:
4125 goto error_rcu_unlock;
4126 }
48842b30 4127 }
48842b30 4128 }
5b4a0ec0 4129
95e047ff 4130error_rcu_unlock:
48842b30 4131 rcu_read_unlock();
3c14c33f 4132 return ret;
48842b30
DG
4133}
4134
5b4a0ec0 4135/*
edb67388 4136 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 4137 */
35a9059d 4138int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
4139 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4140{
4141 int ret = 0;
bec39940 4142 struct lttng_ht_iter iter, uiter;
18eace3b 4143 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
4144 struct ust_app *app;
4145 struct ust_app_session *ua_sess;
4146 struct ust_app_channel *ua_chan;
4147 struct ust_app_event *ua_event;
48842b30 4148
d9bf3ca4 4149 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
a991f516 4150 uevent->attr.name, usess->id);
48842b30 4151
edb67388
DG
4152 /*
4153 * NOTE: At this point, this function is called only if the session and
4154 * channel passed are already created for all apps. and enabled on the
4155 * tracer also.
4156 */
4157
48842b30 4158 rcu_read_lock();
421cb601
DG
4159
4160 /* For all registered applications */
852d0037 4161 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4162 if (!app->compatible) {
4163 /*
4164 * TODO: In time, we should notice the caller of this error by
4165 * telling him that this is a version error.
4166 */
4167 continue;
4168 }
edb67388 4169 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4170 if (!ua_sess) {
4171 /* The application has problem or is probably dead. */
4172 continue;
4173 }
ba767faf 4174
d0b96690
DG
4175 pthread_mutex_lock(&ua_sess->lock);
4176
b161602a
MD
4177 if (ua_sess->deleted) {
4178 pthread_mutex_unlock(&ua_sess->lock);
4179 continue;
4180 }
4181
edb67388 4182 /* Lookup channel in the ust app session */
bec39940
DG
4183 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4184 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
a7169585
MD
4185 /*
4186 * It is possible that the channel cannot be found is
4187 * the channel/event creation occurs concurrently with
4188 * an application exit.
4189 */
4190 if (!ua_chan_node) {
4191 pthread_mutex_unlock(&ua_sess->lock);
4192 continue;
4193 }
edb67388
DG
4194
4195 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4196
18eace3b
DG
4197 /* Get event node */
4198 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 4199 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 4200 if (ua_event == NULL) {
7f79d3a1 4201 DBG3("UST app enable event %s not found for app PID %d."
852d0037 4202 "Skipping app", uevent->attr.name, app->pid);
d0b96690 4203 goto next_app;
35a9059d 4204 }
35a9059d
DG
4205
4206 ret = enable_ust_app_event(ua_sess, ua_event, app);
4207 if (ret < 0) {
d0b96690 4208 pthread_mutex_unlock(&ua_sess->lock);
7f79d3a1 4209 goto error;
48842b30 4210 }
d0b96690
DG
4211 next_app:
4212 pthread_mutex_unlock(&ua_sess->lock);
edb67388
DG
4213 }
4214
7f79d3a1 4215error:
edb67388 4216 rcu_read_unlock();
edb67388
DG
4217 return ret;
4218}
4219
4220/*
4221 * For a specific existing UST session and UST channel, creates the event for
4222 * all registered apps.
4223 */
35a9059d 4224int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
4225 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4226{
4227 int ret = 0;
bec39940
DG
4228 struct lttng_ht_iter iter, uiter;
4229 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
4230 struct ust_app *app;
4231 struct ust_app_session *ua_sess;
4232 struct ust_app_channel *ua_chan;
4233
d9bf3ca4 4234 DBG("UST app creating event %s for all apps for session id %" PRIu64,
a991f516 4235 uevent->attr.name, usess->id);
edb67388 4236
edb67388
DG
4237 rcu_read_lock();
4238
4239 /* For all registered applications */
852d0037 4240 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4241 if (!app->compatible) {
4242 /*
4243 * TODO: In time, we should notice the caller of this error by
4244 * telling him that this is a version error.
4245 */
4246 continue;
4247 }
edb67388 4248 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4249 if (!ua_sess) {
4250 /* The application has problem or is probably dead. */
4251 continue;
4252 }
48842b30 4253
d0b96690 4254 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4255
4256 if (ua_sess->deleted) {
4257 pthread_mutex_unlock(&ua_sess->lock);
4258 continue;
4259 }
4260
48842b30 4261 /* Lookup channel in the ust app session */
bec39940
DG
4262 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4263 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
4264 /* If the channel is not found, there is a code flow error */
4265 assert(ua_chan_node);
4266
48842b30
DG
4267 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4268
edb67388 4269 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
d0b96690 4270 pthread_mutex_unlock(&ua_sess->lock);
edb67388 4271 if (ret < 0) {
49c336c1 4272 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
4273 /* Possible value at this point: -ENOMEM. If so, we stop! */
4274 break;
4275 }
4276 DBG2("UST app event %s already exist on app PID %d",
852d0037 4277 uevent->attr.name, app->pid);
5b4a0ec0 4278 continue;
48842b30 4279 }
48842b30 4280 }
5b4a0ec0 4281
48842b30
DG
4282 rcu_read_unlock();
4283
4284 return ret;
4285}
4286
5b4a0ec0
DG
4287/*
4288 * Start tracing for a specific UST session and app.
4289 */
b34cbebf 4290static
421cb601 4291int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
4292{
4293 int ret = 0;
48842b30 4294 struct ust_app_session *ua_sess;
48842b30 4295
852d0037 4296 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 4297
509cbaf8
MD
4298 rcu_read_lock();
4299
e0c7ec2b
DG
4300 if (!app->compatible) {
4301 goto end;
4302 }
4303
421cb601
DG
4304 ua_sess = lookup_session_by_app(usess, app);
4305 if (ua_sess == NULL) {
d42f20df
DG
4306 /* The session is in teardown process. Ignore and continue. */
4307 goto end;
421cb601 4308 }
48842b30 4309
d0b96690
DG
4310 pthread_mutex_lock(&ua_sess->lock);
4311
b161602a
MD
4312 if (ua_sess->deleted) {
4313 pthread_mutex_unlock(&ua_sess->lock);
4314 goto end;
4315 }
4316
aea829b3
DG
4317 /* Upon restart, we skip the setup, already done */
4318 if (ua_sess->started) {
8be98f9a 4319 goto skip_setup;
aea829b3 4320 }
8be98f9a 4321
a4b92340
DG
4322 /* Create directories if consumer is LOCAL and has a path defined. */
4323 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
4324 strlen(usess->consumer->dst.trace_path) > 0) {
4325 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
7972aab2 4326 S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid);
a4b92340 4327 if (ret < 0) {
df5b86c8 4328 if (errno != EEXIST) {
a4b92340 4329 ERR("Trace directory creation error");
d0b96690 4330 goto error_unlock;
421cb601 4331 }
173af62f 4332 }
7753dea8 4333 }
aea829b3 4334
d65d2de8
DG
4335 /*
4336 * Create the metadata for the application. This returns gracefully if a
4337 * metadata was already set for the session.
4338 */
ad7a9107 4339 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
421cb601 4340 if (ret < 0) {
d0b96690 4341 goto error_unlock;
421cb601 4342 }
48842b30 4343
840cb59c 4344 health_code_update();
86acf0da 4345
8be98f9a 4346skip_setup:
421cb601 4347 /* This start the UST tracing */
fb45065e 4348 pthread_mutex_lock(&app->sock_lock);
852d0037 4349 ret = ustctl_start_session(app->sock, ua_sess->handle);
fb45065e 4350 pthread_mutex_unlock(&app->sock_lock);
421cb601 4351 if (ret < 0) {
ffe60014
DG
4352 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4353 ERR("Error starting tracing for app pid: %d (ret: %d)",
4354 app->pid, ret);
4355 } else {
4356 DBG("UST app start session failed. Application is dead.");
3757b385
DG
4357 /*
4358 * This is normal behavior, an application can die during the
4359 * creation process. Don't report an error so the execution can
4360 * continue normally.
4361 */
4362 pthread_mutex_unlock(&ua_sess->lock);
4363 goto end;
ffe60014 4364 }
d0b96690 4365 goto error_unlock;
421cb601 4366 }
5b4a0ec0 4367
55c3953d
DG
4368 /* Indicate that the session has been started once */
4369 ua_sess->started = 1;
4370
d0b96690
DG
4371 pthread_mutex_unlock(&ua_sess->lock);
4372
840cb59c 4373 health_code_update();
86acf0da 4374
421cb601 4375 /* Quiescent wait after starting trace */
fb45065e 4376 pthread_mutex_lock(&app->sock_lock);
ffe60014 4377 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4378 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4379 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4380 ERR("UST app wait quiescent failed for app pid %d ret %d",
4381 app->pid, ret);
4382 }
48842b30 4383
e0c7ec2b
DG
4384end:
4385 rcu_read_unlock();
840cb59c 4386 health_code_update();
421cb601 4387 return 0;
48842b30 4388
d0b96690
DG
4389error_unlock:
4390 pthread_mutex_unlock(&ua_sess->lock);
509cbaf8 4391 rcu_read_unlock();
840cb59c 4392 health_code_update();
421cb601
DG
4393 return -1;
4394}
48842b30 4395
8be98f9a
MD
4396/*
4397 * Stop tracing for a specific UST session and app.
4398 */
b34cbebf 4399static
8be98f9a
MD
4400int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
4401{
4402 int ret = 0;
4403 struct ust_app_session *ua_sess;
7972aab2 4404 struct ust_registry_session *registry;
8be98f9a 4405
852d0037 4406 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
4407
4408 rcu_read_lock();
4409
e0c7ec2b 4410 if (!app->compatible) {
d88aee68 4411 goto end_no_session;
e0c7ec2b
DG
4412 }
4413
8be98f9a
MD
4414 ua_sess = lookup_session_by_app(usess, app);
4415 if (ua_sess == NULL) {
d88aee68 4416 goto end_no_session;
8be98f9a
MD
4417 }
4418
d88aee68
DG
4419 pthread_mutex_lock(&ua_sess->lock);
4420
b161602a
MD
4421 if (ua_sess->deleted) {
4422 pthread_mutex_unlock(&ua_sess->lock);
4423 goto end_no_session;
4424 }
4425
9bc07046
DG
4426 /*
4427 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
4428 * that was never started. It's possible since we can have a fail start
4429 * from either the application manager thread or the command thread. Simply
4430 * indicate that this is a stop error.
9bc07046 4431 */
f9dfc3d9 4432 if (!ua_sess->started) {
c45536e1
DG
4433 goto error_rcu_unlock;
4434 }
7db205b5 4435
840cb59c 4436 health_code_update();
86acf0da 4437
9d6c7d3f 4438 /* This inhibits UST tracing */
fb45065e 4439 pthread_mutex_lock(&app->sock_lock);
852d0037 4440 ret = ustctl_stop_session(app->sock, ua_sess->handle);
fb45065e 4441 pthread_mutex_unlock(&app->sock_lock);
9d6c7d3f 4442 if (ret < 0) {
ffe60014
DG
4443 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4444 ERR("Error stopping tracing for app pid: %d (ret: %d)",
4445 app->pid, ret);
4446 } else {
4447 DBG("UST app stop session failed. Application is dead.");
3757b385
DG
4448 /*
4449 * This is normal behavior, an application can die during the
4450 * creation process. Don't report an error so the execution can
4451 * continue normally.
4452 */
4453 goto end_unlock;
ffe60014 4454 }
9d6c7d3f
DG
4455 goto error_rcu_unlock;
4456 }
4457
840cb59c 4458 health_code_update();
86acf0da 4459
9d6c7d3f 4460 /* Quiescent wait after stopping trace */
fb45065e 4461 pthread_mutex_lock(&app->sock_lock);
ffe60014 4462 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4463 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4464 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4465 ERR("UST app wait quiescent failed for app pid %d ret %d",
4466 app->pid, ret);
4467 }
9d6c7d3f 4468
840cb59c 4469 health_code_update();
86acf0da 4470
b34cbebf
MD
4471 registry = get_session_registry(ua_sess);
4472 assert(registry);
1b532a60 4473
ce34fcd0
MD
4474 /* Push metadata for application before freeing the application. */
4475 (void) push_metadata(registry, ua_sess->consumer);
b34cbebf 4476
3757b385 4477end_unlock:
b34cbebf
MD
4478 pthread_mutex_unlock(&ua_sess->lock);
4479end_no_session:
4480 rcu_read_unlock();
4481 health_code_update();
4482 return 0;
4483
4484error_rcu_unlock:
4485 pthread_mutex_unlock(&ua_sess->lock);
4486 rcu_read_unlock();
4487 health_code_update();
4488 return -1;
4489}
4490
b34cbebf 4491static
c4b88406
MD
4492int ust_app_flush_app_session(struct ust_app *app,
4493 struct ust_app_session *ua_sess)
b34cbebf 4494{
c4b88406 4495 int ret, retval = 0;
b34cbebf 4496 struct lttng_ht_iter iter;
b34cbebf 4497 struct ust_app_channel *ua_chan;
c4b88406 4498 struct consumer_socket *socket;
b34cbebf 4499
c4b88406 4500 DBG("Flushing app session buffers for ust app pid %d", app->pid);
b34cbebf
MD
4501
4502 rcu_read_lock();
4503
4504 if (!app->compatible) {
c4b88406 4505 goto end_not_compatible;
b34cbebf
MD
4506 }
4507
4508 pthread_mutex_lock(&ua_sess->lock);
4509
b161602a
MD
4510 if (ua_sess->deleted) {
4511 goto end_deleted;
4512 }
4513
b34cbebf
MD
4514 health_code_update();
4515
9d6c7d3f 4516 /* Flushing buffers */
c4b88406
MD
4517 socket = consumer_find_socket_by_bitness(app->bits_per_long,
4518 ua_sess->consumer);
ce34fcd0
MD
4519
4520 /* Flush buffers and push metadata. */
4521 switch (ua_sess->buffer_type) {
4522 case LTTNG_BUFFER_PER_PID:
4523 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4524 node.node) {
4525 health_code_update();
ce34fcd0
MD
4526 ret = consumer_flush_channel(socket, ua_chan->key);
4527 if (ret) {
4528 ERR("Error flushing consumer channel");
4529 retval = -1;
4530 continue;
4531 }
8be98f9a 4532 }
ce34fcd0
MD
4533 break;
4534 case LTTNG_BUFFER_PER_UID:
4535 default:
4536 assert(0);
4537 break;
8be98f9a 4538 }
8be98f9a 4539
840cb59c 4540 health_code_update();
86acf0da 4541
b161602a 4542end_deleted:
d88aee68 4543 pthread_mutex_unlock(&ua_sess->lock);
ce34fcd0 4544
c4b88406
MD
4545end_not_compatible:
4546 rcu_read_unlock();
4547 health_code_update();
4548 return retval;
4549}
4550
4551/*
ce34fcd0
MD
4552 * Flush buffers for all applications for a specific UST session.
4553 * Called with UST session lock held.
c4b88406
MD
4554 */
4555static
ce34fcd0 4556int ust_app_flush_session(struct ltt_ust_session *usess)
c4b88406
MD
4557
4558{
99b1411c 4559 int ret = 0;
c4b88406 4560
ce34fcd0 4561 DBG("Flushing session buffers for all ust apps");
c4b88406
MD
4562
4563 rcu_read_lock();
4564
ce34fcd0
MD
4565 /* Flush buffers and push metadata. */
4566 switch (usess->buffer_type) {
4567 case LTTNG_BUFFER_PER_UID:
4568 {
4569 struct buffer_reg_uid *reg;
4570 struct lttng_ht_iter iter;
4571
4572 /* Flush all per UID buffers associated to that session. */
4573 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
4574 struct ust_registry_session *ust_session_reg;
4575 struct buffer_reg_channel *reg_chan;
4576 struct consumer_socket *socket;
4577
4578 /* Get consumer socket to use to push the metadata.*/
4579 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
4580 usess->consumer);
4581 if (!socket) {
4582 /* Ignore request if no consumer is found for the session. */
4583 continue;
4584 }
4585
4586 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
4587 reg_chan, node.node) {
4588 /*
4589 * The following call will print error values so the return
4590 * code is of little importance because whatever happens, we
4591 * have to try them all.
4592 */
4593 (void) consumer_flush_channel(socket, reg_chan->consumer_key);
4594 }
4595
4596 ust_session_reg = reg->registry->reg.ust;
4597 /* Push metadata. */
4598 (void) push_metadata(ust_session_reg, usess->consumer);
4599 }
ce34fcd0
MD
4600 break;
4601 }
4602 case LTTNG_BUFFER_PER_PID:
4603 {
4604 struct ust_app_session *ua_sess;
4605 struct lttng_ht_iter iter;
4606 struct ust_app *app;
4607
4608 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4609 ua_sess = lookup_session_by_app(usess, app);
4610 if (ua_sess == NULL) {
4611 continue;
4612 }
4613 (void) ust_app_flush_app_session(app, ua_sess);
4614 }
4615 break;
4616 }
4617 default:
99b1411c 4618 ret = -1;
ce34fcd0
MD
4619 assert(0);
4620 break;
c4b88406 4621 }
c4b88406 4622
7db205b5 4623 rcu_read_unlock();
840cb59c 4624 health_code_update();
c4b88406 4625 return ret;
8be98f9a
MD
4626}
4627
84cd17c6
MD
4628/*
4629 * Destroy a specific UST session in apps.
4630 */
3353de95 4631static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6 4632{
ffe60014 4633 int ret;
84cd17c6 4634 struct ust_app_session *ua_sess;
bec39940 4635 struct lttng_ht_iter iter;
d9bf3ca4 4636 struct lttng_ht_node_u64 *node;
84cd17c6 4637
852d0037 4638 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
4639
4640 rcu_read_lock();
4641
e0c7ec2b
DG
4642 if (!app->compatible) {
4643 goto end;
4644 }
4645
84cd17c6 4646 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 4647 node = lttng_ht_iter_get_node_u64(&iter);
84cd17c6 4648 if (node == NULL) {
d42f20df
DG
4649 /* Session is being or is deleted. */
4650 goto end;
84cd17c6
MD
4651 }
4652 ua_sess = caa_container_of(node, struct ust_app_session, node);
c4a1715b 4653
840cb59c 4654 health_code_update();
d0b96690 4655 destroy_app_session(app, ua_sess);
84cd17c6 4656
840cb59c 4657 health_code_update();
7db205b5 4658
84cd17c6 4659 /* Quiescent wait after stopping trace */
fb45065e 4660 pthread_mutex_lock(&app->sock_lock);
ffe60014 4661 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4662 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4663 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4664 ERR("UST app wait quiescent failed for app pid %d ret %d",
4665 app->pid, ret);
4666 }
e0c7ec2b
DG
4667end:
4668 rcu_read_unlock();
840cb59c 4669 health_code_update();
84cd17c6 4670 return 0;
84cd17c6
MD
4671}
4672
5b4a0ec0
DG
4673/*
4674 * Start tracing for the UST session.
4675 */
421cb601
DG
4676int ust_app_start_trace_all(struct ltt_ust_session *usess)
4677{
4678 int ret = 0;
bec39940 4679 struct lttng_ht_iter iter;
421cb601 4680 struct ust_app *app;
48842b30 4681
421cb601
DG
4682 DBG("Starting all UST traces");
4683
4684 rcu_read_lock();
421cb601 4685
852d0037 4686 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
421cb601 4687 ret = ust_app_start_trace(usess, app);
48842b30 4688 if (ret < 0) {
5b4a0ec0
DG
4689 /* Continue to next apps even on error */
4690 continue;
48842b30 4691 }
48842b30 4692 }
5b4a0ec0 4693
48842b30
DG
4694 rcu_read_unlock();
4695
4696 return 0;
4697}
487cf67c 4698
8be98f9a
MD
4699/*
4700 * Start tracing for the UST session.
ce34fcd0 4701 * Called with UST session lock held.
8be98f9a
MD
4702 */
4703int ust_app_stop_trace_all(struct ltt_ust_session *usess)
4704{
4705 int ret = 0;
bec39940 4706 struct lttng_ht_iter iter;
8be98f9a
MD
4707 struct ust_app *app;
4708
4709 DBG("Stopping all UST traces");
4710
4711 rcu_read_lock();
4712
b34cbebf
MD
4713 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4714 ret = ust_app_stop_trace(usess, app);
4715 if (ret < 0) {
4716 /* Continue to next apps even on error */
4717 continue;
4718 }
4719 }
4720
ce34fcd0 4721 (void) ust_app_flush_session(usess);
8be98f9a
MD
4722
4723 rcu_read_unlock();
4724
4725 return 0;
4726}
4727
84cd17c6
MD
4728/*
4729 * Destroy app UST session.
4730 */
4731int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
4732{
4733 int ret = 0;
bec39940 4734 struct lttng_ht_iter iter;
84cd17c6
MD
4735 struct ust_app *app;
4736
4737 DBG("Destroy all UST traces");
4738
4739 rcu_read_lock();
4740
852d0037 4741 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 4742 ret = destroy_trace(usess, app);
84cd17c6
MD
4743 if (ret < 0) {
4744 /* Continue to next apps even on error */
4745 continue;
4746 }
4747 }
4748
4749 rcu_read_unlock();
4750
4751 return 0;
4752}
4753
a9ad0c8f
MD
4754static
4755void ust_app_global_create(struct ltt_ust_session *usess, struct ust_app *app)
487cf67c 4756{
55c54cce 4757 int ret = 0;
31746f93 4758 struct lttng_ht_iter iter, uiter;
3d8ca23b 4759 struct ust_app_session *ua_sess = NULL;
487cf67c
DG
4760 struct ust_app_channel *ua_chan;
4761 struct ust_app_event *ua_event;
727d5404 4762 struct ust_app_ctx *ua_ctx;
a9ad0c8f 4763 int is_created = 0;
1f3580c7 4764
a9ad0c8f 4765 ret = create_ust_app_session(usess, app, &ua_sess, &is_created);
3d8ca23b
DG
4766 if (ret < 0) {
4767 /* Tracer is probably gone or ENOMEM. */
487cf67c
DG
4768 goto error;
4769 }
a9ad0c8f
MD
4770 if (!is_created) {
4771 /* App session already created. */
4772 goto end;
4773 }
3d8ca23b 4774 assert(ua_sess);
487cf67c 4775
d0b96690
DG
4776 pthread_mutex_lock(&ua_sess->lock);
4777
b161602a
MD
4778 if (ua_sess->deleted) {
4779 pthread_mutex_unlock(&ua_sess->lock);
4780 goto end;
4781 }
4782
284d8f55 4783 /*
d0b96690 4784 * We can iterate safely here over all UST app session since the create ust
284d8f55
DG
4785 * app session above made a shadow copy of the UST global domain from the
4786 * ltt ust session.
4787 */
bec39940
DG
4788 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
4789 node.node) {
ad7a9107 4790 ret = do_create_channel(app, usess, ua_sess, ua_chan);
a7169585 4791 if (ret < 0 && ret != -ENOTCONN) {
ad7a9107 4792 /*
a7169585
MD
4793 * Stop everything. On error, the application
4794 * failed, no more file descriptor are available
4795 * or ENOMEM so stopping here is the only thing
4796 * we can do for now. The only exception is
4797 * -ENOTCONN, which indicates that the application
4798 * has exit.
ad7a9107
DG
4799 */
4800 goto error_unlock;
487cf67c
DG
4801 }
4802
31746f93
DG
4803 /*
4804 * Add context using the list so they are enabled in the same order the
4805 * user added them.
4806 */
4807 cds_list_for_each_entry(ua_ctx, &ua_chan->ctx_list, list) {
727d5404
DG
4808 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
4809 if (ret < 0) {
d0b96690 4810 goto error_unlock;
727d5404
DG
4811 }
4812 }
4813
4814
284d8f55 4815 /* For each events */
bec39940
DG
4816 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
4817 node.node) {
284d8f55
DG
4818 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
4819 if (ret < 0) {
d0b96690 4820 goto error_unlock;
487cf67c 4821 }
36dc12cc 4822 }
487cf67c
DG
4823 }
4824
d0b96690
DG
4825 pthread_mutex_unlock(&ua_sess->lock);
4826
14fb1ebe 4827 if (usess->active) {
421cb601 4828 ret = ust_app_start_trace(usess, app);
36dc12cc 4829 if (ret < 0) {
36dc12cc
DG
4830 goto error;
4831 }
4832
852d0037 4833 DBG2("UST trace started for app pid %d", app->pid);
36dc12cc 4834 }
a9ad0c8f 4835end:
ffe60014 4836 /* Everything went well at this point. */
ffe60014
DG
4837 return;
4838
d0b96690
DG
4839error_unlock:
4840 pthread_mutex_unlock(&ua_sess->lock);
487cf67c 4841error:
ffe60014 4842 if (ua_sess) {
d0b96690 4843 destroy_app_session(app, ua_sess);
ffe60014 4844 }
487cf67c
DG
4845 return;
4846}
55cc08a6 4847
a9ad0c8f
MD
4848static
4849void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
4850{
4851 struct ust_app_session *ua_sess;
4852
4853 ua_sess = lookup_session_by_app(usess, app);
4854 if (ua_sess == NULL) {
4855 return;
4856 }
4857 destroy_app_session(app, ua_sess);
4858}
4859
4860/*
4861 * Add channels/events from UST global domain to registered apps at sock.
4862 *
4863 * Called with session lock held.
4864 * Called with RCU read-side lock held.
4865 */
4866void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
4867{
4868 assert(usess);
4869
4870 DBG2("UST app global update for app sock %d for session id %" PRIu64,
4871 app->sock, usess->id);
4872
4873 if (!app->compatible) {
4874 return;
4875 }
4876
4877 if (trace_ust_pid_tracker_lookup(usess, app->pid)) {
4878 ust_app_global_create(usess, app);
4879 } else {
4880 ust_app_global_destroy(usess, app);
4881 }
4882}
4883
4884/*
4885 * Called with session lock held.
4886 */
4887void ust_app_global_update_all(struct ltt_ust_session *usess)
4888{
4889 struct lttng_ht_iter iter;
4890 struct ust_app *app;
4891
4892 rcu_read_lock();
4893 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4894 ust_app_global_update(usess, app);
4895 }
4896 rcu_read_unlock();
4897}
4898
55cc08a6
DG
4899/*
4900 * Add context to a specific channel for global UST domain.
4901 */
4902int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
4903 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
4904{
4905 int ret = 0;
bec39940
DG
4906 struct lttng_ht_node_str *ua_chan_node;
4907 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
4908 struct ust_app_channel *ua_chan = NULL;
4909 struct ust_app_session *ua_sess;
4910 struct ust_app *app;
4911
4912 rcu_read_lock();
4913
852d0037 4914 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4915 if (!app->compatible) {
4916 /*
4917 * TODO: In time, we should notice the caller of this error by
4918 * telling him that this is a version error.
4919 */
4920 continue;
4921 }
55cc08a6
DG
4922 ua_sess = lookup_session_by_app(usess, app);
4923 if (ua_sess == NULL) {
4924 continue;
4925 }
4926
d0b96690 4927 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4928
4929 if (ua_sess->deleted) {
4930 pthread_mutex_unlock(&ua_sess->lock);
4931 continue;
4932 }
4933
55cc08a6 4934 /* Lookup channel in the ust app session */
bec39940
DG
4935 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4936 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6 4937 if (ua_chan_node == NULL) {
d0b96690 4938 goto next_app;
55cc08a6
DG
4939 }
4940 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
4941 node);
55cc08a6
DG
4942 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
4943 if (ret < 0) {
d0b96690 4944 goto next_app;
55cc08a6 4945 }
d0b96690
DG
4946 next_app:
4947 pthread_mutex_unlock(&ua_sess->lock);
55cc08a6
DG
4948 }
4949
55cc08a6
DG
4950 rcu_read_unlock();
4951 return ret;
4952}
4953
76d45b40
DG
4954/*
4955 * Enable event for a channel from a UST session for a specific PID.
4956 */
4957int ust_app_enable_event_pid(struct ltt_ust_session *usess,
4958 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
4959{
4960 int ret = 0;
bec39940 4961 struct lttng_ht_iter iter;
18eace3b 4962 struct lttng_ht_node_str *ua_chan_node;
76d45b40
DG
4963 struct ust_app *app;
4964 struct ust_app_session *ua_sess;
4965 struct ust_app_channel *ua_chan;
4966 struct ust_app_event *ua_event;
4967
4968 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
4969
4970 rcu_read_lock();
4971
4972 app = ust_app_find_by_pid(pid);
4973 if (app == NULL) {
4974 ERR("UST app enable event per PID %d not found", pid);
4975 ret = -1;
d0b96690 4976 goto end;
76d45b40
DG
4977 }
4978
e0c7ec2b
DG
4979 if (!app->compatible) {
4980 ret = 0;
d0b96690 4981 goto end;
e0c7ec2b
DG
4982 }
4983
76d45b40 4984 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4985 if (!ua_sess) {
4986 /* The application has problem or is probably dead. */
d0b96690
DG
4987 ret = 0;
4988 goto end;
c4a1715b 4989 }
76d45b40 4990
d0b96690 4991 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4992
4993 if (ua_sess->deleted) {
4994 ret = 0;
4995 goto end_unlock;
4996 }
4997
76d45b40 4998 /* Lookup channel in the ust app session */
bec39940
DG
4999 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
5000 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
76d45b40
DG
5001 /* If the channel is not found, there is a code flow error */
5002 assert(ua_chan_node);
5003
5004 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5005
18eace3b 5006 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 5007 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 5008 if (ua_event == NULL) {
76d45b40
DG
5009 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5010 if (ret < 0) {
d0b96690 5011 goto end_unlock;
76d45b40
DG
5012 }
5013 } else {
76d45b40
DG
5014 ret = enable_ust_app_event(ua_sess, ua_event, app);
5015 if (ret < 0) {
d0b96690 5016 goto end_unlock;
76d45b40
DG
5017 }
5018 }
5019
d0b96690
DG
5020end_unlock:
5021 pthread_mutex_unlock(&ua_sess->lock);
5022end:
76d45b40
DG
5023 rcu_read_unlock();
5024 return ret;
5025}
7f79d3a1 5026
4466912f
DG
5027/*
5028 * Calibrate registered applications.
5029 */
5030int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
5031{
5032 int ret = 0;
5033 struct lttng_ht_iter iter;
5034 struct ust_app *app;
5035
5036 rcu_read_lock();
5037
852d0037 5038 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4466912f
DG
5039 if (!app->compatible) {
5040 /*
5041 * TODO: In time, we should notice the caller of this error by
5042 * telling him that this is a version error.
5043 */
5044 continue;
5045 }
5046
840cb59c 5047 health_code_update();
86acf0da 5048
fb45065e 5049 pthread_mutex_lock(&app->sock_lock);
852d0037 5050 ret = ustctl_calibrate(app->sock, calibrate);
fb45065e 5051 pthread_mutex_unlock(&app->sock_lock);
4466912f
DG
5052 if (ret < 0) {
5053 switch (ret) {
5054 case -ENOSYS:
5055 /* Means that it's not implemented on the tracer side. */
5056 ret = 0;
5057 break;
5058 default:
4466912f 5059 DBG2("Calibrate app PID %d returned with error %d",
852d0037 5060 app->pid, ret);
4466912f
DG
5061 break;
5062 }
5063 }
5064 }
5065
5066 DBG("UST app global domain calibration finished");
5067
5068 rcu_read_unlock();
5069
840cb59c 5070 health_code_update();
86acf0da 5071
4466912f
DG
5072 return ret;
5073}
d0b96690
DG
5074
5075/*
5076 * Receive registration and populate the given msg structure.
5077 *
5078 * On success return 0 else a negative value returned by the ustctl call.
5079 */
5080int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
5081{
5082 int ret;
5083 uint32_t pid, ppid, uid, gid;
5084
5085 assert(msg);
5086
5087 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
5088 &pid, &ppid, &uid, &gid,
5089 &msg->bits_per_long,
5090 &msg->uint8_t_alignment,
5091 &msg->uint16_t_alignment,
5092 &msg->uint32_t_alignment,
5093 &msg->uint64_t_alignment,
5094 &msg->long_alignment,
5095 &msg->byte_order,
5096 msg->name);
5097 if (ret < 0) {
5098 switch (-ret) {
5099 case EPIPE:
5100 case ECONNRESET:
5101 case LTTNG_UST_ERR_EXITING:
5102 DBG3("UST app recv reg message failed. Application died");
5103 break;
5104 case LTTNG_UST_ERR_UNSUP_MAJOR:
5105 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
5106 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
5107 LTTNG_UST_ABI_MINOR_VERSION);
5108 break;
5109 default:
5110 ERR("UST app recv reg message failed with ret %d", ret);
5111 break;
5112 }
5113 goto error;
5114 }
5115 msg->pid = (pid_t) pid;
5116 msg->ppid = (pid_t) ppid;
5117 msg->uid = (uid_t) uid;
5118 msg->gid = (gid_t) gid;
5119
5120error:
5121 return ret;
5122}
5123
10b56aef
MD
5124/*
5125 * Return a ust app session object using the application object and the
5126 * session object descriptor has a key. If not found, NULL is returned.
5127 * A RCU read side lock MUST be acquired when calling this function.
5128*/
5129static struct ust_app_session *find_session_by_objd(struct ust_app *app,
5130 int objd)
5131{
5132 struct lttng_ht_node_ulong *node;
5133 struct lttng_ht_iter iter;
5134 struct ust_app_session *ua_sess = NULL;
5135
5136 assert(app);
5137
5138 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
5139 node = lttng_ht_iter_get_node_ulong(&iter);
5140 if (node == NULL) {
5141 DBG2("UST app session find by objd %d not found", objd);
5142 goto error;
5143 }
5144
5145 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
5146
5147error:
5148 return ua_sess;
5149}
5150
d88aee68
DG
5151/*
5152 * Return a ust app channel object using the application object and the channel
5153 * object descriptor has a key. If not found, NULL is returned. A RCU read side
5154 * lock MUST be acquired before calling this function.
5155 */
d0b96690
DG
5156static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
5157 int objd)
5158{
5159 struct lttng_ht_node_ulong *node;
5160 struct lttng_ht_iter iter;
5161 struct ust_app_channel *ua_chan = NULL;
5162
5163 assert(app);
5164
5165 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
5166 node = lttng_ht_iter_get_node_ulong(&iter);
5167 if (node == NULL) {
5168 DBG2("UST app channel find by objd %d not found", objd);
5169 goto error;
5170 }
5171
5172 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
5173
5174error:
5175 return ua_chan;
5176}
5177
d88aee68
DG
5178/*
5179 * Reply to a register channel notification from an application on the notify
5180 * socket. The channel metadata is also created.
5181 *
5182 * The session UST registry lock is acquired in this function.
5183 *
5184 * On success 0 is returned else a negative value.
5185 */
d0b96690
DG
5186static int reply_ust_register_channel(int sock, int sobjd, int cobjd,
5187 size_t nr_fields, struct ustctl_field *fields)
5188{
5189 int ret, ret_code = 0;
5190 uint32_t chan_id, reg_count;
7972aab2 5191 uint64_t chan_reg_key;
d0b96690
DG
5192 enum ustctl_channel_header type;
5193 struct ust_app *app;
5194 struct ust_app_channel *ua_chan;
5195 struct ust_app_session *ua_sess;
7972aab2 5196 struct ust_registry_session *registry;
45893984 5197 struct ust_registry_channel *chan_reg;
d0b96690
DG
5198
5199 rcu_read_lock();
5200
5201 /* Lookup application. If not found, there is a code flow error. */
5202 app = find_app_by_notify_sock(sock);
d88aee68
DG
5203 if (!app) {
5204 DBG("Application socket %d is being teardown. Abort event notify",
5205 sock);
5206 ret = 0;
d5d629b5 5207 free(fields);
d88aee68
DG
5208 goto error_rcu_unlock;
5209 }
d0b96690 5210
4950b860 5211 /* Lookup channel by UST object descriptor. */
d0b96690 5212 ua_chan = find_channel_by_objd(app, cobjd);
4950b860
MD
5213 if (!ua_chan) {
5214 DBG("Application channel is being teardown. Abort event notify");
5215 ret = 0;
d5d629b5 5216 free(fields);
4950b860
MD
5217 goto error_rcu_unlock;
5218 }
5219
d0b96690
DG
5220 assert(ua_chan->session);
5221 ua_sess = ua_chan->session;
d0b96690 5222
7972aab2
DG
5223 /* Get right session registry depending on the session buffer type. */
5224 registry = get_session_registry(ua_sess);
5225 assert(registry);
45893984 5226
7972aab2
DG
5227 /* Depending on the buffer type, a different channel key is used. */
5228 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5229 chan_reg_key = ua_chan->tracing_channel_id;
d0b96690 5230 } else {
7972aab2 5231 chan_reg_key = ua_chan->key;
d0b96690
DG
5232 }
5233
7972aab2
DG
5234 pthread_mutex_lock(&registry->lock);
5235
5236 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
5237 assert(chan_reg);
5238
5239 if (!chan_reg->register_done) {
5240 reg_count = ust_registry_get_event_count(chan_reg);
5241 if (reg_count < 31) {
5242 type = USTCTL_CHANNEL_HEADER_COMPACT;
5243 } else {
5244 type = USTCTL_CHANNEL_HEADER_LARGE;
5245 }
5246
5247 chan_reg->nr_ctx_fields = nr_fields;
5248 chan_reg->ctx_fields = fields;
5249 chan_reg->header_type = type;
d0b96690 5250 } else {
7972aab2
DG
5251 /* Get current already assigned values. */
5252 type = chan_reg->header_type;
d5d629b5
DG
5253 free(fields);
5254 /* Set to NULL so the error path does not do a double free. */
5255 fields = NULL;
d0b96690 5256 }
7972aab2
DG
5257 /* Channel id is set during the object creation. */
5258 chan_id = chan_reg->chan_id;
d0b96690
DG
5259
5260 /* Append to metadata */
7972aab2
DG
5261 if (!chan_reg->metadata_dumped) {
5262 ret_code = ust_metadata_channel_statedump(registry, chan_reg);
d0b96690
DG
5263 if (ret_code) {
5264 ERR("Error appending channel metadata (errno = %d)", ret_code);
5265 goto reply;
5266 }
5267 }
5268
5269reply:
7972aab2
DG
5270 DBG3("UST app replying to register channel key %" PRIu64
5271 " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type,
5272 ret_code);
d0b96690
DG
5273
5274 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
5275 if (ret < 0) {
5276 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5277 ERR("UST app reply channel failed with ret %d", ret);
5278 } else {
5279 DBG3("UST app reply channel failed. Application died");
5280 }
5281 goto error;
5282 }
5283
7972aab2
DG
5284 /* This channel registry registration is completed. */
5285 chan_reg->register_done = 1;
5286
d0b96690 5287error:
7972aab2 5288 pthread_mutex_unlock(&registry->lock);
d88aee68 5289error_rcu_unlock:
d0b96690 5290 rcu_read_unlock();
d5d629b5
DG
5291 if (ret) {
5292 free(fields);
5293 }
d0b96690
DG
5294 return ret;
5295}
5296
d88aee68
DG
5297/*
5298 * Add event to the UST channel registry. When the event is added to the
5299 * registry, the metadata is also created. Once done, this replies to the
5300 * application with the appropriate error code.
5301 *
5302 * The session UST registry lock is acquired in the function.
5303 *
5304 * On success 0 is returned else a negative value.
5305 */
d0b96690 5306static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
2106efa0
PP
5307 char *sig, size_t nr_fields, struct ustctl_field *fields,
5308 int loglevel_value, char *model_emf_uri)
d0b96690
DG
5309{
5310 int ret, ret_code;
5311 uint32_t event_id = 0;
7972aab2 5312 uint64_t chan_reg_key;
d0b96690
DG
5313 struct ust_app *app;
5314 struct ust_app_channel *ua_chan;
5315 struct ust_app_session *ua_sess;
7972aab2 5316 struct ust_registry_session *registry;
d0b96690
DG
5317
5318 rcu_read_lock();
5319
5320 /* Lookup application. If not found, there is a code flow error. */
5321 app = find_app_by_notify_sock(sock);
d88aee68
DG
5322 if (!app) {
5323 DBG("Application socket %d is being teardown. Abort event notify",
5324 sock);
5325 ret = 0;
d5d629b5
DG
5326 free(sig);
5327 free(fields);
5328 free(model_emf_uri);
d88aee68
DG
5329 goto error_rcu_unlock;
5330 }
d0b96690 5331
4950b860 5332 /* Lookup channel by UST object descriptor. */
d0b96690 5333 ua_chan = find_channel_by_objd(app, cobjd);
4950b860
MD
5334 if (!ua_chan) {
5335 DBG("Application channel is being teardown. Abort event notify");
5336 ret = 0;
d5d629b5
DG
5337 free(sig);
5338 free(fields);
5339 free(model_emf_uri);
4950b860
MD
5340 goto error_rcu_unlock;
5341 }
5342
d0b96690
DG
5343 assert(ua_chan->session);
5344 ua_sess = ua_chan->session;
5345
7972aab2
DG
5346 registry = get_session_registry(ua_sess);
5347 assert(registry);
5348
5349 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
5350 chan_reg_key = ua_chan->tracing_channel_id;
5351 } else {
5352 chan_reg_key = ua_chan->key;
5353 }
5354
5355 pthread_mutex_lock(&registry->lock);
d0b96690 5356
d5d629b5
DG
5357 /*
5358 * From this point on, this call acquires the ownership of the sig, fields
5359 * and model_emf_uri meaning any free are done inside it if needed. These
5360 * three variables MUST NOT be read/write after this.
5361 */
7972aab2 5362 ret_code = ust_registry_create_event(registry, chan_reg_key,
2106efa0
PP
5363 sobjd, cobjd, name, sig, nr_fields, fields,
5364 loglevel_value, model_emf_uri, ua_sess->buffer_type,
5365 &event_id, app);
d0b96690
DG
5366
5367 /*
5368 * The return value is returned to ustctl so in case of an error, the
5369 * application can be notified. In case of an error, it's important not to
5370 * return a negative error or else the application will get closed.
5371 */
5372 ret = ustctl_reply_register_event(sock, event_id, ret_code);
5373 if (ret < 0) {
5374 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5375 ERR("UST app reply event failed with ret %d", ret);
5376 } else {
5377 DBG3("UST app reply event failed. Application died");
5378 }
5379 /*
5380 * No need to wipe the create event since the application socket will
5381 * get close on error hence cleaning up everything by itself.
5382 */
5383 goto error;
5384 }
5385
7972aab2
DG
5386 DBG3("UST registry event %s with id %" PRId32 " added successfully",
5387 name, event_id);
d88aee68 5388
d0b96690 5389error:
7972aab2 5390 pthread_mutex_unlock(&registry->lock);
d88aee68 5391error_rcu_unlock:
d0b96690
DG
5392 rcu_read_unlock();
5393 return ret;
5394}
5395
10b56aef
MD
5396/*
5397 * Add enum to the UST session registry. Once done, this replies to the
5398 * application with the appropriate error code.
5399 *
5400 * The session UST registry lock is acquired within this function.
5401 *
5402 * On success 0 is returned else a negative value.
5403 */
5404static int add_enum_ust_registry(int sock, int sobjd, char *name,
5405 struct ustctl_enum_entry *entries, size_t nr_entries)
5406{
5407 int ret = 0, ret_code;
5408 struct ust_app *app;
5409 struct ust_app_session *ua_sess;
5410 struct ust_registry_session *registry;
5411 uint64_t enum_id = -1ULL;
5412
5413 rcu_read_lock();
5414
5415 /* Lookup application. If not found, there is a code flow error. */
5416 app = find_app_by_notify_sock(sock);
5417 if (!app) {
5418 /* Return an error since this is not an error */
5419 DBG("Application socket %d is being torn down. Aborting enum registration",
5420 sock);
5421 free(entries);
5422 goto error_rcu_unlock;
5423 }
5424
5425 /* Lookup session by UST object descriptor. */
5426 ua_sess = find_session_by_objd(app, sobjd);
5427 if (!ua_sess) {
5428 /* Return an error since this is not an error */
5429 DBG("Application session is being torn down. Aborting enum registration.");
5430 free(entries);
5431 goto error_rcu_unlock;
5432 }
5433
5434 registry = get_session_registry(ua_sess);
5435 assert(registry);
5436
5437 pthread_mutex_lock(&registry->lock);
5438
5439 /*
5440 * From this point on, the callee acquires the ownership of
5441 * entries. The variable entries MUST NOT be read/written after
5442 * call.
5443 */
5444 ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
5445 entries, nr_entries, &enum_id);
5446 entries = NULL;
5447
5448 /*
5449 * The return value is returned to ustctl so in case of an error, the
5450 * application can be notified. In case of an error, it's important not to
5451 * return a negative error or else the application will get closed.
5452 */
5453 ret = ustctl_reply_register_enum(sock, enum_id, ret_code);
5454 if (ret < 0) {
5455 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5456 ERR("UST app reply enum failed with ret %d", ret);
5457 } else {
5458 DBG3("UST app reply enum failed. Application died");
5459 }
5460 /*
5461 * No need to wipe the create enum since the application socket will
5462 * get close on error hence cleaning up everything by itself.
5463 */
5464 goto error;
5465 }
5466
5467 DBG3("UST registry enum %s added successfully or already found", name);
5468
5469error:
5470 pthread_mutex_unlock(&registry->lock);
5471error_rcu_unlock:
5472 rcu_read_unlock();
5473 return ret;
5474}
5475
d88aee68
DG
5476/*
5477 * Handle application notification through the given notify socket.
5478 *
5479 * Return 0 on success or else a negative value.
5480 */
d0b96690
DG
5481int ust_app_recv_notify(int sock)
5482{
5483 int ret;
5484 enum ustctl_notify_cmd cmd;
5485
5486 DBG3("UST app receiving notify from sock %d", sock);
5487
5488 ret = ustctl_recv_notify(sock, &cmd);
5489 if (ret < 0) {
5490 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5491 ERR("UST app recv notify failed with ret %d", ret);
5492 } else {
5493 DBG3("UST app recv notify failed. Application died");
5494 }
5495 goto error;
5496 }
5497
5498 switch (cmd) {
5499 case USTCTL_NOTIFY_CMD_EVENT:
5500 {
2106efa0 5501 int sobjd, cobjd, loglevel_value;
d0b96690
DG
5502 char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri;
5503 size_t nr_fields;
5504 struct ustctl_field *fields;
5505
5506 DBG2("UST app ustctl register event received");
5507
2106efa0
PP
5508 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name,
5509 &loglevel_value, &sig, &nr_fields, &fields,
5510 &model_emf_uri);
d0b96690
DG
5511 if (ret < 0) {
5512 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5513 ERR("UST app recv event failed with ret %d", ret);
5514 } else {
5515 DBG3("UST app recv event failed. Application died");
5516 }
5517 goto error;
5518 }
5519
d5d629b5
DG
5520 /*
5521 * Add event to the UST registry coming from the notify socket. This
5522 * call will free if needed the sig, fields and model_emf_uri. This
5523 * code path loses the ownsership of these variables and transfer them
5524 * to the this function.
5525 */
d0b96690 5526 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
2106efa0 5527 fields, loglevel_value, model_emf_uri);
d0b96690
DG
5528 if (ret < 0) {
5529 goto error;
5530 }
5531
5532 break;
5533 }
5534 case USTCTL_NOTIFY_CMD_CHANNEL:
5535 {
5536 int sobjd, cobjd;
5537 size_t nr_fields;
5538 struct ustctl_field *fields;
5539
5540 DBG2("UST app ustctl register channel received");
5541
5542 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
5543 &fields);
5544 if (ret < 0) {
5545 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5546 ERR("UST app recv channel failed with ret %d", ret);
5547 } else {
5548 DBG3("UST app recv channel failed. Application died");
5549 }
5550 goto error;
5551 }
5552
d5d629b5
DG
5553 /*
5554 * The fields ownership are transfered to this function call meaning
5555 * that if needed it will be freed. After this, it's invalid to access
5556 * fields or clean it up.
5557 */
d0b96690
DG
5558 ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields,
5559 fields);
5560 if (ret < 0) {
5561 goto error;
5562 }
5563
5564 break;
5565 }
10b56aef
MD
5566 case USTCTL_NOTIFY_CMD_ENUM:
5567 {
5568 int sobjd;
5569 char name[LTTNG_UST_SYM_NAME_LEN];
5570 size_t nr_entries;
5571 struct ustctl_enum_entry *entries;
5572
5573 DBG2("UST app ustctl register enum received");
5574
5575 ret = ustctl_recv_register_enum(sock, &sobjd, name,
5576 &entries, &nr_entries);
5577 if (ret < 0) {
5578 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5579 ERR("UST app recv enum failed with ret %d", ret);
5580 } else {
5581 DBG3("UST app recv enum failed. Application died");
5582 }
5583 goto error;
5584 }
5585
5586 /* Callee assumes ownership of entries */
5587 ret = add_enum_ust_registry(sock, sobjd, name,
5588 entries, nr_entries);
5589 if (ret < 0) {
5590 goto error;
5591 }
5592
5593 break;
5594 }
d0b96690
DG
5595 default:
5596 /* Should NEVER happen. */
5597 assert(0);
5598 }
5599
5600error:
5601 return ret;
5602}
d88aee68
DG
5603
5604/*
5605 * Once the notify socket hangs up, this is called. First, it tries to find the
5606 * corresponding application. On failure, the call_rcu to close the socket is
5607 * executed. If an application is found, it tries to delete it from the notify
5608 * socket hash table. Whathever the result, it proceeds to the call_rcu.
5609 *
5610 * Note that an object needs to be allocated here so on ENOMEM failure, the
5611 * call RCU is not done but the rest of the cleanup is.
5612 */
5613void ust_app_notify_sock_unregister(int sock)
5614{
5615 int err_enomem = 0;
5616 struct lttng_ht_iter iter;
5617 struct ust_app *app;
5618 struct ust_app_notify_sock_obj *obj;
5619
5620 assert(sock >= 0);
5621
5622 rcu_read_lock();
5623
5624 obj = zmalloc(sizeof(*obj));
5625 if (!obj) {
5626 /*
5627 * An ENOMEM is kind of uncool. If this strikes we continue the
5628 * procedure but the call_rcu will not be called. In this case, we
5629 * accept the fd leak rather than possibly creating an unsynchronized
5630 * state between threads.
5631 *
5632 * TODO: The notify object should be created once the notify socket is
5633 * registered and stored independantely from the ust app object. The
5634 * tricky part is to synchronize the teardown of the application and
5635 * this notify object. Let's keep that in mind so we can avoid this
5636 * kind of shenanigans with ENOMEM in the teardown path.
5637 */
5638 err_enomem = 1;
5639 } else {
5640 obj->fd = sock;
5641 }
5642
5643 DBG("UST app notify socket unregister %d", sock);
5644
5645 /*
5646 * Lookup application by notify socket. If this fails, this means that the
5647 * hash table delete has already been done by the application
5648 * unregistration process so we can safely close the notify socket in a
5649 * call RCU.
5650 */
5651 app = find_app_by_notify_sock(sock);
5652 if (!app) {
5653 goto close_socket;
5654 }
5655
5656 iter.iter.node = &app->notify_sock_n.node;
5657
5658 /*
5659 * Whatever happens here either we fail or succeed, in both cases we have
5660 * to close the socket after a grace period to continue to the call RCU
5661 * here. If the deletion is successful, the application is not visible
5662 * anymore by other threads and is it fails it means that it was already
5663 * deleted from the hash table so either way we just have to close the
5664 * socket.
5665 */
5666 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
5667
5668close_socket:
5669 rcu_read_unlock();
5670
5671 /*
5672 * Close socket after a grace period to avoid for the socket to be reused
5673 * before the application object is freed creating potential race between
5674 * threads trying to add unique in the global hash table.
5675 */
5676 if (!err_enomem) {
5677 call_rcu(&obj->head, close_notify_sock_rcu);
5678 }
5679}
f45e313d
DG
5680
5681/*
5682 * Destroy a ust app data structure and free its memory.
5683 */
5684void ust_app_destroy(struct ust_app *app)
5685{
5686 if (!app) {
5687 return;
5688 }
5689
5690 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
5691}
6dc3064a
DG
5692
5693/*
5694 * Take a snapshot for a given UST session. The snapshot is sent to the given
5695 * output.
5696 *
5697 * Return 0 on success or else a negative value.
5698 */
5699int ust_app_snapshot_record(struct ltt_ust_session *usess,
d07ceecd
MD
5700 struct snapshot_output *output, int wait,
5701 uint64_t nb_packets_per_stream)
6dc3064a
DG
5702{
5703 int ret = 0;
7badf927 5704 unsigned int snapshot_done = 0;
6dc3064a
DG
5705 struct lttng_ht_iter iter;
5706 struct ust_app *app;
af706bb7 5707 char pathname[PATH_MAX];
6dc3064a
DG
5708
5709 assert(usess);
5710 assert(output);
5711
5712 rcu_read_lock();
5713
8c924c7b
MD
5714 switch (usess->buffer_type) {
5715 case LTTNG_BUFFER_PER_UID:
5716 {
5717 struct buffer_reg_uid *reg;
6dc3064a 5718
8c924c7b
MD
5719 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5720 struct buffer_reg_channel *reg_chan;
5721 struct consumer_socket *socket;
6dc3064a 5722
8c924c7b
MD
5723 /* Get consumer socket to use to push the metadata.*/
5724 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5725 usess->consumer);
5726 if (!socket) {
5727 ret = -EINVAL;
5728 goto error;
5729 }
6dc3064a 5730
8c924c7b
MD
5731 memset(pathname, 0, sizeof(pathname));
5732 ret = snprintf(pathname, sizeof(pathname),
5733 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH,
5734 reg->uid, reg->bits_per_long);
5735 if (ret < 0) {
5736 PERROR("snprintf snapshot path");
5737 goto error;
5738 }
5739
5740 /* Add the UST default trace dir to path. */
5741 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5742 reg_chan, node.node) {
68808f4e
DG
5743 ret = consumer_snapshot_channel(socket, reg_chan->consumer_key,
5744 output, 0, usess->uid, usess->gid, pathname, wait,
d07ceecd 5745 nb_packets_per_stream);
8c924c7b
MD
5746 if (ret < 0) {
5747 goto error;
5748 }
5749 }
68808f4e
DG
5750 ret = consumer_snapshot_channel(socket,
5751 reg->registry->reg.ust->metadata_key, output, 1,
d07ceecd 5752 usess->uid, usess->gid, pathname, wait, 0);
8c924c7b
MD
5753 if (ret < 0) {
5754 goto error;
5755 }
7badf927 5756 snapshot_done = 1;
af706bb7 5757 }
8c924c7b
MD
5758 break;
5759 }
5760 case LTTNG_BUFFER_PER_PID:
5761 {
5762 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5763 struct consumer_socket *socket;
5764 struct lttng_ht_iter chan_iter;
5765 struct ust_app_channel *ua_chan;
5766 struct ust_app_session *ua_sess;
5767 struct ust_registry_session *registry;
5768
5769 ua_sess = lookup_session_by_app(usess, app);
5770 if (!ua_sess) {
5771 /* Session not associated with this app. */
5772 continue;
5773 }
af706bb7 5774
8c924c7b
MD
5775 /* Get the right consumer socket for the application. */
5776 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5777 output->consumer);
5778 if (!socket) {
5c786ded 5779 ret = -EINVAL;
5c786ded
JD
5780 goto error;
5781 }
5782
8c924c7b
MD
5783 /* Add the UST default trace dir to path. */
5784 memset(pathname, 0, sizeof(pathname));
5785 ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s",
5786 ua_sess->path);
6dc3064a 5787 if (ret < 0) {
8c924c7b 5788 PERROR("snprintf snapshot path");
6dc3064a
DG
5789 goto error;
5790 }
6dc3064a 5791
8c924c7b
MD
5792 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
5793 ua_chan, node.node) {
68808f4e
DG
5794 ret = consumer_snapshot_channel(socket, ua_chan->key, output,
5795 0, ua_sess->euid, ua_sess->egid, pathname, wait,
d07ceecd 5796 nb_packets_per_stream);
8c924c7b
MD
5797 if (ret < 0) {
5798 goto error;
5799 }
5800 }
5801
5802 registry = get_session_registry(ua_sess);
5803 assert(registry);
5804 ret = consumer_snapshot_channel(socket, registry->metadata_key, output,
d07ceecd 5805 1, ua_sess->euid, ua_sess->egid, pathname, wait, 0);
8c924c7b
MD
5806 if (ret < 0) {
5807 goto error;
5808 }
7badf927 5809 snapshot_done = 1;
8c924c7b
MD
5810 }
5811 break;
5812 }
5813 default:
5814 assert(0);
5815 break;
6dc3064a
DG
5816 }
5817
7badf927
DG
5818 if (!snapshot_done) {
5819 /*
5820 * If no snapshot was made and we are not in the error path, this means
5821 * that there are no buffers thus no (prior) application to snapshot
5822 * data from so we have simply NO data.
5823 */
5824 ret = -ENODATA;
5825 }
5826
6dc3064a
DG
5827error:
5828 rcu_read_unlock();
5829 return ret;
5830}
5c786ded
JD
5831
5832/*
d07ceecd 5833 * Return the size taken by one more packet per stream.
5c786ded 5834 */
d07ceecd
MD
5835uint64_t ust_app_get_size_one_more_packet_per_stream(struct ltt_ust_session *usess,
5836 uint64_t cur_nr_packets)
5c786ded 5837{
d07ceecd 5838 uint64_t tot_size = 0;
5c786ded
JD
5839 struct ust_app *app;
5840 struct lttng_ht_iter iter;
5841
5842 assert(usess);
5843
5844 switch (usess->buffer_type) {
5845 case LTTNG_BUFFER_PER_UID:
5846 {
5847 struct buffer_reg_uid *reg;
5848
5849 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5850 struct buffer_reg_channel *reg_chan;
5851
b7064eaa 5852 rcu_read_lock();
5c786ded
JD
5853 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5854 reg_chan, node.node) {
d07ceecd
MD
5855 if (cur_nr_packets >= reg_chan->num_subbuf) {
5856 /*
5857 * Don't take channel into account if we
5858 * already grab all its packets.
5859 */
5860 continue;
5861 }
5862 tot_size += reg_chan->subbuf_size * reg_chan->stream_count;
5c786ded 5863 }
b7064eaa 5864 rcu_read_unlock();
5c786ded
JD
5865 }
5866 break;
5867 }
5868 case LTTNG_BUFFER_PER_PID:
5869 {
5870 rcu_read_lock();
5871 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5872 struct ust_app_channel *ua_chan;
5873 struct ust_app_session *ua_sess;
5874 struct lttng_ht_iter chan_iter;
5875
5876 ua_sess = lookup_session_by_app(usess, app);
5877 if (!ua_sess) {
5878 /* Session not associated with this app. */
5879 continue;
5880 }
5881
5882 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
5883 ua_chan, node.node) {
d07ceecd
MD
5884 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
5885 /*
5886 * Don't take channel into account if we
5887 * already grab all its packets.
5888 */
5889 continue;
5890 }
5891 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
5c786ded
JD
5892 }
5893 }
5894 rcu_read_unlock();
5895 break;
5896 }
5897 default:
5898 assert(0);
5899 break;
5900 }
5901
d07ceecd 5902 return tot_size;
5c786ded 5903}
fb83fe64
JD
5904
5905int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
5906 struct cds_list_head *buffer_reg_uid_list,
5907 struct consumer_output *consumer, uint64_t uchan_id,
5908 int overwrite, uint64_t *discarded, uint64_t *lost)
5909{
5910 int ret;
5911 uint64_t consumer_chan_key;
5912
5913 ret = buffer_reg_uid_consumer_channel_key(
5914 buffer_reg_uid_list, ust_session_id,
5915 uchan_id, &consumer_chan_key);
5916 if (ret < 0) {
5917 goto end;
5918 }
5919
5920 if (overwrite) {
5921 ret = consumer_get_lost_packets(ust_session_id,
5922 consumer_chan_key, consumer, lost);
5923 } else {
5924 ret = consumer_get_discarded_events(ust_session_id,
5925 consumer_chan_key, consumer, discarded);
5926 }
5927
5928end:
5929 return ret;
5930}
5931
5932int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
5933 struct ltt_ust_channel *uchan,
5934 struct consumer_output *consumer, int overwrite,
5935 uint64_t *discarded, uint64_t *lost)
5936{
5937 int ret = 0;
5938 struct lttng_ht_iter iter;
5939 struct lttng_ht_node_str *ua_chan_node;
5940 struct ust_app *app;
5941 struct ust_app_session *ua_sess;
5942 struct ust_app_channel *ua_chan;
5943
5944 rcu_read_lock();
5945 /*
5946 * Iterate over every registered applications, return when we
5947 * found one in the right session and channel.
5948 */
5949 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5950 struct lttng_ht_iter uiter;
5951
5952 ua_sess = lookup_session_by_app(usess, app);
5953 if (ua_sess == NULL) {
5954 continue;
5955 }
5956
5957 /* Get channel */
5958 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5959 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5960 /* If the session is found for the app, the channel must be there */
5961 assert(ua_chan_node);
5962
5963 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5964
5965 if (overwrite) {
5966 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
5967 consumer, lost);
5968 goto end;
5969 } else {
5970 ret = consumer_get_discarded_events(usess->id,
5971 ua_chan->key, consumer, discarded);
5972 goto end;
5973 }
5974 goto end;
5975 }
5976
5977end:
5978 rcu_read_unlock();
5979 return ret;
5980}
This page took 0.413658 seconds and 4 git commands to generate.