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