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