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