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