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