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