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