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