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