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