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