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