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