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