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