common: document why spawn_viewer() is public
[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>
e393070a
JR
29#include <lttng/condition/on-event-internal.h>
30#include <lttng/condition/on-event.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);
d602bd6a 1256 assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT);
267d66aa 1257
d602bd6a 1258 assert(LTTNG_CONDITION_STATUS_OK == lttng_condition_on_event_get_rule(condition, &event_rule));
267d66aa 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;
fc4b93fa 2003 enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
993578ff
JR
2004 int loglevel = -1, ret = 0;
2005 const char *pattern;
2006
2007 /* For now only LTTNG_EVENT_RULE_TYPE_TRACEPOINT are supported. */
2008 assert(lttng_event_rule_get_type(rule) ==
2009 LTTNG_EVENT_RULE_TYPE_TRACEPOINT);
2010
2011 memset(event_notifier, 0, sizeof(*event_notifier));
2012
44760c20
JR
2013 if (lttng_event_rule_targets_agent_domain(rule)) {
2014 /*
2015 * Special event for agents
2016 * The actual meat of the event is in the filter that will be
2017 * attached later on.
2018 * Set the default values for the agent event.
2019 */
2020 pattern = event_get_default_agent_ust_name(
2021 lttng_event_rule_get_domain_type(rule));
2022 loglevel = 0;
fc4b93fa 2023 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
44760c20 2024 } else {
85b05318 2025 const struct lttng_log_level_rule *log_level_rule;
993578ff 2026
85b05318 2027 status = lttng_event_rule_tracepoint_get_pattern(rule, &pattern);
44760c20
JR
2028 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
2029 /* At this point, this is a fatal error. */
2030 abort();
2031 }
993578ff 2032
85b05318
JR
2033 status = lttng_event_rule_tracepoint_get_log_level_rule(
2034 rule, &log_level_rule);
2035 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
fc4b93fa 2036 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
85b05318
JR
2037 } else if (status == LTTNG_EVENT_RULE_STATUS_OK) {
2038 enum lttng_log_level_rule_status llr_status;
2039
2040 switch (lttng_log_level_rule_get_type(log_level_rule)) {
2041 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
2042 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE;
2043 llr_status = lttng_log_level_rule_exactly_get_level(
2044 log_level_rule, &loglevel);
2045 break;
2046 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
2047 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE;
2048 llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
2049 log_level_rule, &loglevel);
2050 break;
2051 default:
2052 abort();
2053 }
993578ff 2054
85b05318
JR
2055 assert(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
2056 } else {
2057 /* At this point this is a fatal error. */
2058 abort();
44760c20 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);
d602bd6a 2098 assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT);
993578ff 2099
d602bd6a
JR
2100 condition_status = lttng_condition_on_event_get_rule(
2101 condition, &event_rule);
267d66aa 2102 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
d602bd6a 2103
267d66aa
JR
2104 assert(event_rule);
2105 assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_TRACEPOINT);
2106
2107 init_ust_event_notifier_from_event_rule(event_rule, &event_notifier);
993578ff
JR
2108 event_notifier.event.token = ua_event_notifier_rule->token;
2109
2110 /* Create UST event notifier against the tracer. */
2111 pthread_mutex_lock(&app->sock_lock);
2112 ret = ustctl_create_event_notifier(app->sock, &event_notifier,
2113 app->event_notifier_group.object,
2114 &ua_event_notifier_rule->obj);
2115 pthread_mutex_unlock(&app->sock_lock);
2116 if (ret < 0) {
2117 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2118 ERR("Error ustctl create event notifier: name = '%s', app = '%s' (ppid: %d), ret = %d",
2119 event_notifier.event.name, app->name,
2120 app->ppid, ret);
2121 } else {
2122 /*
2123 * This is normal behavior, an application can die
2124 * during the creation process. Don't report an error so
2125 * the execution can continue normally.
2126 */
2127 ret = 0;
2128 DBG3("UST app create event notifier failed (application is dead): app = '%s' (ppid = %d)",
2129 app->name, app->ppid);
2130 }
2131
2132 goto error;
2133 }
2134
2135 ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle;
2136
2137 DBG2("UST app event notifier %s created successfully: app = '%s' (ppid: %d), object: %p",
2138 event_notifier.event.name, app->name, app->ppid,
2139 ua_event_notifier_rule->obj);
2140
2141 health_code_update();
2142
2143 /* Set filter if one is present. */
2144 if (ua_event_notifier_rule->filter) {
2145 ret = set_ust_object_filter(app, ua_event_notifier_rule->filter,
2146 ua_event_notifier_rule->obj);
2147 if (ret < 0) {
2148 goto error;
2149 }
2150 }
2151
2152 /* Set exclusions for the event. */
2153 if (ua_event_notifier_rule->exclusion) {
2154 ret = set_ust_object_exclusions(app,
2155 ua_event_notifier_rule->exclusion,
2156 ua_event_notifier_rule->obj);
2157 if (ret < 0) {
2158 goto error;
2159 }
2160 }
f83be61d
JR
2161
2162 /* Set the capture bytecodes. */
d602bd6a 2163 cond_status = lttng_condition_on_event_get_capture_descriptor_count(
f83be61d
JR
2164 condition, &capture_bytecode_count);
2165 assert(cond_status == LTTNG_CONDITION_STATUS_OK);
2166
2167 for (i = 0; i < capture_bytecode_count; i++) {
2168 const struct lttng_bytecode *capture_bytecode =
d602bd6a 2169 lttng_condition_on_event_get_capture_bytecode_at_index(
f83be61d
JR
2170 condition, i);
2171
11f6ce94 2172 ret = set_ust_capture(app, capture_bytecode, i,
f83be61d
JR
2173 ua_event_notifier_rule->obj);
2174 if (ret < 0) {
2175 goto error;
2176 }
2177 }
993578ff
JR
2178
2179 /*
2180 * We now need to explicitly enable the event, since it
2181 * is disabled at creation.
2182 */
2183 ret = enable_ust_object(app, ua_event_notifier_rule->obj);
2184 if (ret < 0) {
2185 /*
2186 * If we hit an EPERM, something is wrong with our enable call.
2187 * If we get an EEXIST, there is a problem on the tracer side
2188 * since we just created it.
2189 */
2190 switch (ret) {
2191 case -LTTNG_UST_ERR_PERM:
2192 /* Code flow problem. */
2193 abort();
2194 case -LTTNG_UST_ERR_EXIST:
2195 /* It's OK for our use case. */
2196 ret = 0;
2197 break;
2198 default:
2199 break;
2200 }
2201
2202 goto error;
2203 }
2204
2205 ua_event_notifier_rule->enabled = true;
2206
2207error:
2208 health_code_update();
2209 return ret;
2210}
2211
5b4a0ec0
DG
2212/*
2213 * Copy data between an UST app event and a LTT event.
2214 */
421cb601 2215static void shadow_copy_event(struct ust_app_event *ua_event,
48842b30
DG
2216 struct ltt_ust_event *uevent)
2217{
b4ffad32
JI
2218 size_t exclusion_alloc_size;
2219
48842b30
DG
2220 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
2221 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
2222
fc34caaa
DG
2223 ua_event->enabled = uevent->enabled;
2224
5b4a0ec0
DG
2225 /* Copy event attributes */
2226 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
2227
53a80697
MD
2228 /* Copy filter bytecode */
2229 if (uevent->filter) {
2b00d462 2230 ua_event->filter = lttng_bytecode_copy(uevent->filter);
025faf73 2231 /* Filter might be NULL here in case of ENONEM. */
53a80697 2232 }
b4ffad32
JI
2233
2234 /* Copy exclusion data */
2235 if (uevent->exclusion) {
51755dc8 2236 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
fc4b93fa 2237 LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count;
b4ffad32 2238 ua_event->exclusion = zmalloc(exclusion_alloc_size);
5f8df26c
JI
2239 if (ua_event->exclusion == NULL) {
2240 PERROR("malloc");
2241 } else {
2242 memcpy(ua_event->exclusion, uevent->exclusion,
2243 exclusion_alloc_size);
b4ffad32
JI
2244 }
2245 }
48842b30
DG
2246}
2247
5b4a0ec0
DG
2248/*
2249 * Copy data between an UST app channel and a LTT channel.
2250 */
421cb601 2251static void shadow_copy_channel(struct ust_app_channel *ua_chan,
48842b30
DG
2252 struct ltt_ust_channel *uchan)
2253{
fc34caaa 2254 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
48842b30
DG
2255
2256 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
2257 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
ffe60014 2258
1624d5b7
JD
2259 ua_chan->tracefile_size = uchan->tracefile_size;
2260 ua_chan->tracefile_count = uchan->tracefile_count;
2261
ffe60014
DG
2262 /* Copy event attributes since the layout is different. */
2263 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
2264 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
2265 ua_chan->attr.overwrite = uchan->attr.overwrite;
2266 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
2267 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
e9404c27 2268 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
ffe60014 2269 ua_chan->attr.output = uchan->attr.output;
491d1539
MD
2270 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
2271
ffe60014
DG
2272 /*
2273 * Note that the attribute channel type is not set since the channel on the
2274 * tracing registry side does not have this information.
2275 */
48842b30 2276
fc34caaa 2277 ua_chan->enabled = uchan->enabled;
7972aab2 2278 ua_chan->tracing_channel_id = uchan->id;
fc34caaa 2279
fc34caaa 2280 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
48842b30
DG
2281}
2282
5b4a0ec0
DG
2283/*
2284 * Copy data between a UST app session and a regular LTT session.
2285 */
421cb601 2286static void shadow_copy_session(struct ust_app_session *ua_sess,
bec39940 2287 struct ltt_ust_session *usess, struct ust_app *app)
48842b30 2288{
477d7741
MD
2289 struct tm *timeinfo;
2290 char datetime[16];
2291 int ret;
d7ba1388 2292 char tmp_shm_path[PATH_MAX];
477d7741 2293
940c4592 2294 timeinfo = localtime(&app->registration_time);
477d7741 2295 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
48842b30 2296
421cb601 2297 DBG2("Shadow copy of session handle %d", ua_sess->handle);
48842b30 2298
7972aab2
DG
2299 ua_sess->tracing_id = usess->id;
2300 ua_sess->id = get_next_session_id();
ff588497
JR
2301 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid);
2302 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid);
2303 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid);
2304 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid);
7972aab2
DG
2305 ua_sess->buffer_type = usess->buffer_type;
2306 ua_sess->bits_per_long = app->bits_per_long;
6addfa37 2307
7972aab2 2308 /* There is only one consumer object per session possible. */
6addfa37 2309 consumer_output_get(usess->consumer);
7972aab2 2310 ua_sess->consumer = usess->consumer;
6addfa37 2311
2bba9e53 2312 ua_sess->output_traces = usess->output_traces;
ecc48a90 2313 ua_sess->live_timer_interval = usess->live_timer_interval;
84ad93e8
DG
2314 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
2315 &usess->metadata_attr);
7972aab2
DG
2316
2317 switch (ua_sess->buffer_type) {
2318 case LTTNG_BUFFER_PER_PID:
2319 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
dec56f6c 2320 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid,
7972aab2
DG
2321 datetime);
2322 break;
2323 case LTTNG_BUFFER_PER_UID:
2324 ret = snprintf(ua_sess->path, sizeof(ua_sess->path),
470cc211 2325 DEFAULT_UST_TRACE_UID_PATH,
ff588497 2326 lttng_credentials_get_uid(&ua_sess->real_credentials),
470cc211 2327 app->bits_per_long);
7972aab2
DG
2328 break;
2329 default:
2330 assert(0);
2331 goto error;
2332 }
477d7741
MD
2333 if (ret < 0) {
2334 PERROR("asprintf UST shadow copy session");
477d7741 2335 assert(0);
7972aab2 2336 goto error;
477d7741
MD
2337 }
2338
3d071855
MD
2339 strncpy(ua_sess->root_shm_path, usess->root_shm_path,
2340 sizeof(ua_sess->root_shm_path));
2341 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
d7ba1388
MD
2342 strncpy(ua_sess->shm_path, usess->shm_path,
2343 sizeof(ua_sess->shm_path));
2344 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2345 if (ua_sess->shm_path[0]) {
2346 switch (ua_sess->buffer_type) {
2347 case LTTNG_BUFFER_PER_PID:
2348 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
5da88b0f 2349 "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
d7ba1388
MD
2350 app->name, app->pid, datetime);
2351 break;
2352 case LTTNG_BUFFER_PER_UID:
2353 ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path),
5da88b0f 2354 "/" DEFAULT_UST_TRACE_UID_PATH,
d7ba1388
MD
2355 app->uid, app->bits_per_long);
2356 break;
2357 default:
2358 assert(0);
2359 goto error;
2360 }
2361 if (ret < 0) {
2362 PERROR("sprintf UST shadow copy session");
2363 assert(0);
2364 goto error;
2365 }
2366 strncat(ua_sess->shm_path, tmp_shm_path,
2367 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
2368 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2369 }
6addfa37 2370 return;
7972aab2
DG
2371
2372error:
6addfa37 2373 consumer_output_put(ua_sess->consumer);
48842b30
DG
2374}
2375
78f0bacd
DG
2376/*
2377 * Lookup sesison wrapper.
2378 */
84cd17c6 2379static
fb9a95c4 2380void __lookup_session_by_app(const struct ltt_ust_session *usess,
bec39940 2381 struct ust_app *app, struct lttng_ht_iter *iter)
84cd17c6
MD
2382{
2383 /* Get right UST app session from app */
d9bf3ca4 2384 lttng_ht_lookup(app->sessions, &usess->id, iter);
84cd17c6
MD
2385}
2386
421cb601
DG
2387/*
2388 * Return ust app session from the app session hashtable using the UST session
a991f516 2389 * id.
421cb601 2390 */
48842b30 2391static struct ust_app_session *lookup_session_by_app(
fb9a95c4 2392 const struct ltt_ust_session *usess, struct ust_app *app)
48842b30 2393{
bec39940 2394 struct lttng_ht_iter iter;
d9bf3ca4 2395 struct lttng_ht_node_u64 *node;
48842b30 2396
84cd17c6 2397 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 2398 node = lttng_ht_iter_get_node_u64(&iter);
48842b30
DG
2399 if (node == NULL) {
2400 goto error;
2401 }
2402
2403 return caa_container_of(node, struct ust_app_session, node);
2404
2405error:
2406 return NULL;
2407}
2408
7972aab2
DG
2409/*
2410 * Setup buffer registry per PID for the given session and application. If none
2411 * is found, a new one is created, added to the global registry and
2412 * initialized. If regp is valid, it's set with the newly created object.
2413 *
2414 * Return 0 on success or else a negative value.
2415 */
2416static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
2417 struct ust_app *app, struct buffer_reg_pid **regp)
2418{
2419 int ret = 0;
2420 struct buffer_reg_pid *reg_pid;
2421
2422 assert(ua_sess);
2423 assert(app);
2424
2425 rcu_read_lock();
2426
2427 reg_pid = buffer_reg_pid_find(ua_sess->id);
2428 if (!reg_pid) {
2429 /*
2430 * This is the create channel path meaning that if there is NO
2431 * registry available, we have to create one for this session.
2432 */
d7ba1388 2433 ret = buffer_reg_pid_create(ua_sess->id, &reg_pid,
3d071855 2434 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2435 if (ret < 0) {
2436 goto error;
2437 }
7972aab2
DG
2438 } else {
2439 goto end;
2440 }
2441
2442 /* Initialize registry. */
2443 ret = ust_registry_session_init(&reg_pid->registry->reg.ust, app,
2444 app->bits_per_long, app->uint8_t_alignment,
2445 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf 2446 app->uint64_t_alignment, app->long_alignment,
470cc211
JG
2447 app->byte_order, app->version.major, app->version.minor,
2448 reg_pid->root_shm_path, reg_pid->shm_path,
ff588497
JR
2449 lttng_credentials_get_uid(&ua_sess->effective_credentials),
2450 lttng_credentials_get_gid(&ua_sess->effective_credentials),
2451 ua_sess->tracing_id,
8de88061 2452 app->uid);
7972aab2 2453 if (ret < 0) {
286c991a
MD
2454 /*
2455 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2456 * destroy the buffer registry, because it is always expected
2457 * that if the buffer registry can be found, its ust registry is
2458 * non-NULL.
2459 */
2460 buffer_reg_pid_destroy(reg_pid);
7972aab2
DG
2461 goto error;
2462 }
2463
286c991a
MD
2464 buffer_reg_pid_add(reg_pid);
2465
7972aab2
DG
2466 DBG3("UST app buffer registry per PID created successfully");
2467
2468end:
2469 if (regp) {
2470 *regp = reg_pid;
2471 }
2472error:
2473 rcu_read_unlock();
2474 return ret;
2475}
2476
2477/*
2478 * Setup buffer registry per UID for the given session and application. If none
2479 * is found, a new one is created, added to the global registry and
2480 * initialized. If regp is valid, it's set with the newly created object.
2481 *
2482 * Return 0 on success or else a negative value.
2483 */
2484static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
d7ba1388 2485 struct ust_app_session *ua_sess,
7972aab2
DG
2486 struct ust_app *app, struct buffer_reg_uid **regp)
2487{
2488 int ret = 0;
2489 struct buffer_reg_uid *reg_uid;
2490
2491 assert(usess);
2492 assert(app);
2493
2494 rcu_read_lock();
2495
2496 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
2497 if (!reg_uid) {
2498 /*
2499 * This is the create channel path meaning that if there is NO
2500 * registry available, we have to create one for this session.
2501 */
2502 ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid,
3d071855
MD
2503 LTTNG_DOMAIN_UST, &reg_uid,
2504 ua_sess->root_shm_path, ua_sess->shm_path);
7972aab2
DG
2505 if (ret < 0) {
2506 goto error;
2507 }
7972aab2
DG
2508 } else {
2509 goto end;
2510 }
2511
2512 /* Initialize registry. */
af6142cf 2513 ret = ust_registry_session_init(&reg_uid->registry->reg.ust, NULL,
7972aab2
DG
2514 app->bits_per_long, app->uint8_t_alignment,
2515 app->uint16_t_alignment, app->uint32_t_alignment,
af6142cf
MD
2516 app->uint64_t_alignment, app->long_alignment,
2517 app->byte_order, app->version.major,
3d071855 2518 app->version.minor, reg_uid->root_shm_path,
8de88061
JR
2519 reg_uid->shm_path, usess->uid, usess->gid,
2520 ua_sess->tracing_id, app->uid);
7972aab2 2521 if (ret < 0) {
286c991a
MD
2522 /*
2523 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2524 * destroy the buffer registry, because it is always expected
2525 * that if the buffer registry can be found, its ust registry is
2526 * non-NULL.
2527 */
2528 buffer_reg_uid_destroy(reg_uid, NULL);
7972aab2
DG
2529 goto error;
2530 }
2531 /* Add node to teardown list of the session. */
2532 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2533
286c991a 2534 buffer_reg_uid_add(reg_uid);
7972aab2 2535
286c991a 2536 DBG3("UST app buffer registry per UID created successfully");
7972aab2
DG
2537end:
2538 if (regp) {
2539 *regp = reg_uid;
2540 }
2541error:
2542 rcu_read_unlock();
2543 return ret;
2544}
2545
421cb601 2546/*
3d8ca23b 2547 * Create a session on the tracer side for the given app.
421cb601 2548 *
3d8ca23b
DG
2549 * On success, ua_sess_ptr is populated with the session pointer or else left
2550 * untouched. If the session was created, is_created is set to 1. On error,
2551 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2552 * be NULL.
2553 *
2554 * Returns 0 on success or else a negative code which is either -ENOMEM or
2555 * -ENOTCONN which is the default code if the ustctl_create_session fails.
421cb601 2556 */
03f91eaa 2557static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
3d8ca23b
DG
2558 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
2559 int *is_created)
421cb601 2560{
3d8ca23b 2561 int ret, created = 0;
421cb601
DG
2562 struct ust_app_session *ua_sess;
2563
3d8ca23b
DG
2564 assert(usess);
2565 assert(app);
2566 assert(ua_sess_ptr);
2567
840cb59c 2568 health_code_update();
86acf0da 2569
421cb601
DG
2570 ua_sess = lookup_session_by_app(usess, app);
2571 if (ua_sess == NULL) {
d9bf3ca4 2572 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
852d0037 2573 app->pid, usess->id);
40bbd087 2574 ua_sess = alloc_ust_app_session();
421cb601
DG
2575 if (ua_sess == NULL) {
2576 /* Only malloc can failed so something is really wrong */
3d8ca23b
DG
2577 ret = -ENOMEM;
2578 goto error;
421cb601 2579 }
477d7741 2580 shadow_copy_session(ua_sess, usess, app);
3d8ca23b 2581 created = 1;
421cb601
DG
2582 }
2583
7972aab2
DG
2584 switch (usess->buffer_type) {
2585 case LTTNG_BUFFER_PER_PID:
2586 /* Init local registry. */
2587 ret = setup_buffer_reg_pid(ua_sess, app, NULL);
421cb601 2588 if (ret < 0) {
e64207cf 2589 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2590 goto error;
2591 }
2592 break;
2593 case LTTNG_BUFFER_PER_UID:
2594 /* Look for a global registry. If none exists, create one. */
d7ba1388 2595 ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL);
7972aab2 2596 if (ret < 0) {
e64207cf 2597 delete_ust_app_session(-1, ua_sess, app);
7972aab2
DG
2598 goto error;
2599 }
2600 break;
2601 default:
2602 assert(0);
2603 ret = -EINVAL;
2604 goto error;
2605 }
2606
2607 health_code_update();
2608
2609 if (ua_sess->handle == -1) {
fb45065e 2610 pthread_mutex_lock(&app->sock_lock);
7972aab2 2611 ret = ustctl_create_session(app->sock);
fb45065e 2612 pthread_mutex_unlock(&app->sock_lock);
7972aab2
DG
2613 if (ret < 0) {
2614 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2615 ERR("Creating session for app pid %d with ret %d",
ffe60014
DG
2616 app->pid, ret);
2617 } else {
2618 DBG("UST app creating session failed. Application is dead");
3757b385
DG
2619 /*
2620 * This is normal behavior, an application can die during the
2621 * creation process. Don't report an error so the execution can
2622 * continue normally. This will get flagged ENOTCONN and the
2623 * caller will handle it.
2624 */
2625 ret = 0;
ffe60014 2626 }
d0b96690 2627 delete_ust_app_session(-1, ua_sess, app);
3d8ca23b
DG
2628 if (ret != -ENOMEM) {
2629 /*
2630 * Tracer is probably gone or got an internal error so let's
2631 * behave like it will soon unregister or not usable.
2632 */
2633 ret = -ENOTCONN;
2634 }
2635 goto error;
421cb601
DG
2636 }
2637
7972aab2
DG
2638 ua_sess->handle = ret;
2639
2640 /* Add ust app session to app's HT */
d9bf3ca4
MD
2641 lttng_ht_node_init_u64(&ua_sess->node,
2642 ua_sess->tracing_id);
2643 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
10b56aef
MD
2644 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2645 lttng_ht_add_unique_ulong(app->ust_sessions_objd,
2646 &ua_sess->ust_objd_node);
7972aab2
DG
2647
2648 DBG2("UST app session created successfully with handle %d", ret);
2649 }
2650
2651 *ua_sess_ptr = ua_sess;
2652 if (is_created) {
2653 *is_created = created;
2654 }
2655
2656 /* Everything went well. */
2657 ret = 0;
2658
2659error:
2660 health_code_update();
2661 return ret;
2662}
2663
6a6b2068
JG
2664/*
2665 * Match function for a hash table lookup of ust_app_ctx.
2666 *
2667 * It matches an ust app context based on the context type and, in the case
2668 * of perf counters, their name.
2669 */
2670static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2671{
2672 struct ust_app_ctx *ctx;
bdf64013 2673 const struct lttng_ust_context_attr *key;
6a6b2068
JG
2674
2675 assert(node);
2676 assert(_key);
2677
2678 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2679 key = _key;
2680
2681 /* Context type */
2682 if (ctx->ctx.ctx != key->ctx) {
2683 goto no_match;
2684 }
2685
bdf64013 2686 switch(key->ctx) {
fc4b93fa 2687 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
6a6b2068 2688 if (strncmp(key->u.perf_counter.name,
bdf64013
JG
2689 ctx->ctx.u.perf_counter.name,
2690 sizeof(key->u.perf_counter.name))) {
2691 goto no_match;
2692 }
2693 break;
fc4b93fa 2694 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
bdf64013
JG
2695 if (strcmp(key->u.app_ctx.provider_name,
2696 ctx->ctx.u.app_ctx.provider_name) ||
2697 strcmp(key->u.app_ctx.ctx_name,
2698 ctx->ctx.u.app_ctx.ctx_name)) {
6a6b2068
JG
2699 goto no_match;
2700 }
bdf64013
JG
2701 break;
2702 default:
2703 break;
6a6b2068
JG
2704 }
2705
2706 /* Match. */
2707 return 1;
2708
2709no_match:
2710 return 0;
2711}
2712
2713/*
2714 * Lookup for an ust app context from an lttng_ust_context.
2715 *
be184a0f 2716 * Must be called while holding RCU read side lock.
6a6b2068
JG
2717 * Return an ust_app_ctx object or NULL on error.
2718 */
2719static
2720struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
bdf64013 2721 struct lttng_ust_context_attr *uctx)
6a6b2068
JG
2722{
2723 struct lttng_ht_iter iter;
2724 struct lttng_ht_node_ulong *node;
2725 struct ust_app_ctx *app_ctx = NULL;
2726
2727 assert(uctx);
2728 assert(ht);
2729
2730 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2731 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2732 ht_match_ust_app_ctx, uctx, &iter.iter);
2733 node = lttng_ht_iter_get_node_ulong(&iter);
2734 if (!node) {
2735 goto end;
2736 }
2737
2738 app_ctx = caa_container_of(node, struct ust_app_ctx, node);
2739
2740end:
2741 return app_ctx;
2742}
2743
7972aab2
DG
2744/*
2745 * Create a context for the channel on the tracer.
2746 *
2747 * Called with UST app session lock held and a RCU read side lock.
2748 */
2749static
c9edf082 2750int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
f3db82be 2751 struct lttng_ust_context_attr *uctx,
7972aab2
DG
2752 struct ust_app *app)
2753{
2754 int ret = 0;
7972aab2
DG
2755 struct ust_app_ctx *ua_ctx;
2756
2757 DBG2("UST app adding context to channel %s", ua_chan->name);
2758
6a6b2068
JG
2759 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2760 if (ua_ctx) {
7972aab2
DG
2761 ret = -EEXIST;
2762 goto error;
2763 }
2764
2765 ua_ctx = alloc_ust_app_ctx(uctx);
2766 if (ua_ctx == NULL) {
2767 /* malloc failed */
7682f304 2768 ret = -ENOMEM;
7972aab2
DG
2769 goto error;
2770 }
2771
2772 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
aa3514e9 2773 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
31746f93 2774 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
7972aab2
DG
2775
2776 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2777 if (ret < 0) {
2778 goto error;
2779 }
2780
2781error:
2782 return ret;
2783}
2784
2785/*
2786 * Enable on the tracer side a ust app event for the session and channel.
2787 *
2788 * Called with UST app session lock held.
2789 */
2790static
2791int enable_ust_app_event(struct ust_app_session *ua_sess,
2792 struct ust_app_event *ua_event, struct ust_app *app)
2793{
2794 int ret;
2795
3428a1b7 2796 ret = enable_ust_object(app, ua_event->obj);
7972aab2
DG
2797 if (ret < 0) {
2798 goto error;
2799 }
2800
2801 ua_event->enabled = 1;
2802
2803error:
2804 return ret;
2805}
2806
2807/*
2808 * Disable on the tracer side a ust app event for the session and channel.
2809 */
2810static int disable_ust_app_event(struct ust_app_session *ua_sess,
2811 struct ust_app_event *ua_event, struct ust_app *app)
2812{
2813 int ret;
2814
e2456d0a 2815 ret = disable_ust_object(app, ua_event->obj);
7972aab2
DG
2816 if (ret < 0) {
2817 goto error;
2818 }
2819
2820 ua_event->enabled = 0;
2821
2822error:
2823 return ret;
2824}
2825
2826/*
2827 * Lookup ust app channel for session and disable it on the tracer side.
2828 */
2829static
2830int disable_ust_app_channel(struct ust_app_session *ua_sess,
2831 struct ust_app_channel *ua_chan, struct ust_app *app)
2832{
2833 int ret;
2834
2835 ret = disable_ust_channel(app, ua_sess, ua_chan);
2836 if (ret < 0) {
2837 goto error;
2838 }
2839
2840 ua_chan->enabled = 0;
2841
2842error:
2843 return ret;
2844}
2845
2846/*
2847 * Lookup ust app channel for session and enable it on the tracer side. This
2848 * MUST be called with a RCU read side lock acquired.
2849 */
2850static int enable_ust_app_channel(struct ust_app_session *ua_sess,
2851 struct ltt_ust_channel *uchan, struct ust_app *app)
2852{
2853 int ret = 0;
2854 struct lttng_ht_iter iter;
2855 struct lttng_ht_node_str *ua_chan_node;
2856 struct ust_app_channel *ua_chan;
2857
2858 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2859 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2860 if (ua_chan_node == NULL) {
d9bf3ca4 2861 DBG2("Unable to find channel %s in ust session id %" PRIu64,
7972aab2
DG
2862 uchan->name, ua_sess->tracing_id);
2863 goto error;
2864 }
2865
2866 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2867
2868 ret = enable_ust_channel(app, ua_sess, ua_chan);
2869 if (ret < 0) {
2870 goto error;
2871 }
2872
2873error:
2874 return ret;
2875}
2876
2877/*
2878 * Ask the consumer to create a channel and get it if successful.
2879 *
fad1ed2f
JR
2880 * Called with UST app session lock held.
2881 *
7972aab2
DG
2882 * Return 0 on success or else a negative value.
2883 */
2884static int do_consumer_create_channel(struct ltt_ust_session *usess,
2885 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
e098433c
JG
2886 int bitness, struct ust_registry_session *registry,
2887 uint64_t trace_archive_id)
7972aab2
DG
2888{
2889 int ret;
2890 unsigned int nb_fd = 0;
2891 struct consumer_socket *socket;
2892
2893 assert(usess);
2894 assert(ua_sess);
2895 assert(ua_chan);
2896 assert(registry);
2897
2898 rcu_read_lock();
2899 health_code_update();
2900
2901 /* Get the right consumer socket for the application. */
2902 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
2903 if (!socket) {
2904 ret = -EINVAL;
2905 goto error;
2906 }
2907
2908 health_code_update();
2909
2910 /* Need one fd for the channel. */
2911 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
2912 if (ret < 0) {
2913 ERR("Exhausted number of available FD upon create channel");
2914 goto error;
2915 }
2916
2917 /*
2918 * Ask consumer to create channel. The consumer will return the number of
2919 * stream we have to expect.
2920 */
2921 ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket,
d2956687 2922 registry, usess->current_trace_chunk);
7972aab2
DG
2923 if (ret < 0) {
2924 goto error_ask;
2925 }
2926
2927 /*
2928 * Compute the number of fd needed before receiving them. It must be 2 per
2929 * stream (2 being the default value here).
2930 */
2931 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
2932
2933 /* Reserve the amount of file descriptor we need. */
2934 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
2935 if (ret < 0) {
2936 ERR("Exhausted number of available FD upon create channel");
2937 goto error_fd_get_stream;
2938 }
2939
2940 health_code_update();
2941
2942 /*
2943 * Now get the channel from the consumer. This call wil populate the stream
2944 * list of that channel and set the ust objects.
2945 */
d9078d0c
DG
2946 if (usess->consumer->enabled) {
2947 ret = ust_consumer_get_channel(socket, ua_chan);
2948 if (ret < 0) {
2949 goto error_destroy;
2950 }
7972aab2
DG
2951 }
2952
2953 rcu_read_unlock();
2954 return 0;
2955
2956error_destroy:
2957 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
2958error_fd_get_stream:
2959 /*
2960 * Initiate a destroy channel on the consumer since we had an error
2961 * handling it on our side. The return value is of no importance since we
2962 * already have a ret value set by the previous error that we need to
2963 * return.
2964 */
2965 (void) ust_consumer_destroy_channel(socket, ua_chan);
2966error_ask:
2967 lttng_fd_put(LTTNG_FD_APPS, 1);
2968error:
2969 health_code_update();
2970 rcu_read_unlock();
2971 return ret;
2972}
2973
2974/*
2975 * Duplicate the ust data object of the ust app stream and save it in the
2976 * buffer registry stream.
2977 *
2978 * Return 0 on success or else a negative value.
2979 */
2980static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
2981 struct ust_app_stream *stream)
2982{
2983 int ret;
2984
2985 assert(reg_stream);
2986 assert(stream);
2987
2988 /* Reserve the amount of file descriptor we need. */
2989 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2990 if (ret < 0) {
2991 ERR("Exhausted number of available FD upon duplicate stream");
2992 goto error;
2993 }
2994
2995 /* Duplicate object for stream once the original is in the registry. */
2996 ret = ustctl_duplicate_ust_object_data(&stream->obj,
2997 reg_stream->obj.ust);
2998 if (ret < 0) {
2999 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3000 reg_stream->obj.ust, stream->obj, ret);
3001 lttng_fd_put(LTTNG_FD_APPS, 2);
3002 goto error;
3003 }
3004 stream->handle = stream->obj->handle;
3005
3006error:
3007 return ret;
3008}
3009
3010/*
3011 * Duplicate the ust data object of the ust app. channel and save it in the
3012 * buffer registry channel.
3013 *
3014 * Return 0 on success or else a negative value.
3015 */
3273699d 3016static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan,
7972aab2
DG
3017 struct ust_app_channel *ua_chan)
3018{
3019 int ret;
3020
3273699d 3021 assert(buf_reg_chan);
7972aab2
DG
3022 assert(ua_chan);
3023
3024 /* Need two fds for the channel. */
3025 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3026 if (ret < 0) {
3027 ERR("Exhausted number of available FD upon duplicate channel");
3028 goto error_fd_get;
3029 }
3030
3031 /* Duplicate object for stream once the original is in the registry. */
3273699d 3032 ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust);
7972aab2
DG
3033 if (ret < 0) {
3034 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3273699d 3035 buf_reg_chan->obj.ust, ua_chan->obj, ret);
7972aab2
DG
3036 goto error;
3037 }
3038 ua_chan->handle = ua_chan->obj->handle;
3039
3040 return 0;
3041
3042error:
3043 lttng_fd_put(LTTNG_FD_APPS, 1);
3044error_fd_get:
3045 return ret;
3046}
3047
3048/*
3049 * For a given channel buffer registry, setup all streams of the given ust
3050 * application channel.
3051 *
3052 * Return 0 on success or else a negative value.
3053 */
3273699d 3054static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan,
fb45065e
MD
3055 struct ust_app_channel *ua_chan,
3056 struct ust_app *app)
7972aab2
DG
3057{
3058 int ret = 0;
3059 struct ust_app_stream *stream, *stmp;
3060
3273699d 3061 assert(buf_reg_chan);
7972aab2
DG
3062 assert(ua_chan);
3063
3064 DBG2("UST app setup buffer registry stream");
3065
3066 /* Send all streams to application. */
3067 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
3068 struct buffer_reg_stream *reg_stream;
3069
3070 ret = buffer_reg_stream_create(&reg_stream);
3071 if (ret < 0) {
3072 goto error;
3073 }
3074
3075 /*
3076 * Keep original pointer and nullify it in the stream so the delete
3077 * stream call does not release the object.
3078 */
3079 reg_stream->obj.ust = stream->obj;
3080 stream->obj = NULL;
3273699d 3081 buffer_reg_stream_add(reg_stream, buf_reg_chan);
421cb601 3082
7972aab2
DG
3083 /* We don't need the streams anymore. */
3084 cds_list_del(&stream->list);
fb45065e 3085 delete_ust_app_stream(-1, stream, app);
7972aab2 3086 }
421cb601 3087
7972aab2
DG
3088error:
3089 return ret;
3090}
3091
3092/*
3093 * Create a buffer registry channel for the given session registry and
3094 * application channel object. If regp pointer is valid, it's set with the
3095 * created object. Important, the created object is NOT added to the session
3096 * registry hash table.
3097 *
3098 * Return 0 on success else a negative value.
3099 */
3100static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3101 struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp)
3102{
3103 int ret;
3273699d 3104 struct buffer_reg_channel *buf_reg_chan = NULL;
7972aab2
DG
3105
3106 assert(reg_sess);
3107 assert(ua_chan);
3108
3109 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
3110
3111 /* Create buffer registry channel. */
3273699d 3112 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan);
7972aab2
DG
3113 if (ret < 0) {
3114 goto error_create;
421cb601 3115 }
3273699d
FD
3116 assert(buf_reg_chan);
3117 buf_reg_chan->consumer_key = ua_chan->key;
3118 buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
3119 buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
421cb601 3120
7972aab2
DG
3121 /* Create and add a channel registry to session. */
3122 ret = ust_registry_channel_add(reg_sess->reg.ust,
3123 ua_chan->tracing_channel_id);
3124 if (ret < 0) {
3125 goto error;
d88aee68 3126 }
3273699d 3127 buffer_reg_channel_add(reg_sess, buf_reg_chan);
d88aee68 3128
7972aab2 3129 if (regp) {
3273699d 3130 *regp = buf_reg_chan;
3d8ca23b 3131 }
d88aee68 3132
7972aab2 3133 return 0;
3d8ca23b
DG
3134
3135error:
7972aab2 3136 /* Safe because the registry channel object was not added to any HT. */
3273699d 3137 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
7972aab2 3138error_create:
3d8ca23b 3139 return ret;
421cb601
DG
3140}
3141
55cc08a6 3142/*
7972aab2
DG
3143 * Setup buffer registry channel for the given session registry and application
3144 * channel object. If regp pointer is valid, it's set with the created object.
d0b96690 3145 *
7972aab2 3146 * Return 0 on success else a negative value.
55cc08a6 3147 */
7972aab2 3148static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3273699d 3149 struct ust_app_channel *ua_chan, struct buffer_reg_channel *buf_reg_chan,
fb45065e 3150 struct ust_app *app)
55cc08a6 3151{
7972aab2 3152 int ret;
55cc08a6 3153
7972aab2 3154 assert(reg_sess);
3273699d 3155 assert(buf_reg_chan);
7972aab2
DG
3156 assert(ua_chan);
3157 assert(ua_chan->obj);
55cc08a6 3158
7972aab2 3159 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
55cc08a6 3160
7972aab2 3161 /* Setup all streams for the registry. */
3273699d 3162 ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app);
7972aab2 3163 if (ret < 0) {
55cc08a6
DG
3164 goto error;
3165 }
3166
3273699d 3167 buf_reg_chan->obj.ust = ua_chan->obj;
7972aab2 3168 ua_chan->obj = NULL;
55cc08a6 3169
7972aab2 3170 return 0;
55cc08a6
DG
3171
3172error:
3273699d
FD
3173 buffer_reg_channel_remove(reg_sess, buf_reg_chan);
3174 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
55cc08a6
DG
3175 return ret;
3176}
3177
edb67388 3178/*
7972aab2 3179 * Send buffer registry channel to the application.
d0b96690 3180 *
7972aab2 3181 * Return 0 on success else a negative value.
edb67388 3182 */
3273699d 3183static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan,
7972aab2
DG
3184 struct ust_app *app, struct ust_app_session *ua_sess,
3185 struct ust_app_channel *ua_chan)
edb67388
DG
3186{
3187 int ret;
7972aab2 3188 struct buffer_reg_stream *reg_stream;
edb67388 3189
3273699d 3190 assert(buf_reg_chan);
7972aab2
DG
3191 assert(app);
3192 assert(ua_sess);
3193 assert(ua_chan);
3194
3195 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
3196
3273699d 3197 ret = duplicate_channel_object(buf_reg_chan, ua_chan);
edb67388
DG
3198 if (ret < 0) {
3199 goto error;
3200 }
3201
7972aab2
DG
3202 /* Send channel to the application. */
3203 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
a7169585
MD
3204 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3205 ret = -ENOTCONN; /* Caused by app exiting. */
3206 goto error;
3207 } else if (ret < 0) {
7972aab2
DG
3208 goto error;
3209 }
3210
3211 health_code_update();
3212
3213 /* Send all streams to application. */
3273699d
FD
3214 pthread_mutex_lock(&buf_reg_chan->stream_list_lock);
3215 cds_list_for_each_entry(reg_stream, &buf_reg_chan->streams, lnode) {
7972aab2
DG
3216 struct ust_app_stream stream;
3217
3218 ret = duplicate_stream_object(reg_stream, &stream);
3219 if (ret < 0) {
3220 goto error_stream_unlock;
3221 }
3222
3223 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
3224 if (ret < 0) {
fb45065e 3225 (void) release_ust_app_stream(-1, &stream, app);
a7169585
MD
3226 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3227 ret = -ENOTCONN; /* Caused by app exiting. */
a7169585 3228 }
7972aab2
DG
3229 goto error_stream_unlock;
3230 }
edb67388 3231
7972aab2
DG
3232 /*
3233 * The return value is not important here. This function will output an
3234 * error if needed.
3235 */
fb45065e 3236 (void) release_ust_app_stream(-1, &stream, app);
7972aab2
DG
3237 }
3238 ua_chan->is_sent = 1;
3239
3240error_stream_unlock:
3273699d 3241 pthread_mutex_unlock(&buf_reg_chan->stream_list_lock);
edb67388
DG
3242error:
3243 return ret;
3244}
3245
9730260e 3246/*
7972aab2
DG
3247 * Create and send to the application the created buffers with per UID buffers.
3248 *
9acdc1d6 3249 * This MUST be called with a RCU read side lock acquired.
71e0a100 3250 * The session list lock and the session's lock must be acquired.
9acdc1d6 3251 *
7972aab2 3252 * Return 0 on success else a negative value.
9730260e 3253 */
7972aab2
DG
3254static int create_channel_per_uid(struct ust_app *app,
3255 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3256 struct ust_app_channel *ua_chan)
9730260e
DG
3257{
3258 int ret;
7972aab2 3259 struct buffer_reg_uid *reg_uid;
3273699d 3260 struct buffer_reg_channel *buf_reg_chan;
e32d7f27 3261 struct ltt_session *session = NULL;
e098433c 3262 enum lttng_error_code notification_ret;
3273699d 3263 struct ust_registry_channel *ust_reg_chan;
9730260e 3264
7972aab2
DG
3265 assert(app);
3266 assert(usess);
3267 assert(ua_sess);
3268 assert(ua_chan);
3269
3270 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
3271
3272 reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid);
3273 /*
3274 * The session creation handles the creation of this global registry
3275 * object. If none can be find, there is a code flow problem or a
3276 * teardown race.
3277 */
3278 assert(reg_uid);
3279
3273699d 3280 buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id,
7972aab2 3281 reg_uid);
3273699d 3282 if (buf_reg_chan) {
2721f7ea
JG
3283 goto send_channel;
3284 }
7972aab2 3285
2721f7ea 3286 /* Create the buffer registry channel object. */
3273699d 3287 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan);
2721f7ea
JG
3288 if (ret < 0) {
3289 ERR("Error creating the UST channel \"%s\" registry instance",
f14256d6 3290 ua_chan->name);
2721f7ea
JG
3291 goto error;
3292 }
f14256d6 3293
e098433c
JG
3294 session = session_find_by_id(ua_sess->tracing_id);
3295 assert(session);
3296 assert(pthread_mutex_trylock(&session->lock));
3297 assert(session_trylock_list());
3298
2721f7ea
JG
3299 /*
3300 * Create the buffers on the consumer side. This call populates the
3301 * ust app channel object with all streams and data object.
3302 */
3303 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 3304 app->bits_per_long, reg_uid->registry->reg.ust,
d2956687 3305 session->most_recent_chunk_id.value);
2721f7ea
JG
3306 if (ret < 0) {
3307 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3308 ua_chan->name);
7972aab2
DG
3309
3310 /*
2721f7ea
JG
3311 * Let's remove the previously created buffer registry channel so
3312 * it's not visible anymore in the session registry.
7972aab2 3313 */
2721f7ea
JG
3314 ust_registry_channel_del_free(reg_uid->registry->reg.ust,
3315 ua_chan->tracing_channel_id, false);
3273699d
FD
3316 buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan);
3317 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
2721f7ea 3318 goto error;
7972aab2
DG
3319 }
3320
2721f7ea
JG
3321 /*
3322 * Setup the streams and add it to the session registry.
3323 */
3324 ret = setup_buffer_reg_channel(reg_uid->registry,
3273699d 3325 ua_chan, buf_reg_chan, app);
2721f7ea
JG
3326 if (ret < 0) {
3327 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
3328 goto error;
3329 }
3330
e098433c
JG
3331 /* Notify the notification subsystem of the channel's creation. */
3332 pthread_mutex_lock(&reg_uid->registry->reg.ust->lock);
3273699d 3333 ust_reg_chan = ust_registry_channel_find(reg_uid->registry->reg.ust,
e098433c 3334 ua_chan->tracing_channel_id);
3273699d
FD
3335 assert(ust_reg_chan);
3336 ust_reg_chan->consumer_key = ua_chan->key;
3337 ust_reg_chan = NULL;
e098433c 3338 pthread_mutex_unlock(&reg_uid->registry->reg.ust->lock);
e9404c27 3339
e098433c
JG
3340 notification_ret = notification_thread_command_add_channel(
3341 notification_thread_handle, session->name,
ff588497
JR
3342 lttng_credentials_get_uid(&ua_sess->effective_credentials),
3343 lttng_credentials_get_gid(&ua_sess->effective_credentials),
3344 ua_chan->name,
470cc211 3345 ua_chan->key, LTTNG_DOMAIN_UST,
e098433c
JG
3346 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3347 if (notification_ret != LTTNG_OK) {
3348 ret = - (int) notification_ret;
3349 ERR("Failed to add channel to notification thread");
3350 goto error;
e9404c27
JG
3351 }
3352
2721f7ea 3353send_channel:
66ff8e3f 3354 /* Send buffers to the application. */
3273699d 3355 ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan);
66ff8e3f
JG
3356 if (ret < 0) {
3357 if (ret != -ENOTCONN) {
3358 ERR("Error sending channel to application");
3359 }
3360 goto error;
3361 }
3362
9730260e 3363error:
e32d7f27
JG
3364 if (session) {
3365 session_put(session);
3366 }
9730260e
DG
3367 return ret;
3368}
3369
78f0bacd 3370/*
7972aab2
DG
3371 * Create and send to the application the created buffers with per PID buffers.
3372 *
fad1ed2f 3373 * Called with UST app session lock held.
71e0a100 3374 * The session list lock and the session's lock must be acquired.
fad1ed2f 3375 *
7972aab2 3376 * Return 0 on success else a negative value.
78f0bacd 3377 */
7972aab2
DG
3378static int create_channel_per_pid(struct ust_app *app,
3379 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3380 struct ust_app_channel *ua_chan)
78f0bacd 3381{
8535a6d9 3382 int ret;
7972aab2 3383 struct ust_registry_session *registry;
e9404c27 3384 enum lttng_error_code cmd_ret;
e32d7f27 3385 struct ltt_session *session = NULL;
e9404c27 3386 uint64_t chan_reg_key;
3273699d 3387 struct ust_registry_channel *ust_reg_chan;
78f0bacd 3388
7972aab2
DG
3389 assert(app);
3390 assert(usess);
3391 assert(ua_sess);
3392 assert(ua_chan);
3393
3394 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3395
3396 rcu_read_lock();
3397
3398 registry = get_session_registry(ua_sess);
fad1ed2f 3399 /* The UST app session lock is held, registry shall not be null. */
7972aab2
DG
3400 assert(registry);
3401
3402 /* Create and add a new channel registry to session. */
3403 ret = ust_registry_channel_add(registry, ua_chan->key);
78f0bacd 3404 if (ret < 0) {
f14256d6
MD
3405 ERR("Error creating the UST channel \"%s\" registry instance",
3406 ua_chan->name);
78f0bacd
DG
3407 goto error;
3408 }
3409
e098433c
JG
3410 session = session_find_by_id(ua_sess->tracing_id);
3411 assert(session);
3412
3413 assert(pthread_mutex_trylock(&session->lock));
3414 assert(session_trylock_list());
3415
7972aab2
DG
3416 /* Create and get channel on the consumer side. */
3417 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
e098433c 3418 app->bits_per_long, registry,
d2956687 3419 session->most_recent_chunk_id.value);
7972aab2 3420 if (ret < 0) {
f14256d6
MD
3421 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3422 ua_chan->name);
5b951542 3423 goto error_remove_from_registry;
7972aab2
DG
3424 }
3425
3426 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3427 if (ret < 0) {
a7169585
MD
3428 if (ret != -ENOTCONN) {
3429 ERR("Error sending channel to application");
3430 }
5b951542 3431 goto error_remove_from_registry;
7972aab2 3432 }
8535a6d9 3433
e9404c27
JG
3434 chan_reg_key = ua_chan->key;
3435 pthread_mutex_lock(&registry->lock);
3273699d
FD
3436 ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key);
3437 assert(ust_reg_chan);
3438 ust_reg_chan->consumer_key = ua_chan->key;
e9404c27
JG
3439 pthread_mutex_unlock(&registry->lock);
3440
3441 cmd_ret = notification_thread_command_add_channel(
3442 notification_thread_handle, session->name,
ff588497
JR
3443 lttng_credentials_get_uid(&ua_sess->effective_credentials),
3444 lttng_credentials_get_gid(&ua_sess->effective_credentials),
3445 ua_chan->name,
470cc211 3446 ua_chan->key, LTTNG_DOMAIN_UST,
e9404c27
JG
3447 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3448 if (cmd_ret != LTTNG_OK) {
3449 ret = - (int) cmd_ret;
3450 ERR("Failed to add channel to notification thread");
5b951542 3451 goto error_remove_from_registry;
e9404c27
JG
3452 }
3453
5b951542
MD
3454error_remove_from_registry:
3455 if (ret) {
3456 ust_registry_channel_del_free(registry, ua_chan->key, false);
3457 }
78f0bacd 3458error:
7972aab2 3459 rcu_read_unlock();
e32d7f27
JG
3460 if (session) {
3461 session_put(session);
3462 }
78f0bacd
DG
3463 return ret;
3464}
3465
3466/*
7972aab2 3467 * From an already allocated ust app channel, create the channel buffers if
88e3c2f5 3468 * needed and send them to the application. This MUST be called with a RCU read
7972aab2
DG
3469 * side lock acquired.
3470 *
fad1ed2f
JR
3471 * Called with UST app session lock held.
3472 *
a7169585
MD
3473 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3474 * the application exited concurrently.
78f0bacd 3475 */
88e3c2f5 3476static int ust_app_channel_send(struct ust_app *app,
7972aab2
DG
3477 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3478 struct ust_app_channel *ua_chan)
78f0bacd 3479{
7972aab2 3480 int ret;
78f0bacd 3481
7972aab2
DG
3482 assert(app);
3483 assert(usess);
88e3c2f5 3484 assert(usess->active);
7972aab2
DG
3485 assert(ua_sess);
3486 assert(ua_chan);
3487
3488 /* Handle buffer type before sending the channel to the application. */
3489 switch (usess->buffer_type) {
3490 case LTTNG_BUFFER_PER_UID:
3491 {
3492 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3493 if (ret < 0) {
3494 goto error;
3495 }
3496 break;
3497 }
3498 case LTTNG_BUFFER_PER_PID:
3499 {
3500 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3501 if (ret < 0) {
3502 goto error;
3503 }
3504 break;
3505 }
3506 default:
3507 assert(0);
3508 ret = -EINVAL;
78f0bacd
DG
3509 goto error;
3510 }
3511
7972aab2
DG
3512 /* Initialize ust objd object using the received handle and add it. */
3513 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3514 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
78f0bacd 3515
7972aab2
DG
3516 /* If channel is not enabled, disable it on the tracer */
3517 if (!ua_chan->enabled) {
3518 ret = disable_ust_channel(app, ua_sess, ua_chan);
3519 if (ret < 0) {
3520 goto error;
3521 }
78f0bacd
DG
3522 }
3523
3524error:
3525 return ret;
3526}
3527
284d8f55 3528/*
88e3c2f5 3529 * Create UST app channel and return it through ua_chanp if not NULL.
d0b96690 3530 *
36b588ed 3531 * Called with UST app session lock and RCU read-side lock held.
7972aab2 3532 *
88e3c2f5 3533 * Return 0 on success or else a negative value.
284d8f55 3534 */
88e3c2f5
JG
3535static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3536 struct ltt_ust_channel *uchan,
fc4b93fa 3537 enum lttng_ust_abi_chan_type type, struct ltt_ust_session *usess,
4d710ac2 3538 struct ust_app_channel **ua_chanp)
5b4a0ec0
DG
3539{
3540 int ret = 0;
bec39940
DG
3541 struct lttng_ht_iter iter;
3542 struct lttng_ht_node_str *ua_chan_node;
5b4a0ec0
DG
3543 struct ust_app_channel *ua_chan;
3544
3545 /* Lookup channel in the ust app session */
bec39940
DG
3546 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3547 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
fc34caaa 3548 if (ua_chan_node != NULL) {
5b4a0ec0 3549 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
fc34caaa 3550 goto end;
5b4a0ec0
DG
3551 }
3552
d0b96690 3553 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
fc34caaa
DG
3554 if (ua_chan == NULL) {
3555 /* Only malloc can fail here */
4d710ac2 3556 ret = -ENOMEM;
88e3c2f5 3557 goto error;
fc34caaa
DG
3558 }
3559 shadow_copy_channel(ua_chan, uchan);
3560
ffe60014
DG
3561 /* Set channel type. */
3562 ua_chan->attr.type = type;
3563
d0b96690
DG
3564 /* Only add the channel if successful on the tracer side. */
3565 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
fc34caaa 3566end:
4d710ac2
DG
3567 if (ua_chanp) {
3568 *ua_chanp = ua_chan;
3569 }
3570
3571 /* Everything went well. */
3572 return 0;
5b4a0ec0
DG
3573
3574error:
4d710ac2 3575 return ret;
5b4a0ec0
DG
3576}
3577
3578/*
3579 * Create UST app event and create it on the tracer side.
d0b96690 3580 *
993578ff 3581 * Must be called with the RCU read side lock held.
d0b96690 3582 * Called with ust app session mutex held.
5b4a0ec0 3583 */
edb67388
DG
3584static
3585int create_ust_app_event(struct ust_app_session *ua_sess,
3586 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
3587 struct ust_app *app)
284d8f55 3588{
edb67388 3589 int ret = 0;
5b4a0ec0 3590 struct ust_app_event *ua_event;
284d8f55 3591
edb67388
DG
3592 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3593 if (ua_event == NULL) {
20533947 3594 /* Only failure mode of alloc_ust_app_event(). */
edb67388 3595 ret = -ENOMEM;
fc34caaa 3596 goto end;
5b4a0ec0 3597 }
edb67388 3598 shadow_copy_event(ua_event, uevent);
5b4a0ec0 3599
edb67388 3600 /* Create it on the tracer side */
5b4a0ec0 3601 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
284d8f55 3602 if (ret < 0) {
e9f11505
JG
3603 /*
3604 * Not found previously means that it does not exist on the
3605 * tracer. If the application reports that the event existed,
3606 * it means there is a bug in the sessiond or lttng-ust
3607 * (or corruption, etc.)
3608 */
3609 if (ret == -LTTNG_UST_ERR_EXIST) {
3610 ERR("Tracer for application reported that an event being created already existed: "
3611 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3612 uevent->attr.name,
3613 app->pid, app->ppid, app->uid,
3614 app->gid);
3615 }
284d8f55
DG
3616 goto error;
3617 }
3618
d0b96690 3619 add_unique_ust_app_event(ua_chan, ua_event);
284d8f55 3620
993578ff
JR
3621 DBG2("UST app create event completed: app = '%s' (ppid: %d)",
3622 app->name, app->ppid);
7f79d3a1 3623
edb67388 3624end:
fc34caaa
DG
3625 return ret;
3626
5b4a0ec0 3627error:
fc34caaa 3628 /* Valid. Calling here is already in a read side lock */
fb45065e 3629 delete_ust_app_event(-1, ua_event, app);
edb67388 3630 return ret;
5b4a0ec0
DG
3631}
3632
993578ff
JR
3633/*
3634 * Create UST app event notifier rule and create it on the tracer side.
3635 *
3636 * Must be called with the RCU read side lock held.
3637 * Called with ust app session mutex held.
3638 */
3639static
267d66aa
JR
3640int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger,
3641 struct ust_app *app)
993578ff
JR
3642{
3643 int ret = 0;
3644 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
3645
267d66aa 3646 ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger);
993578ff
JR
3647 if (ua_event_notifier_rule == NULL) {
3648 ret = -ENOMEM;
3649 goto end;
3650 }
3651
3652 /* Create it on the tracer side. */
3653 ret = create_ust_event_notifier(app, ua_event_notifier_rule);
3654 if (ret < 0) {
3655 /*
3656 * Not found previously means that it does not exist on the
3657 * tracer. If the application reports that the event existed,
3658 * it means there is a bug in the sessiond or lttng-ust
3659 * (or corruption, etc.)
3660 */
3661 if (ret == -LTTNG_UST_ERR_EXIST) {
3662 ERR("Tracer for application reported that an event notifier being created already exists: "
3663 "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d",
267d66aa 3664 lttng_trigger_get_tracer_token(trigger),
993578ff
JR
3665 app->pid, app->ppid, app->uid,
3666 app->gid);
3667 }
3668 goto error;
3669 }
3670
3671 lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht,
3672 &ua_event_notifier_rule->node);
3673
3674 DBG2("UST app create token event rule completed: app = '%s' (ppid: %d), token = %" PRIu64,
267d66aa 3675 app->name, app->ppid, lttng_trigger_get_tracer_token(trigger));
993578ff
JR
3676
3677end:
3678 return ret;
3679
3680error:
3681 /* The RCU read side lock is already being held by the caller. */
3682 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app);
3683 return ret;
3684}
3685
5b4a0ec0
DG
3686/*
3687 * Create UST metadata and open it on the tracer side.
d0b96690 3688 *
7972aab2 3689 * Called with UST app session lock held and RCU read side lock.
5b4a0ec0
DG
3690 */
3691static int create_ust_app_metadata(struct ust_app_session *ua_sess,
ad7a9107 3692 struct ust_app *app, struct consumer_output *consumer)
5b4a0ec0
DG
3693{
3694 int ret = 0;
ffe60014 3695 struct ust_app_channel *metadata;
d88aee68 3696 struct consumer_socket *socket;
7972aab2 3697 struct ust_registry_session *registry;
e32d7f27 3698 struct ltt_session *session = NULL;
5b4a0ec0 3699
ffe60014
DG
3700 assert(ua_sess);
3701 assert(app);
d88aee68 3702 assert(consumer);
5b4a0ec0 3703
7972aab2 3704 registry = get_session_registry(ua_sess);
fad1ed2f 3705 /* The UST app session is held registry shall not be null. */
7972aab2
DG
3706 assert(registry);
3707
ce34fcd0
MD
3708 pthread_mutex_lock(&registry->lock);
3709
1b532a60
DG
3710 /* Metadata already exists for this registry or it was closed previously */
3711 if (registry->metadata_key || registry->metadata_closed) {
7972aab2
DG
3712 ret = 0;
3713 goto error;
5b4a0ec0
DG
3714 }
3715
ffe60014 3716 /* Allocate UST metadata */
d0b96690 3717 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
ffe60014
DG
3718 if (!metadata) {
3719 /* malloc() failed */
3720 ret = -ENOMEM;
3721 goto error;
3722 }
5b4a0ec0 3723
ad7a9107 3724 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
5b4a0ec0 3725
7972aab2
DG
3726 /* Need one fd for the channel. */
3727 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3728 if (ret < 0) {
3729 ERR("Exhausted number of available FD upon create metadata");
3730 goto error;
3731 }
3732
4dc3dfc5
DG
3733 /* Get the right consumer socket for the application. */
3734 socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer);
3735 if (!socket) {
3736 ret = -EINVAL;
3737 goto error_consumer;
3738 }
3739
331744e3
JD
3740 /*
3741 * Keep metadata key so we can identify it on the consumer side. Assign it
3742 * to the registry *before* we ask the consumer so we avoid the race of the
3743 * consumer requesting the metadata and the ask_channel call on our side
3744 * did not returned yet.
3745 */
3746 registry->metadata_key = metadata->key;
3747
e098433c
JG
3748 session = session_find_by_id(ua_sess->tracing_id);
3749 assert(session);
3750
3751 assert(pthread_mutex_trylock(&session->lock));
3752 assert(session_trylock_list());
3753
d88aee68
DG
3754 /*
3755 * Ask the metadata channel creation to the consumer. The metadata object
3756 * will be created by the consumer and kept their. However, the stream is
3757 * never added or monitored until we do a first push metadata to the
3758 * consumer.
3759 */
7972aab2 3760 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
d2956687 3761 registry, session->current_trace_chunk);
d88aee68 3762 if (ret < 0) {
f2a444f1
DG
3763 /* Nullify the metadata key so we don't try to close it later on. */
3764 registry->metadata_key = 0;
d88aee68
DG
3765 goto error_consumer;
3766 }
3767
3768 /*
3769 * The setup command will make the metadata stream be sent to the relayd,
3770 * if applicable, and the thread managing the metadatas. This is important
3771 * because after this point, if an error occurs, the only way the stream
3772 * can be deleted is to be monitored in the consumer.
3773 */
7972aab2 3774 ret = consumer_setup_metadata(socket, metadata->key);
ffe60014 3775 if (ret < 0) {
f2a444f1
DG
3776 /* Nullify the metadata key so we don't try to close it later on. */
3777 registry->metadata_key = 0;
d88aee68 3778 goto error_consumer;
5b4a0ec0
DG
3779 }
3780
7972aab2
DG
3781 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3782 metadata->key, app->pid);
5b4a0ec0 3783
d88aee68 3784error_consumer:
b80f0b6c 3785 lttng_fd_put(LTTNG_FD_APPS, 1);
d88aee68 3786 delete_ust_app_channel(-1, metadata, app);
5b4a0ec0 3787error:
ce34fcd0 3788 pthread_mutex_unlock(&registry->lock);
e32d7f27
JG
3789 if (session) {
3790 session_put(session);
3791 }
ffe60014 3792 return ret;
5b4a0ec0
DG
3793}
3794
5b4a0ec0 3795/*
d88aee68
DG
3796 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3797 * acquired before calling this function.
5b4a0ec0
DG
3798 */
3799struct ust_app *ust_app_find_by_pid(pid_t pid)
3800{
d88aee68 3801 struct ust_app *app = NULL;
bec39940
DG
3802 struct lttng_ht_node_ulong *node;
3803 struct lttng_ht_iter iter;
5b4a0ec0 3804
bec39940
DG
3805 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3806 node = lttng_ht_iter_get_node_ulong(&iter);
5b4a0ec0
DG
3807 if (node == NULL) {
3808 DBG2("UST app no found with pid %d", pid);
3809 goto error;
3810 }
5b4a0ec0
DG
3811
3812 DBG2("Found UST app by pid %d", pid);
3813
d88aee68 3814 app = caa_container_of(node, struct ust_app, pid_n);
5b4a0ec0
DG
3815
3816error:
d88aee68 3817 return app;
5b4a0ec0
DG
3818}
3819
d88aee68
DG
3820/*
3821 * Allocate and init an UST app object using the registration information and
3822 * the command socket. This is called when the command socket connects to the
3823 * session daemon.
3824 *
3825 * The object is returned on success or else NULL.
3826 */
d0b96690 3827struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
5b4a0ec0 3828{
5e2abfaf 3829 int ret;
d0b96690 3830 struct ust_app *lta = NULL;
da873412 3831 struct lttng_pipe *event_notifier_event_source_pipe = NULL;
d0b96690
DG
3832
3833 assert(msg);
3834 assert(sock >= 0);
3835
3836 DBG3("UST app creating application for socket %d", sock);
5b4a0ec0 3837
173af62f
DG
3838 if ((msg->bits_per_long == 64 &&
3839 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
3840 || (msg->bits_per_long == 32 &&
3841 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
f943b0fb 3842 ERR("Registration failed: application \"%s\" (pid: %d) has "
d0b96690
DG
3843 "%d-bit long, but no consumerd for this size is available.\n",
3844 msg->name, msg->pid, msg->bits_per_long);
3845 goto error;
3f2c5fcc 3846 }
d0b96690 3847
5e2abfaf
JG
3848 /*
3849 * Reserve the two file descriptors of the event source pipe. The write
3850 * end will be closed once it is passed to the application, at which
3851 * point a single 'put' will be performed.
3852 */
3853 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
3854 if (ret) {
3855 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s' (ppid: %d)",
3856 msg->name, (int) msg->ppid);
3857 goto error;
3858 }
3859
da873412
JR
3860 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
3861 if (!event_notifier_event_source_pipe) {
3862 PERROR("Failed to open application event source pipe: '%s' (ppid = %d)",
3863 msg->name, msg->ppid);
3864 goto error;
3865 }
3866
5b4a0ec0
DG
3867 lta = zmalloc(sizeof(struct ust_app));
3868 if (lta == NULL) {
3869 PERROR("malloc");
da873412 3870 goto error_free_pipe;
5b4a0ec0
DG
3871 }
3872
da873412
JR
3873 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
3874
5b4a0ec0
DG
3875 lta->ppid = msg->ppid;
3876 lta->uid = msg->uid;
3877 lta->gid = msg->gid;
d0b96690 3878
7753dea8 3879 lta->bits_per_long = msg->bits_per_long;
d0b96690
DG
3880 lta->uint8_t_alignment = msg->uint8_t_alignment;
3881 lta->uint16_t_alignment = msg->uint16_t_alignment;
3882 lta->uint32_t_alignment = msg->uint32_t_alignment;
3883 lta->uint64_t_alignment = msg->uint64_t_alignment;
3884 lta->long_alignment = msg->long_alignment;
3885 lta->byte_order = msg->byte_order;
3886
5b4a0ec0
DG
3887 lta->v_major = msg->major;
3888 lta->v_minor = msg->minor;
d9bf3ca4 3889 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d0b96690 3890 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
10b56aef 3891 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
d0b96690 3892 lta->notify_sock = -1;
993578ff 3893 lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
d88aee68
DG
3894
3895 /* Copy name and make sure it's NULL terminated. */
3896 strncpy(lta->name, msg->name, sizeof(lta->name));
3897 lta->name[UST_APP_PROCNAME_LEN] = '\0';
3898
3899 /*
3900 * Before this can be called, when receiving the registration information,
3901 * the application compatibility is checked. So, at this point, the
3902 * application can work with this session daemon.
3903 */
d0b96690 3904 lta->compatible = 1;
5b4a0ec0 3905
852d0037 3906 lta->pid = msg->pid;
d0b96690 3907 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
852d0037 3908 lta->sock = sock;
fb45065e 3909 pthread_mutex_init(&lta->sock_lock, NULL);
d0b96690 3910 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
5b4a0ec0 3911
d42f20df 3912 CDS_INIT_LIST_HEAD(&lta->teardown_head);
d0b96690 3913 return lta;
da873412
JR
3914
3915error_free_pipe:
3916 lttng_pipe_destroy(event_notifier_event_source_pipe);
5e2abfaf 3917 lttng_fd_put(LTTNG_FD_APPS, 2);
da873412
JR
3918error:
3919 return NULL;
d0b96690
DG
3920}
3921
d88aee68
DG
3922/*
3923 * For a given application object, add it to every hash table.
3924 */
d0b96690
DG
3925void ust_app_add(struct ust_app *app)
3926{
3927 assert(app);
3928 assert(app->notify_sock >= 0);
3929
940c4592
JR
3930 app->registration_time = time(NULL);
3931
5b4a0ec0 3932 rcu_read_lock();
852d0037
DG
3933
3934 /*
3935 * On a re-registration, we want to kick out the previous registration of
3936 * that pid
3937 */
d0b96690 3938 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
852d0037
DG
3939
3940 /*
3941 * The socket _should_ be unique until _we_ call close. So, a add_unique
3942 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
3943 * already in the table.
3944 */
d0b96690 3945 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
852d0037 3946
d0b96690
DG
3947 /* Add application to the notify socket hash table. */
3948 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
3949 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
5b4a0ec0 3950
d0b96690 3951 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
d88aee68
DG
3952 "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid,
3953 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
3954 app->v_minor);
5b4a0ec0 3955
d0b96690
DG
3956 rcu_read_unlock();
3957}
3958
d88aee68
DG
3959/*
3960 * Set the application version into the object.
3961 *
3962 * Return 0 on success else a negative value either an errno code or a
3963 * LTTng-UST error code.
3964 */
d0b96690
DG
3965int ust_app_version(struct ust_app *app)
3966{
d88aee68
DG
3967 int ret;
3968
d0b96690 3969 assert(app);
d88aee68 3970
fb45065e 3971 pthread_mutex_lock(&app->sock_lock);
d88aee68 3972 ret = ustctl_tracer_version(app->sock, &app->version);
fb45065e 3973 pthread_mutex_unlock(&app->sock_lock);
d88aee68
DG
3974 if (ret < 0) {
3975 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
5368d366 3976 ERR("UST app %d version failed with ret %d", app->sock, ret);
d88aee68 3977 } else {
5368d366 3978 DBG3("UST app %d version failed. Application is dead", app->sock);
d88aee68
DG
3979 }
3980 }
3981
3982 return ret;
5b4a0ec0
DG
3983}
3984
da873412
JR
3985/*
3986 * Setup the base event notifier group.
3987 *
3988 * Return 0 on success else a negative value either an errno code or a
3989 * LTTng-UST error code.
3990 */
3991int ust_app_setup_event_notifier_group(struct ust_app *app)
3992{
3993 int ret;
3994 int event_pipe_write_fd;
fc4b93fa 3995 struct lttng_ust_abi_object_data *event_notifier_group = NULL;
da873412
JR
3996 enum lttng_error_code lttng_ret;
3997
3998 assert(app);
3999
4000 /* Get the write side of the pipe. */
4001 event_pipe_write_fd = lttng_pipe_get_writefd(
4002 app->event_notifier_group.event_pipe);
4003
4004 pthread_mutex_lock(&app->sock_lock);
4005 ret = ustctl_create_event_notifier_group(app->sock,
4006 event_pipe_write_fd, &event_notifier_group);
4007 pthread_mutex_unlock(&app->sock_lock);
4008 if (ret < 0) {
4009 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4010 ERR("Failed to create application event notifier group: ret = %d, app socket fd = %d, event_pipe_write_fd = %d",
4011 ret, app->sock, event_pipe_write_fd);
4012 } else {
4013 DBG("Failed to create application event notifier group (application is dead): app socket fd = %d",
4014 app->sock);
4015 }
4016
4017 goto error;
4018 }
4019
5d4193fd
JG
4020 ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe);
4021 if (ret) {
4022 ERR("Failed to close write end of the application's event source pipe: app = '%s' (ppid = %d)",
4023 app->name, app->ppid);
4024 goto error;
4025 }
4026
5e2abfaf
JG
4027 /*
4028 * Release the file descriptor that was reserved for the write-end of
4029 * the pipe.
4030 */
4031 lttng_fd_put(LTTNG_FD_APPS, 1);
4032
da873412
JR
4033 lttng_ret = notification_thread_command_add_tracer_event_source(
4034 notification_thread_handle,
4035 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe),
4036 LTTNG_DOMAIN_UST);
4037 if (lttng_ret != LTTNG_OK) {
4038 ERR("Failed to add tracer event source to notification thread");
4039 ret = - 1;
4040 goto error;
4041 }
4042
4043 /* Assign handle only when the complete setup is valid. */
4044 app->event_notifier_group.object = event_notifier_group;
4045 return ret;
4046
4047error:
4048 ustctl_release_object(app->sock, app->event_notifier_group.object);
4049 free(app->event_notifier_group.object);
4050 return ret;
4051}
4052
5b4a0ec0
DG
4053/*
4054 * Unregister app by removing it from the global traceable app list and freeing
4055 * the data struct.
4056 *
4057 * The socket is already closed at this point so no close to sock.
4058 */
4059void ust_app_unregister(int sock)
4060{
4061 struct ust_app *lta;
bec39940 4062 struct lttng_ht_node_ulong *node;
c4b88406 4063 struct lttng_ht_iter ust_app_sock_iter;
bec39940 4064 struct lttng_ht_iter iter;
d42f20df 4065 struct ust_app_session *ua_sess;
525b0740 4066 int ret;
5b4a0ec0
DG
4067
4068 rcu_read_lock();
886459c6 4069
5b4a0ec0 4070 /* Get the node reference for a call_rcu */
c4b88406
MD
4071 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
4072 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
d0b96690 4073 assert(node);
284d8f55 4074
852d0037 4075 lta = caa_container_of(node, struct ust_app, sock_n);
852d0037
DG
4076 DBG("PID %d unregistering with sock %d", lta->pid, sock);
4077
d88aee68 4078 /*
ce34fcd0
MD
4079 * For per-PID buffers, perform "push metadata" and flush all
4080 * application streams before removing app from hash tables,
4081 * ensuring proper behavior of data_pending check.
c4b88406 4082 * Remove sessions so they are not visible during deletion.
d88aee68 4083 */
d42f20df
DG
4084 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
4085 node.node) {
7972aab2
DG
4086 struct ust_registry_session *registry;
4087
d42f20df
DG
4088 ret = lttng_ht_del(lta->sessions, &iter);
4089 if (ret) {
4090 /* The session was already removed so scheduled for teardown. */
4091 continue;
4092 }
4093
ce34fcd0
MD
4094 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
4095 (void) ust_app_flush_app_session(lta, ua_sess);
4096 }
c4b88406 4097
d42f20df
DG
4098 /*
4099 * Add session to list for teardown. This is safe since at this point we
4100 * are the only one using this list.
4101 */
d88aee68
DG
4102 pthread_mutex_lock(&ua_sess->lock);
4103
b161602a
MD
4104 if (ua_sess->deleted) {
4105 pthread_mutex_unlock(&ua_sess->lock);
4106 continue;
4107 }
4108
d88aee68
DG
4109 /*
4110 * Normally, this is done in the delete session process which is
4111 * executed in the call rcu below. However, upon registration we can't
4112 * afford to wait for the grace period before pushing data or else the
4113 * data pending feature can race between the unregistration and stop
4114 * command where the data pending command is sent *before* the grace
4115 * period ended.
4116 *
4117 * The close metadata below nullifies the metadata pointer in the
4118 * session so the delete session will NOT push/close a second time.
4119 */
7972aab2 4120 registry = get_session_registry(ua_sess);
ce34fcd0 4121 if (registry) {
7972aab2
DG
4122 /* Push metadata for application before freeing the application. */
4123 (void) push_metadata(registry, ua_sess->consumer);
4124
4125 /*
4126 * Don't ask to close metadata for global per UID buffers. Close
1b532a60
DG
4127 * metadata only on destroy trace session in this case. Also, the
4128 * previous push metadata could have flag the metadata registry to
4129 * close so don't send a close command if closed.
7972aab2 4130 */
ce34fcd0 4131 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
7972aab2
DG
4132 /* And ask to close it for this session registry. */
4133 (void) close_metadata(registry, ua_sess->consumer);
4134 }
4135 }
d42f20df 4136 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
c4b88406 4137
d88aee68 4138 pthread_mutex_unlock(&ua_sess->lock);
d42f20df
DG
4139 }
4140
c4b88406
MD
4141 /* Remove application from PID hash table */
4142 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
4143 assert(!ret);
4144
4145 /*
4146 * Remove application from notify hash table. The thread handling the
4147 * notify socket could have deleted the node so ignore on error because
c48239ca
JG
4148 * either way it's valid. The close of that socket is handled by the
4149 * apps_notify_thread.
c4b88406
MD
4150 */
4151 iter.iter.node = &lta->notify_sock_n.node;
4152 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
4153
4154 /*
4155 * Ignore return value since the node might have been removed before by an
4156 * add replace during app registration because the PID can be reassigned by
4157 * the OS.
4158 */
4159 iter.iter.node = &lta->pid_n.node;
4160 ret = lttng_ht_del(ust_app_ht, &iter);
4161 if (ret) {
4162 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4163 lta->pid);
4164 }
4165
852d0037
DG
4166 /* Free memory */
4167 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
4168
5b4a0ec0
DG
4169 rcu_read_unlock();
4170 return;
284d8f55
DG
4171}
4172
5b4a0ec0
DG
4173/*
4174 * Fill events array with all events name of all registered apps.
4175 */
4176int ust_app_list_events(struct lttng_event **events)
421cb601 4177{
5b4a0ec0
DG
4178 int ret, handle;
4179 size_t nbmem, count = 0;
bec39940 4180 struct lttng_ht_iter iter;
5b4a0ec0 4181 struct ust_app *app;
c617c0c6 4182 struct lttng_event *tmp_event;
421cb601 4183
5b4a0ec0 4184 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
4185 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
4186 if (tmp_event == NULL) {
5b4a0ec0
DG
4187 PERROR("zmalloc ust app events");
4188 ret = -ENOMEM;
421cb601
DG
4189 goto error;
4190 }
4191
5b4a0ec0 4192 rcu_read_lock();
421cb601 4193
852d0037 4194 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
fc4b93fa 4195 struct lttng_ust_abi_tracepoint_iter uiter;
ac3bd9c0 4196
840cb59c 4197 health_code_update();
86acf0da 4198
e0c7ec2b
DG
4199 if (!app->compatible) {
4200 /*
4201 * TODO: In time, we should notice the caller of this error by
4202 * telling him that this is a version error.
4203 */
4204 continue;
4205 }
fb45065e 4206 pthread_mutex_lock(&app->sock_lock);
852d0037 4207 handle = ustctl_tracepoint_list(app->sock);
5b4a0ec0 4208 if (handle < 0) {
ffe60014
DG
4209 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4210 ERR("UST app list events getting handle failed for app pid %d",
4211 app->pid);
4212 }
fb45065e 4213 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
4214 continue;
4215 }
421cb601 4216
852d0037 4217 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
fb54cdbf 4218 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
4219 /* Handle ustctl error. */
4220 if (ret < 0) {
fb45065e
MD
4221 int release_ret;
4222
a2ba1ab0 4223 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
4224 ERR("UST app tp list get failed for app %d with ret %d",
4225 app->sock, ret);
4226 } else {
4227 DBG3("UST app tp list get failed. Application is dead");
3757b385
DG
4228 /*
4229 * This is normal behavior, an application can die during the
4230 * creation process. Don't report an error so the execution can
4231 * continue normally. Continue normal execution.
4232 */
4233 break;
ffe60014 4234 }
98f595d4 4235 free(tmp_event);
fb45065e 4236 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
4237 if (release_ret < 0 &&
4238 release_ret != -LTTNG_UST_ERR_EXITING &&
4239 release_ret != -EPIPE) {
fb45065e
MD
4240 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4241 }
4242 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4243 goto rcu_error;
4244 }
4245
840cb59c 4246 health_code_update();
815564d8 4247 if (count >= nbmem) {
d7b3776f 4248 /* In case the realloc fails, we free the memory */
53efb85a
MD
4249 struct lttng_event *new_tmp_event;
4250 size_t new_nbmem;
4251
4252 new_nbmem = nbmem << 1;
4253 DBG2("Reallocating event list from %zu to %zu entries",
4254 nbmem, new_nbmem);
4255 new_tmp_event = realloc(tmp_event,
4256 new_nbmem * sizeof(struct lttng_event));
4257 if (new_tmp_event == NULL) {
fb45065e
MD
4258 int release_ret;
4259
5b4a0ec0 4260 PERROR("realloc ust app events");
c617c0c6 4261 free(tmp_event);
5b4a0ec0 4262 ret = -ENOMEM;
fb45065e 4263 release_ret = ustctl_release_handle(app->sock, handle);
68313703
JG
4264 if (release_ret < 0 &&
4265 release_ret != -LTTNG_UST_ERR_EXITING &&
4266 release_ret != -EPIPE) {
fb45065e
MD
4267 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4268 }
4269 pthread_mutex_unlock(&app->sock_lock);
5b4a0ec0
DG
4270 goto rcu_error;
4271 }
53efb85a
MD
4272 /* Zero the new memory */
4273 memset(new_tmp_event + nbmem, 0,
4274 (new_nbmem - nbmem) * sizeof(struct lttng_event));
4275 nbmem = new_nbmem;
4276 tmp_event = new_tmp_event;
5b4a0ec0 4277 }
fc4b93fa 4278 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN);
c617c0c6 4279 tmp_event[count].loglevel = uiter.loglevel;
fc4b93fa 4280 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT;
c617c0c6
MD
4281 tmp_event[count].pid = app->pid;
4282 tmp_event[count].enabled = -1;
5b4a0ec0 4283 count++;
421cb601 4284 }
fb45065e
MD
4285 ret = ustctl_release_handle(app->sock, handle);
4286 pthread_mutex_unlock(&app->sock_lock);
68313703 4287 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
fb45065e
MD
4288 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
4289 }
421cb601
DG
4290 }
4291
5b4a0ec0 4292 ret = count;
c617c0c6 4293 *events = tmp_event;
421cb601 4294
5b4a0ec0 4295 DBG2("UST app list events done (%zu events)", count);
421cb601 4296
5b4a0ec0
DG
4297rcu_error:
4298 rcu_read_unlock();
421cb601 4299error:
840cb59c 4300 health_code_update();
5b4a0ec0 4301 return ret;
421cb601
DG
4302}
4303
f37d259d
MD
4304/*
4305 * Fill events array with all events name of all registered apps.
4306 */
4307int ust_app_list_event_fields(struct lttng_event_field **fields)
4308{
4309 int ret, handle;
4310 size_t nbmem, count = 0;
4311 struct lttng_ht_iter iter;
4312 struct ust_app *app;
c617c0c6 4313 struct lttng_event_field *tmp_event;
f37d259d
MD
4314
4315 nbmem = UST_APP_EVENT_LIST_SIZE;
c617c0c6
MD
4316 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
4317 if (tmp_event == NULL) {
f37d259d
MD
4318 PERROR("zmalloc ust app event fields");
4319 ret = -ENOMEM;
4320 goto error;
4321 }
4322
4323 rcu_read_lock();
4324
4325 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
fc4b93fa 4326 struct lttng_ust_abi_field_iter uiter;
f37d259d 4327
840cb59c 4328 health_code_update();
86acf0da 4329
f37d259d
MD
4330 if (!app->compatible) {
4331 /*
4332 * TODO: In time, we should notice the caller of this error by
4333 * telling him that this is a version error.
4334 */
4335 continue;
4336 }
fb45065e 4337 pthread_mutex_lock(&app->sock_lock);
f37d259d
MD
4338 handle = ustctl_tracepoint_field_list(app->sock);
4339 if (handle < 0) {
ffe60014
DG
4340 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4341 ERR("UST app list field getting handle failed for app pid %d",
4342 app->pid);
4343 }
fb45065e 4344 pthread_mutex_unlock(&app->sock_lock);
f37d259d
MD
4345 continue;
4346 }
4347
4348 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
fb54cdbf 4349 &uiter)) != -LTTNG_UST_ERR_NOENT) {
ffe60014
DG
4350 /* Handle ustctl error. */
4351 if (ret < 0) {
fb45065e
MD
4352 int release_ret;
4353
a2ba1ab0 4354 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
ffe60014
DG
4355 ERR("UST app tp list field failed for app %d with ret %d",
4356 app->sock, ret);
4357 } else {
4358 DBG3("UST app tp list field failed. Application is dead");
3757b385
DG
4359 /*
4360 * This is normal behavior, an application can die during the
4361 * creation process. Don't report an error so the execution can
98f595d4 4362 * continue normally. Reset list and count for next app.
3757b385
DG
4363 */
4364 break;
ffe60014 4365 }
98f595d4 4366 free(tmp_event);
fb45065e
MD
4367 release_ret = ustctl_release_handle(app->sock, handle);
4368 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
4369 if (release_ret < 0 &&
4370 release_ret != -LTTNG_UST_ERR_EXITING &&
4371 release_ret != -EPIPE) {
fb45065e
MD
4372 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4373 }
ffe60014
DG
4374 goto rcu_error;
4375 }
4376
840cb59c 4377 health_code_update();
f37d259d 4378 if (count >= nbmem) {
d7b3776f 4379 /* In case the realloc fails, we free the memory */
53efb85a
MD
4380 struct lttng_event_field *new_tmp_event;
4381 size_t new_nbmem;
4382
4383 new_nbmem = nbmem << 1;
4384 DBG2("Reallocating event field list from %zu to %zu entries",
4385 nbmem, new_nbmem);
4386 new_tmp_event = realloc(tmp_event,
4387 new_nbmem * sizeof(struct lttng_event_field));
4388 if (new_tmp_event == NULL) {
fb45065e
MD
4389 int release_ret;
4390
f37d259d 4391 PERROR("realloc ust app event fields");
c617c0c6 4392 free(tmp_event);
f37d259d 4393 ret = -ENOMEM;
fb45065e
MD
4394 release_ret = ustctl_release_handle(app->sock, handle);
4395 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
4396 if (release_ret &&
4397 release_ret != -LTTNG_UST_ERR_EXITING &&
4398 release_ret != -EPIPE) {
fb45065e
MD
4399 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4400 }
f37d259d
MD
4401 goto rcu_error;
4402 }
53efb85a
MD
4403 /* Zero the new memory */
4404 memset(new_tmp_event + nbmem, 0,
4405 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
4406 nbmem = new_nbmem;
4407 tmp_event = new_tmp_event;
f37d259d 4408 }
f37d259d 4409
fc4b93fa 4410 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
2e84128e
DG
4411 /* Mapping between these enums matches 1 to 1. */
4412 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
c617c0c6 4413 tmp_event[count].nowrite = uiter.nowrite;
f37d259d 4414
fc4b93fa 4415 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
c617c0c6 4416 tmp_event[count].event.loglevel = uiter.loglevel;
2e84128e 4417 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
c617c0c6
MD
4418 tmp_event[count].event.pid = app->pid;
4419 tmp_event[count].event.enabled = -1;
f37d259d
MD
4420 count++;
4421 }
fb45065e
MD
4422 ret = ustctl_release_handle(app->sock, handle);
4423 pthread_mutex_unlock(&app->sock_lock);
68313703
JG
4424 if (ret < 0 &&
4425 ret != -LTTNG_UST_ERR_EXITING &&
4426 ret != -EPIPE) {
fb45065e
MD
4427 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
4428 }
f37d259d
MD
4429 }
4430
4431 ret = count;
c617c0c6 4432 *fields = tmp_event;
f37d259d
MD
4433
4434 DBG2("UST app list event fields done (%zu events)", count);
4435
4436rcu_error:
4437 rcu_read_unlock();
4438error:
840cb59c 4439 health_code_update();
f37d259d
MD
4440 return ret;
4441}
4442
5b4a0ec0
DG
4443/*
4444 * Free and clean all traceable apps of the global list.
36b588ed
MD
4445 *
4446 * Should _NOT_ be called with RCU read-side lock held.
5b4a0ec0
DG
4447 */
4448void ust_app_clean_list(void)
421cb601 4449{
5b4a0ec0 4450 int ret;
659ed79f 4451 struct ust_app *app;
bec39940 4452 struct lttng_ht_iter iter;
421cb601 4453
5b4a0ec0 4454 DBG2("UST app cleaning registered apps hash table");
421cb601 4455
5b4a0ec0 4456 rcu_read_lock();
421cb601 4457
faadaa3a
JG
4458 /* Cleanup notify socket hash table */
4459 if (ust_app_ht_by_notify_sock) {
4460 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
4461 notify_sock_n.node) {
b69a1b40
JG
4462 /*
4463 * Assert that all notifiers are gone as all triggers
4464 * are unregistered prior to this clean-up.
4465 */
4466 assert(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0);
faadaa3a 4467
faadaa3a
JG
4468 ust_app_notify_sock_unregister(app->notify_sock);
4469 }
4470 }
4471
f1b711c4
MD
4472 if (ust_app_ht) {
4473 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4474 ret = lttng_ht_del(ust_app_ht, &iter);
4475 assert(!ret);
4476 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
4477 }
421cb601
DG
4478 }
4479
852d0037 4480 /* Cleanup socket hash table */
f1b711c4
MD
4481 if (ust_app_ht_by_sock) {
4482 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
4483 sock_n.node) {
4484 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
4485 assert(!ret);
4486 }
bec39940 4487 }
852d0037 4488
36b588ed 4489 rcu_read_unlock();
d88aee68 4490
bec39940 4491 /* Destroy is done only when the ht is empty */
f1b711c4
MD
4492 if (ust_app_ht) {
4493 ht_cleanup_push(ust_app_ht);
4494 }
4495 if (ust_app_ht_by_sock) {
4496 ht_cleanup_push(ust_app_ht_by_sock);
4497 }
4498 if (ust_app_ht_by_notify_sock) {
4499 ht_cleanup_push(ust_app_ht_by_notify_sock);
4500 }
5b4a0ec0
DG
4501}
4502
4503/*
4504 * Init UST app hash table.
4505 */
57703f6e 4506int ust_app_ht_alloc(void)
5b4a0ec0 4507{
bec39940 4508 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4509 if (!ust_app_ht) {
4510 return -1;
4511 }
852d0037 4512 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4513 if (!ust_app_ht_by_sock) {
4514 return -1;
4515 }
d0b96690 4516 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
57703f6e
MD
4517 if (!ust_app_ht_by_notify_sock) {
4518 return -1;
4519 }
4520 return 0;
421cb601
DG
4521}
4522
78f0bacd
DG
4523/*
4524 * For a specific UST session, disable the channel for all registered apps.
4525 */
35a9059d 4526int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4527 struct ltt_ust_channel *uchan)
4528{
4529 int ret = 0;
bec39940
DG
4530 struct lttng_ht_iter iter;
4531 struct lttng_ht_node_str *ua_chan_node;
78f0bacd
DG
4532 struct ust_app *app;
4533 struct ust_app_session *ua_sess;
8535a6d9 4534 struct ust_app_channel *ua_chan;
78f0bacd 4535
88e3c2f5 4536 assert(usess->active);
d9bf3ca4 4537 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
a991f516 4538 uchan->name, usess->id);
78f0bacd
DG
4539
4540 rcu_read_lock();
4541
4542 /* For every registered applications */
852d0037 4543 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
bec39940 4544 struct lttng_ht_iter uiter;
e0c7ec2b
DG
4545 if (!app->compatible) {
4546 /*
4547 * TODO: In time, we should notice the caller of this error by
4548 * telling him that this is a version error.
4549 */
4550 continue;
4551 }
78f0bacd
DG
4552 ua_sess = lookup_session_by_app(usess, app);
4553 if (ua_sess == NULL) {
4554 continue;
4555 }
4556
8535a6d9 4557 /* Get channel */
bec39940
DG
4558 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4559 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
8535a6d9
DG
4560 /* If the session if found for the app, the channel must be there */
4561 assert(ua_chan_node);
4562
4563 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4564 /* The channel must not be already disabled */
4565 assert(ua_chan->enabled == 1);
4566
4567 /* Disable channel onto application */
4568 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
78f0bacd
DG
4569 if (ret < 0) {
4570 /* XXX: We might want to report this error at some point... */
4571 continue;
4572 }
4573 }
4574
4575 rcu_read_unlock();
78f0bacd
DG
4576 return ret;
4577}
4578
4579/*
4580 * For a specific UST session, enable the channel for all registered apps.
4581 */
35a9059d 4582int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
78f0bacd
DG
4583 struct ltt_ust_channel *uchan)
4584{
4585 int ret = 0;
bec39940 4586 struct lttng_ht_iter iter;
78f0bacd
DG
4587 struct ust_app *app;
4588 struct ust_app_session *ua_sess;
4589
88e3c2f5 4590 assert(usess->active);
d9bf3ca4 4591 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
a991f516 4592 uchan->name, usess->id);
78f0bacd
DG
4593
4594 rcu_read_lock();
4595
4596 /* For every registered applications */
852d0037 4597 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4598 if (!app->compatible) {
4599 /*
4600 * TODO: In time, we should notice the caller of this error by
4601 * telling him that this is a version error.
4602 */
4603 continue;
4604 }
78f0bacd
DG
4605 ua_sess = lookup_session_by_app(usess, app);
4606 if (ua_sess == NULL) {
4607 continue;
4608 }
4609
4610 /* Enable channel onto application */
4611 ret = enable_ust_app_channel(ua_sess, uchan, app);
4612 if (ret < 0) {
4613 /* XXX: We might want to report this error at some point... */
4614 continue;
4615 }
4616 }
4617
4618 rcu_read_unlock();
78f0bacd
DG
4619 return ret;
4620}
4621
b0a40d28
DG
4622/*
4623 * Disable an event in a channel and for a specific session.
4624 */
35a9059d
DG
4625int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4626 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
b0a40d28
DG
4627{
4628 int ret = 0;
bec39940 4629 struct lttng_ht_iter iter, uiter;
700c5a9d 4630 struct lttng_ht_node_str *ua_chan_node;
b0a40d28
DG
4631 struct ust_app *app;
4632 struct ust_app_session *ua_sess;
4633 struct ust_app_channel *ua_chan;
4634 struct ust_app_event *ua_event;
4635
88e3c2f5 4636 assert(usess->active);
b0a40d28 4637 DBG("UST app disabling event %s for all apps in channel "
d9bf3ca4
MD
4638 "%s for session id %" PRIu64,
4639 uevent->attr.name, uchan->name, usess->id);
b0a40d28
DG
4640
4641 rcu_read_lock();
4642
4643 /* For all registered applications */
852d0037 4644 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4645 if (!app->compatible) {
4646 /*
4647 * TODO: In time, we should notice the caller of this error by
4648 * telling him that this is a version error.
4649 */
4650 continue;
4651 }
b0a40d28
DG
4652 ua_sess = lookup_session_by_app(usess, app);
4653 if (ua_sess == NULL) {
4654 /* Next app */
4655 continue;
4656 }
4657
4658 /* Lookup channel in the ust app session */
bec39940
DG
4659 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4660 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
b0a40d28 4661 if (ua_chan_node == NULL) {
d9bf3ca4 4662 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
852d0037 4663 "Skipping", uchan->name, usess->id, app->pid);
b0a40d28
DG
4664 continue;
4665 }
4666 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4667
700c5a9d
JR
4668 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4669 uevent->filter, uevent->attr.loglevel,
4670 uevent->exclusion);
4671 if (ua_event == NULL) {
b0a40d28 4672 DBG2("Event %s not found in channel %s for app pid %d."
852d0037 4673 "Skipping", uevent->attr.name, uchan->name, app->pid);
b0a40d28
DG
4674 continue;
4675 }
b0a40d28 4676
7f79d3a1 4677 ret = disable_ust_app_event(ua_sess, ua_event, app);
b0a40d28
DG
4678 if (ret < 0) {
4679 /* XXX: Report error someday... */
4680 continue;
4681 }
4682 }
4683
4684 rcu_read_unlock();
88e3c2f5
JG
4685 return ret;
4686}
4687
4688/* The ua_sess lock must be held by the caller. */
4689static
4690int ust_app_channel_create(struct ltt_ust_session *usess,
4691 struct ust_app_session *ua_sess,
4692 struct ltt_ust_channel *uchan, struct ust_app *app,
4693 struct ust_app_channel **_ua_chan)
4694{
4695 int ret = 0;
4696 struct ust_app_channel *ua_chan = NULL;
4697
4698 assert(ua_sess);
4699 ASSERT_LOCKED(ua_sess->lock);
4700
4701 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4702 sizeof(uchan->name))) {
4703 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4704 &uchan->attr);
4705 ret = 0;
4706 } else {
4707 struct ltt_ust_context *uctx = NULL;
4708
4709 /*
4710 * Create channel onto application and synchronize its
4711 * configuration.
4712 */
4713 ret = ust_app_channel_allocate(ua_sess, uchan,
fc4b93fa 4714 LTTNG_UST_ABI_CHAN_PER_CPU, usess,
88e3c2f5 4715 &ua_chan);
88ebf5a7
JR
4716 if (ret < 0) {
4717 goto error;
4718 }
4719
4720 ret = ust_app_channel_send(app, usess,
4721 ua_sess, ua_chan);
4722 if (ret) {
4723 goto error;
88e3c2f5
JG
4724 }
4725
4726 /* Add contexts. */
4727 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4728 ret = create_ust_app_channel_context(ua_chan,
4729 &uctx->ctx, app);
4730 if (ret) {
88ebf5a7 4731 goto error;
88e3c2f5
JG
4732 }
4733 }
4734 }
88ebf5a7
JR
4735
4736error:
88e3c2f5
JG
4737 if (ret < 0) {
4738 switch (ret) {
4739 case -ENOTCONN:
4740 /*
4741 * The application's socket is not valid. Either a bad socket
4742 * or a timeout on it. We can't inform the caller that for a
4743 * specific app, the session failed so lets continue here.
4744 */
4745 ret = 0; /* Not an error. */
4746 break;
4747 case -ENOMEM:
4748 default:
4749 break;
4750 }
4751 }
88ebf5a7 4752
88e3c2f5
JG
4753 if (ret == 0 && _ua_chan) {
4754 /*
4755 * Only return the application's channel on success. Note
4756 * that the channel can still be part of the application's
4757 * channel hashtable on error.
4758 */
4759 *_ua_chan = ua_chan;
4760 }
b0a40d28
DG
4761 return ret;
4762}
4763
5b4a0ec0 4764/*
edb67388 4765 * Enable event for a specific session and channel on the tracer.
5b4a0ec0 4766 */
35a9059d 4767int ust_app_enable_event_glb(struct ltt_ust_session *usess,
48842b30
DG
4768 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4769{
4770 int ret = 0;
bec39940 4771 struct lttng_ht_iter iter, uiter;
18eace3b 4772 struct lttng_ht_node_str *ua_chan_node;
48842b30
DG
4773 struct ust_app *app;
4774 struct ust_app_session *ua_sess;
4775 struct ust_app_channel *ua_chan;
4776 struct ust_app_event *ua_event;
48842b30 4777
88e3c2f5 4778 assert(usess->active);
d9bf3ca4 4779 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
a991f516 4780 uevent->attr.name, usess->id);
48842b30 4781
edb67388
DG
4782 /*
4783 * NOTE: At this point, this function is called only if the session and
4784 * channel passed are already created for all apps. and enabled on the
4785 * tracer also.
4786 */
4787
48842b30 4788 rcu_read_lock();
421cb601
DG
4789
4790 /* For all registered applications */
852d0037 4791 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4792 if (!app->compatible) {
4793 /*
4794 * TODO: In time, we should notice the caller of this error by
4795 * telling him that this is a version error.
4796 */
4797 continue;
4798 }
edb67388 4799 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4800 if (!ua_sess) {
4801 /* The application has problem or is probably dead. */
4802 continue;
4803 }
ba767faf 4804
d0b96690
DG
4805 pthread_mutex_lock(&ua_sess->lock);
4806
b161602a
MD
4807 if (ua_sess->deleted) {
4808 pthread_mutex_unlock(&ua_sess->lock);
4809 continue;
4810 }
4811
edb67388 4812 /* Lookup channel in the ust app session */
bec39940
DG
4813 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4814 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
a7169585
MD
4815 /*
4816 * It is possible that the channel cannot be found is
4817 * the channel/event creation occurs concurrently with
4818 * an application exit.
4819 */
4820 if (!ua_chan_node) {
4821 pthread_mutex_unlock(&ua_sess->lock);
4822 continue;
4823 }
edb67388
DG
4824
4825 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4826
18eace3b
DG
4827 /* Get event node */
4828 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
39c5a3a7 4829 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
18eace3b 4830 if (ua_event == NULL) {
7f79d3a1 4831 DBG3("UST app enable event %s not found for app PID %d."
852d0037 4832 "Skipping app", uevent->attr.name, app->pid);
d0b96690 4833 goto next_app;
35a9059d 4834 }
35a9059d
DG
4835
4836 ret = enable_ust_app_event(ua_sess, ua_event, app);
4837 if (ret < 0) {
d0b96690 4838 pthread_mutex_unlock(&ua_sess->lock);
7f79d3a1 4839 goto error;
48842b30 4840 }
d0b96690
DG
4841 next_app:
4842 pthread_mutex_unlock(&ua_sess->lock);
edb67388
DG
4843 }
4844
7f79d3a1 4845error:
edb67388 4846 rcu_read_unlock();
edb67388
DG
4847 return ret;
4848}
4849
4850/*
4851 * For a specific existing UST session and UST channel, creates the event for
4852 * all registered apps.
4853 */
35a9059d 4854int ust_app_create_event_glb(struct ltt_ust_session *usess,
edb67388
DG
4855 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4856{
4857 int ret = 0;
bec39940
DG
4858 struct lttng_ht_iter iter, uiter;
4859 struct lttng_ht_node_str *ua_chan_node;
edb67388
DG
4860 struct ust_app *app;
4861 struct ust_app_session *ua_sess;
4862 struct ust_app_channel *ua_chan;
4863
88e3c2f5 4864 assert(usess->active);
d9bf3ca4 4865 DBG("UST app creating event %s for all apps for session id %" PRIu64,
a991f516 4866 uevent->attr.name, usess->id);
edb67388 4867
edb67388
DG
4868 rcu_read_lock();
4869
4870 /* For all registered applications */
852d0037 4871 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
4872 if (!app->compatible) {
4873 /*
4874 * TODO: In time, we should notice the caller of this error by
4875 * telling him that this is a version error.
4876 */
4877 continue;
4878 }
edb67388 4879 ua_sess = lookup_session_by_app(usess, app);
c4a1715b
DG
4880 if (!ua_sess) {
4881 /* The application has problem or is probably dead. */
4882 continue;
4883 }
48842b30 4884
d0b96690 4885 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
4886
4887 if (ua_sess->deleted) {
4888 pthread_mutex_unlock(&ua_sess->lock);
4889 continue;
4890 }
4891
48842b30 4892 /* Lookup channel in the ust app session */
bec39940
DG
4893 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4894 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
edb67388
DG
4895 /* If the channel is not found, there is a code flow error */
4896 assert(ua_chan_node);
4897
48842b30
DG
4898 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4899
edb67388 4900 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
d0b96690 4901 pthread_mutex_unlock(&ua_sess->lock);
edb67388 4902 if (ret < 0) {
49c336c1 4903 if (ret != -LTTNG_UST_ERR_EXIST) {
fc34caaa
DG
4904 /* Possible value at this point: -ENOMEM. If so, we stop! */
4905 break;
4906 }
4907 DBG2("UST app event %s already exist on app PID %d",
852d0037 4908 uevent->attr.name, app->pid);
5b4a0ec0 4909 continue;
48842b30 4910 }
48842b30 4911 }
5b4a0ec0 4912
48842b30 4913 rcu_read_unlock();
48842b30
DG
4914 return ret;
4915}
4916
5b4a0ec0
DG
4917/*
4918 * Start tracing for a specific UST session and app.
fad1ed2f
JR
4919 *
4920 * Called with UST app session lock held.
4921 *
5b4a0ec0 4922 */
b34cbebf 4923static
421cb601 4924int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
48842b30
DG
4925{
4926 int ret = 0;
48842b30 4927 struct ust_app_session *ua_sess;
48842b30 4928
852d0037 4929 DBG("Starting tracing for ust app pid %d", app->pid);
5cf5d0e7 4930
509cbaf8
MD
4931 rcu_read_lock();
4932
e0c7ec2b
DG
4933 if (!app->compatible) {
4934 goto end;
4935 }
4936
421cb601
DG
4937 ua_sess = lookup_session_by_app(usess, app);
4938 if (ua_sess == NULL) {
d42f20df
DG
4939 /* The session is in teardown process. Ignore and continue. */
4940 goto end;
421cb601 4941 }
48842b30 4942
d0b96690
DG
4943 pthread_mutex_lock(&ua_sess->lock);
4944
b161602a
MD
4945 if (ua_sess->deleted) {
4946 pthread_mutex_unlock(&ua_sess->lock);
4947 goto end;
4948 }
4949
b0a1c741
JR
4950 if (ua_sess->enabled) {
4951 pthread_mutex_unlock(&ua_sess->lock);
4952 goto end;
4953 }
4954
aea829b3
DG
4955 /* Upon restart, we skip the setup, already done */
4956 if (ua_sess->started) {
8be98f9a 4957 goto skip_setup;
aea829b3 4958 }
8be98f9a 4959
840cb59c 4960 health_code_update();
86acf0da 4961
8be98f9a 4962skip_setup:
a945cdc7 4963 /* This starts the UST tracing */
fb45065e 4964 pthread_mutex_lock(&app->sock_lock);
852d0037 4965 ret = ustctl_start_session(app->sock, ua_sess->handle);
fb45065e 4966 pthread_mutex_unlock(&app->sock_lock);
421cb601 4967 if (ret < 0) {
ffe60014
DG
4968 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4969 ERR("Error starting tracing for app pid: %d (ret: %d)",
4970 app->pid, ret);
4971 } else {
4972 DBG("UST app start session failed. Application is dead.");
3757b385
DG
4973 /*
4974 * This is normal behavior, an application can die during the
4975 * creation process. Don't report an error so the execution can
4976 * continue normally.
4977 */
4978 pthread_mutex_unlock(&ua_sess->lock);
4979 goto end;
ffe60014 4980 }
d0b96690 4981 goto error_unlock;
421cb601 4982 }
5b4a0ec0 4983
55c3953d
DG
4984 /* Indicate that the session has been started once */
4985 ua_sess->started = 1;
b0a1c741 4986 ua_sess->enabled = 1;
55c3953d 4987
d0b96690
DG
4988 pthread_mutex_unlock(&ua_sess->lock);
4989
840cb59c 4990 health_code_update();
86acf0da 4991
421cb601 4992 /* Quiescent wait after starting trace */
fb45065e 4993 pthread_mutex_lock(&app->sock_lock);
ffe60014 4994 ret = ustctl_wait_quiescent(app->sock);
fb45065e 4995 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
4996 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
4997 ERR("UST app wait quiescent failed for app pid %d ret %d",
4998 app->pid, ret);
4999 }
48842b30 5000
e0c7ec2b
DG
5001end:
5002 rcu_read_unlock();
840cb59c 5003 health_code_update();
421cb601 5004 return 0;
48842b30 5005
d0b96690
DG
5006error_unlock:
5007 pthread_mutex_unlock(&ua_sess->lock);
509cbaf8 5008 rcu_read_unlock();
840cb59c 5009 health_code_update();
421cb601
DG
5010 return -1;
5011}
48842b30 5012
8be98f9a
MD
5013/*
5014 * Stop tracing for a specific UST session and app.
5015 */
b34cbebf 5016static
8be98f9a
MD
5017int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
5018{
5019 int ret = 0;
5020 struct ust_app_session *ua_sess;
7972aab2 5021 struct ust_registry_session *registry;
8be98f9a 5022
852d0037 5023 DBG("Stopping tracing for ust app pid %d", app->pid);
8be98f9a
MD
5024
5025 rcu_read_lock();
5026
e0c7ec2b 5027 if (!app->compatible) {
d88aee68 5028 goto end_no_session;
e0c7ec2b
DG
5029 }
5030
8be98f9a
MD
5031 ua_sess = lookup_session_by_app(usess, app);
5032 if (ua_sess == NULL) {
d88aee68 5033 goto end_no_session;
8be98f9a
MD
5034 }
5035
d88aee68
DG
5036 pthread_mutex_lock(&ua_sess->lock);
5037
b161602a
MD
5038 if (ua_sess->deleted) {
5039 pthread_mutex_unlock(&ua_sess->lock);
5040 goto end_no_session;
5041 }
5042
9bc07046
DG
5043 /*
5044 * If started = 0, it means that stop trace has been called for a session
c45536e1
DG
5045 * that was never started. It's possible since we can have a fail start
5046 * from either the application manager thread or the command thread. Simply
5047 * indicate that this is a stop error.
9bc07046 5048 */
f9dfc3d9 5049 if (!ua_sess->started) {
c45536e1
DG
5050 goto error_rcu_unlock;
5051 }
7db205b5 5052
840cb59c 5053 health_code_update();
86acf0da 5054
9d6c7d3f 5055 /* This inhibits UST tracing */
fb45065e 5056 pthread_mutex_lock(&app->sock_lock);
852d0037 5057 ret = ustctl_stop_session(app->sock, ua_sess->handle);
fb45065e 5058 pthread_mutex_unlock(&app->sock_lock);
9d6c7d3f 5059 if (ret < 0) {
ffe60014
DG
5060 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5061 ERR("Error stopping tracing for app pid: %d (ret: %d)",
5062 app->pid, ret);
5063 } else {
5064 DBG("UST app stop session failed. Application is dead.");
3757b385
DG
5065 /*
5066 * This is normal behavior, an application can die during the
5067 * creation process. Don't report an error so the execution can
5068 * continue normally.
5069 */
5070 goto end_unlock;
ffe60014 5071 }
9d6c7d3f
DG
5072 goto error_rcu_unlock;
5073 }
5074
840cb59c 5075 health_code_update();
b0a1c741 5076 ua_sess->enabled = 0;
86acf0da 5077
9d6c7d3f 5078 /* Quiescent wait after stopping trace */
fb45065e 5079 pthread_mutex_lock(&app->sock_lock);
ffe60014 5080 ret = ustctl_wait_quiescent(app->sock);
fb45065e 5081 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
5082 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5083 ERR("UST app wait quiescent failed for app pid %d ret %d",
5084 app->pid, ret);
5085 }
9d6c7d3f 5086
840cb59c 5087 health_code_update();
86acf0da 5088
b34cbebf 5089 registry = get_session_registry(ua_sess);
fad1ed2f
JR
5090
5091 /* The UST app session is held registry shall not be null. */
b34cbebf 5092 assert(registry);
1b532a60 5093
ce34fcd0
MD
5094 /* Push metadata for application before freeing the application. */
5095 (void) push_metadata(registry, ua_sess->consumer);
b34cbebf 5096
3757b385 5097end_unlock:
b34cbebf
MD
5098 pthread_mutex_unlock(&ua_sess->lock);
5099end_no_session:
5100 rcu_read_unlock();
5101 health_code_update();
5102 return 0;
5103
5104error_rcu_unlock:
5105 pthread_mutex_unlock(&ua_sess->lock);
5106 rcu_read_unlock();
5107 health_code_update();
5108 return -1;
5109}
5110
b34cbebf 5111static
c4b88406
MD
5112int ust_app_flush_app_session(struct ust_app *app,
5113 struct ust_app_session *ua_sess)
b34cbebf 5114{
c4b88406 5115 int ret, retval = 0;
b34cbebf 5116 struct lttng_ht_iter iter;
b34cbebf 5117 struct ust_app_channel *ua_chan;
c4b88406 5118 struct consumer_socket *socket;
b34cbebf 5119
c4b88406 5120 DBG("Flushing app session buffers for ust app pid %d", app->pid);
b34cbebf
MD
5121
5122 rcu_read_lock();
5123
5124 if (!app->compatible) {
c4b88406 5125 goto end_not_compatible;
b34cbebf
MD
5126 }
5127
5128 pthread_mutex_lock(&ua_sess->lock);
5129
b161602a
MD
5130 if (ua_sess->deleted) {
5131 goto end_deleted;
5132 }
5133
b34cbebf
MD
5134 health_code_update();
5135
9d6c7d3f 5136 /* Flushing buffers */
c4b88406
MD
5137 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5138 ua_sess->consumer);
ce34fcd0
MD
5139
5140 /* Flush buffers and push metadata. */
5141 switch (ua_sess->buffer_type) {
5142 case LTTNG_BUFFER_PER_PID:
5143 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
5144 node.node) {
5145 health_code_update();
ce34fcd0
MD
5146 ret = consumer_flush_channel(socket, ua_chan->key);
5147 if (ret) {
5148 ERR("Error flushing consumer channel");
5149 retval = -1;
5150 continue;
5151 }
8be98f9a 5152 }
ce34fcd0
MD
5153 break;
5154 case LTTNG_BUFFER_PER_UID:
5155 default:
5156 assert(0);
5157 break;
8be98f9a 5158 }
8be98f9a 5159
840cb59c 5160 health_code_update();
86acf0da 5161
b161602a 5162end_deleted:
d88aee68 5163 pthread_mutex_unlock(&ua_sess->lock);
ce34fcd0 5164
c4b88406
MD
5165end_not_compatible:
5166 rcu_read_unlock();
5167 health_code_update();
5168 return retval;
5169}
5170
5171/*
ce34fcd0
MD
5172 * Flush buffers for all applications for a specific UST session.
5173 * Called with UST session lock held.
c4b88406
MD
5174 */
5175static
ce34fcd0 5176int ust_app_flush_session(struct ltt_ust_session *usess)
c4b88406
MD
5177
5178{
99b1411c 5179 int ret = 0;
c4b88406 5180
ce34fcd0 5181 DBG("Flushing session buffers for all ust apps");
c4b88406
MD
5182
5183 rcu_read_lock();
5184
ce34fcd0
MD
5185 /* Flush buffers and push metadata. */
5186 switch (usess->buffer_type) {
5187 case LTTNG_BUFFER_PER_UID:
5188 {
5189 struct buffer_reg_uid *reg;
5190 struct lttng_ht_iter iter;
5191
5192 /* Flush all per UID buffers associated to that session. */
5193 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5194 struct ust_registry_session *ust_session_reg;
3273699d 5195 struct buffer_reg_channel *buf_reg_chan;
ce34fcd0
MD
5196 struct consumer_socket *socket;
5197
5198 /* Get consumer socket to use to push the metadata.*/
5199 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5200 usess->consumer);
5201 if (!socket) {
5202 /* Ignore request if no consumer is found for the session. */
5203 continue;
5204 }
5205
5206 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
3273699d 5207 buf_reg_chan, node.node) {
ce34fcd0
MD
5208 /*
5209 * The following call will print error values so the return
5210 * code is of little importance because whatever happens, we
5211 * have to try them all.
5212 */
3273699d 5213 (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key);
ce34fcd0
MD
5214 }
5215
5216 ust_session_reg = reg->registry->reg.ust;
5217 /* Push metadata. */
5218 (void) push_metadata(ust_session_reg, usess->consumer);
5219 }
ce34fcd0
MD
5220 break;
5221 }
5222 case LTTNG_BUFFER_PER_PID:
5223 {
5224 struct ust_app_session *ua_sess;
5225 struct lttng_ht_iter iter;
5226 struct ust_app *app;
5227
5228 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5229 ua_sess = lookup_session_by_app(usess, app);
5230 if (ua_sess == NULL) {
5231 continue;
5232 }
5233 (void) ust_app_flush_app_session(app, ua_sess);
5234 }
5235 break;
5236 }
5237 default:
99b1411c 5238 ret = -1;
ce34fcd0
MD
5239 assert(0);
5240 break;
c4b88406 5241 }
c4b88406 5242
7db205b5 5243 rcu_read_unlock();
840cb59c 5244 health_code_update();
c4b88406 5245 return ret;
8be98f9a
MD
5246}
5247
0dd01979
MD
5248static
5249int ust_app_clear_quiescent_app_session(struct ust_app *app,
5250 struct ust_app_session *ua_sess)
5251{
5252 int ret = 0;
5253 struct lttng_ht_iter iter;
5254 struct ust_app_channel *ua_chan;
5255 struct consumer_socket *socket;
5256
5257 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
5258
5259 rcu_read_lock();
5260
5261 if (!app->compatible) {
5262 goto end_not_compatible;
5263 }
5264
5265 pthread_mutex_lock(&ua_sess->lock);
5266
5267 if (ua_sess->deleted) {
5268 goto end_unlock;
5269 }
5270
5271 health_code_update();
5272
5273 socket = consumer_find_socket_by_bitness(app->bits_per_long,
5274 ua_sess->consumer);
5275 if (!socket) {
5276 ERR("Failed to find consumer (%" PRIu32 ") socket",
5277 app->bits_per_long);
5278 ret = -1;
5279 goto end_unlock;
5280 }
5281
5282 /* Clear quiescent state. */
5283 switch (ua_sess->buffer_type) {
5284 case LTTNG_BUFFER_PER_PID:
5285 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
5286 ua_chan, node.node) {
5287 health_code_update();
5288 ret = consumer_clear_quiescent_channel(socket,
5289 ua_chan->key);
5290 if (ret) {
5291 ERR("Error clearing quiescent state for consumer channel");
5292 ret = -1;
5293 continue;
5294 }
5295 }
5296 break;
5297 case LTTNG_BUFFER_PER_UID:
5298 default:
5299 assert(0);
5300 ret = -1;
5301 break;
5302 }
5303
5304 health_code_update();
5305
5306end_unlock:
5307 pthread_mutex_unlock(&ua_sess->lock);
5308
5309end_not_compatible:
5310 rcu_read_unlock();
5311 health_code_update();
5312 return ret;
5313}
5314
5315/*
5316 * Clear quiescent state in each stream for all applications for a
5317 * specific UST session.
5318 * Called with UST session lock held.
5319 */
5320static
5321int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
5322
5323{
5324 int ret = 0;
5325
5326 DBG("Clearing stream quiescent state for all ust apps");
5327
5328 rcu_read_lock();
5329
5330 switch (usess->buffer_type) {
5331 case LTTNG_BUFFER_PER_UID:
5332 {
5333 struct lttng_ht_iter iter;
5334 struct buffer_reg_uid *reg;
5335
5336 /*
5337 * Clear quiescent for all per UID buffers associated to
5338 * that session.
5339 */
5340 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5341 struct consumer_socket *socket;
3273699d 5342 struct buffer_reg_channel *buf_reg_chan;
0dd01979
MD
5343
5344 /* Get associated consumer socket.*/
5345 socket = consumer_find_socket_by_bitness(
5346 reg->bits_per_long, usess->consumer);
5347 if (!socket) {
5348 /*
5349 * Ignore request if no consumer is found for
5350 * the session.
5351 */
5352 continue;
5353 }
5354
5355 cds_lfht_for_each_entry(reg->registry->channels->ht,
3273699d 5356 &iter.iter, buf_reg_chan, node.node) {
0dd01979
MD
5357 /*
5358 * The following call will print error values so
5359 * the return code is of little importance
5360 * because whatever happens, we have to try them
5361 * all.
5362 */
5363 (void) consumer_clear_quiescent_channel(socket,
3273699d 5364 buf_reg_chan->consumer_key);
0dd01979
MD
5365 }
5366 }
5367 break;
5368 }
5369 case LTTNG_BUFFER_PER_PID:
5370 {
5371 struct ust_app_session *ua_sess;
5372 struct lttng_ht_iter iter;
5373 struct ust_app *app;
5374
5375 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
5376 pid_n.node) {
5377 ua_sess = lookup_session_by_app(usess, app);
5378 if (ua_sess == NULL) {
5379 continue;
5380 }
5381 (void) ust_app_clear_quiescent_app_session(app,
5382 ua_sess);
5383 }
5384 break;
5385 }
5386 default:
5387 ret = -1;
5388 assert(0);
5389 break;
5390 }
5391
5392 rcu_read_unlock();
5393 health_code_update();
5394 return ret;
5395}
5396
84cd17c6
MD
5397/*
5398 * Destroy a specific UST session in apps.
5399 */
3353de95 5400static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
84cd17c6 5401{
ffe60014 5402 int ret;
84cd17c6 5403 struct ust_app_session *ua_sess;
bec39940 5404 struct lttng_ht_iter iter;
d9bf3ca4 5405 struct lttng_ht_node_u64 *node;
84cd17c6 5406
852d0037 5407 DBG("Destroy tracing for ust app pid %d", app->pid);
84cd17c6
MD
5408
5409 rcu_read_lock();
5410
e0c7ec2b
DG
5411 if (!app->compatible) {
5412 goto end;
5413 }
5414
84cd17c6 5415 __lookup_session_by_app(usess, app, &iter);
d9bf3ca4 5416 node = lttng_ht_iter_get_node_u64(&iter);
84cd17c6 5417 if (node == NULL) {
d42f20df
DG
5418 /* Session is being or is deleted. */
5419 goto end;
84cd17c6
MD
5420 }
5421 ua_sess = caa_container_of(node, struct ust_app_session, node);
c4a1715b 5422
840cb59c 5423 health_code_update();
d0b96690 5424 destroy_app_session(app, ua_sess);
84cd17c6 5425
840cb59c 5426 health_code_update();
7db205b5 5427
84cd17c6 5428 /* Quiescent wait after stopping trace */
fb45065e 5429 pthread_mutex_lock(&app->sock_lock);
ffe60014 5430 ret = ustctl_wait_quiescent(app->sock);
fb45065e 5431 pthread_mutex_unlock(&app->sock_lock);
ffe60014
DG
5432 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
5433 ERR("UST app wait quiescent failed for app pid %d ret %d",
5434 app->pid, ret);
5435 }
e0c7ec2b
DG
5436end:
5437 rcu_read_unlock();
840cb59c 5438 health_code_update();
84cd17c6 5439 return 0;
84cd17c6
MD
5440}
5441
5b4a0ec0
DG
5442/*
5443 * Start tracing for the UST session.
5444 */
421cb601
DG
5445int ust_app_start_trace_all(struct ltt_ust_session *usess)
5446{
bec39940 5447 struct lttng_ht_iter iter;
421cb601 5448 struct ust_app *app;
48842b30 5449
421cb601
DG
5450 DBG("Starting all UST traces");
5451
bb2452c8
MD
5452 /*
5453 * Even though the start trace might fail, flag this session active so
5454 * other application coming in are started by default.
5455 */
5456 usess->active = 1;
5457
421cb601 5458 rcu_read_lock();
421cb601 5459
0dd01979
MD
5460 /*
5461 * In a start-stop-start use-case, we need to clear the quiescent state
5462 * of each channel set by the prior stop command, thus ensuring that a
5463 * following stop or destroy is sure to grab a timestamp_end near those
5464 * operations, even if the packet is empty.
5465 */
5466 (void) ust_app_clear_quiescent_session(usess);
5467
0498a00c
MD
5468 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5469 ust_app_global_update(usess, app);
5470 }
5471
48842b30
DG
5472 rcu_read_unlock();
5473
5474 return 0;
5475}
487cf67c 5476
8be98f9a
MD
5477/*
5478 * Start tracing for the UST session.
ce34fcd0 5479 * Called with UST session lock held.
8be98f9a
MD
5480 */
5481int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5482{
5483 int ret = 0;
bec39940 5484 struct lttng_ht_iter iter;
8be98f9a
MD
5485 struct ust_app *app;
5486
5487 DBG("Stopping all UST traces");
5488
bb2452c8
MD
5489 /*
5490 * Even though the stop trace might fail, flag this session inactive so
5491 * other application coming in are not started by default.
5492 */
5493 usess->active = 0;
5494
8be98f9a
MD
5495 rcu_read_lock();
5496
b34cbebf
MD
5497 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5498 ret = ust_app_stop_trace(usess, app);
5499 if (ret < 0) {
5500 /* Continue to next apps even on error */
5501 continue;
5502 }
5503 }
5504
ce34fcd0 5505 (void) ust_app_flush_session(usess);
8be98f9a
MD
5506
5507 rcu_read_unlock();
5508
5509 return 0;
5510}
5511
84cd17c6
MD
5512/*
5513 * Destroy app UST session.
5514 */
5515int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5516{
5517 int ret = 0;
bec39940 5518 struct lttng_ht_iter iter;
84cd17c6
MD
5519 struct ust_app *app;
5520
5521 DBG("Destroy all UST traces");
5522
5523 rcu_read_lock();
5524
852d0037 5525 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3353de95 5526 ret = destroy_trace(usess, app);
84cd17c6
MD
5527 if (ret < 0) {
5528 /* Continue to next apps even on error */
5529 continue;
5530 }
5531 }
5532
5533 rcu_read_unlock();
5534
5535 return 0;
5536}
5537
88e3c2f5 5538/* The ua_sess lock must be held by the caller. */
a9ad0c8f 5539static
88e3c2f5
JG
5540int find_or_create_ust_app_channel(
5541 struct ltt_ust_session *usess,
5542 struct ust_app_session *ua_sess,
5543 struct ust_app *app,
5544 struct ltt_ust_channel *uchan,
5545 struct ust_app_channel **ua_chan)
487cf67c 5546{
55c54cce 5547 int ret = 0;
88e3c2f5
JG
5548 struct lttng_ht_iter iter;
5549 struct lttng_ht_node_str *ua_chan_node;
5550
5551 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5552 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5553 if (ua_chan_node) {
5554 *ua_chan = caa_container_of(ua_chan_node,
5555 struct ust_app_channel, node);
5556 goto end;
5557 }
5558
5559 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5560 if (ret) {
5561 goto end;
5562 }
5563end:
5564 return ret;
5565}
5566
5567static
5568int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5569 struct ltt_ust_event *uevent, struct ust_app_session *ua_sess,
5570 struct ust_app *app)
5571{
5572 int ret = 0;
5573 struct ust_app_event *ua_event = NULL;
5574
5575 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5576 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5577 if (!ua_event) {
5578 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
5579 if (ret < 0) {
5580 goto end;
5581 }
5582 } else {
5583 if (ua_event->enabled != uevent->enabled) {
5584 ret = uevent->enabled ?
5585 enable_ust_app_event(ua_sess, ua_event, app) :
5586 disable_ust_app_event(ua_sess, ua_event, app);
5587 }
5588 }
5589
5590end:
5591 return ret;
5592}
5593
993578ff
JR
5594/* Called with RCU read-side lock held. */
5595static
5596void ust_app_synchronize_event_notifier_rules(struct ust_app *app)
5597{
5598 int ret = 0;
5599 enum lttng_error_code ret_code;
5600 enum lttng_trigger_status t_status;
5601 struct lttng_ht_iter app_trigger_iter;
5602 struct lttng_triggers *triggers = NULL;
5603 struct ust_app_event_notifier_rule *event_notifier_rule;
5604 unsigned int count, i;
5605
5606 /*
5607 * Currrently, registering or unregistering a trigger with an
5608 * event rule condition causes a full synchronization of the event
5609 * notifiers.
5610 *
5611 * The first step attempts to add an event notifier for all registered
5612 * triggers that apply to the user space tracers. Then, the
5613 * application's event notifiers rules are all checked against the list
5614 * of registered triggers. Any event notifier that doesn't have a
5615 * matching trigger can be assumed to have been disabled.
5616 *
5617 * All of this is inefficient, but is put in place to get the feature
5618 * rolling as it is simpler at this moment. It will be optimized Soon™
5619 * to allow the state of enabled
5620 * event notifiers to be synchronized in a piece-wise way.
5621 */
5622
5623 /* Get all triggers using uid 0 (root) */
5624 ret_code = notification_thread_command_list_triggers(
5625 notification_thread_handle, 0, &triggers);
5626 if (ret_code != LTTNG_OK) {
5627 ret = -1;
5628 goto end;
5629 }
5630
5631 assert(triggers);
5632
5633 t_status = lttng_triggers_get_count(triggers, &count);
5634 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
5635 ret = -1;
5636 goto end;
5637 }
5638
5639 for (i = 0; i < count; i++) {
5640 struct lttng_condition *condition;
5641 struct lttng_event_rule *event_rule;
5642 struct lttng_trigger *trigger;
5643 const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule;
5644 enum lttng_condition_status condition_status;
5645 uint64_t token;
5646
5647 trigger = lttng_triggers_borrow_mutable_at_index(triggers, i);
5648 assert(trigger);
5649
5650 token = lttng_trigger_get_tracer_token(trigger);
5651 condition = lttng_trigger_get_condition(trigger);
5652
d602bd6a 5653 if (lttng_condition_get_type(condition) != LTTNG_CONDITION_TYPE_ON_EVENT) {
993578ff
JR
5654 /* Does not apply */
5655 continue;
5656 }
5657
d602bd6a 5658 condition_status = lttng_condition_on_event_borrow_rule_mutable(condition, &event_rule);
993578ff
JR
5659 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
5660
5661 if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) {
5662 /* Skip kernel related triggers. */
5663 continue;
5664 }
5665
5666 /*
5667 * Find or create the associated token event rule. The caller
5668 * holds the RCU read lock, so this is safe to call without
5669 * explicitly acquiring it here.
5670 */
5671 looked_up_event_notifier_rule = find_ust_app_event_notifier_rule(
5672 app->token_to_event_notifier_rule_ht, token);
5673 if (!looked_up_event_notifier_rule) {
267d66aa 5674 ret = create_ust_app_event_notifier_rule(trigger, app);
993578ff
JR
5675 if (ret < 0) {
5676 goto end;
5677 }
5678 }
5679 }
5680
5681 rcu_read_lock();
5682 /* Remove all unknown event sources from the app. */
5683 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
5684 &app_trigger_iter.iter, event_notifier_rule,
5685 node.node) {
5686 const uint64_t app_token = event_notifier_rule->token;
5687 bool found = false;
5688
5689 /*
5690 * Check if the app event trigger still exists on the
5691 * notification side.
5692 */
5693 for (i = 0; i < count; i++) {
5694 uint64_t notification_thread_token;
5695 const struct lttng_trigger *trigger =
5696 lttng_triggers_get_at_index(
5697 triggers, i);
5698
5699 assert(trigger);
5700
5701 notification_thread_token =
5702 lttng_trigger_get_tracer_token(trigger);
5703
5704 if (notification_thread_token == app_token) {
5705 found = true;
5706 break;
5707 }
5708 }
5709
5710 if (found) {
5711 /* Still valid. */
5712 continue;
5713 }
5714
5715 /*
5716 * This trigger was unregistered, disable it on the tracer's
5717 * side.
5718 */
5719 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht,
5720 &app_trigger_iter);
5721 assert(ret == 0);
5722
5723 /* Callee logs errors. */
5724 (void) disable_ust_object(app, event_notifier_rule->obj);
5725
5726 delete_ust_app_event_notifier_rule(
5727 app->sock, event_notifier_rule, app);
5728 }
5729
5730 rcu_read_unlock();
5731
5732end:
5733 lttng_triggers_destroy(triggers);
5734 return;
5735}
5736
88e3c2f5 5737/*
a84d1024 5738 * RCU read lock must be held by the caller.
88e3c2f5
JG
5739 */
5740static
a84d1024
FD
5741void ust_app_synchronize_all_channels(struct ltt_ust_session *usess,
5742 struct ust_app_session *ua_sess,
88e3c2f5
JG
5743 struct ust_app *app)
5744{
5745 int ret = 0;
5746 struct cds_lfht_iter uchan_iter;
5747 struct ltt_ust_channel *uchan;
1f3580c7 5748
a84d1024 5749 assert(usess);
3d8ca23b 5750 assert(ua_sess);
a84d1024 5751 assert(app);
ef67c072 5752
88e3c2f5
JG
5753 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5754 uchan, node.node) {
5755 struct ust_app_channel *ua_chan;
5756 struct cds_lfht_iter uevent_iter;
5757 struct ltt_ust_event *uevent;
487cf67c 5758
31746f93 5759 /*
88e3c2f5
JG
5760 * Search for a matching ust_app_channel. If none is found,
5761 * create it. Creating the channel will cause the ua_chan
5762 * structure to be allocated, the channel buffers to be
5763 * allocated (if necessary) and sent to the application, and
5764 * all enabled contexts will be added to the channel.
31746f93 5765 */
f3db82be 5766 ret = find_or_create_ust_app_channel(usess, ua_sess,
88e3c2f5
JG
5767 app, uchan, &ua_chan);
5768 if (ret) {
5769 /* Tracer is probably gone or ENOMEM. */
a84d1024 5770 goto end;
727d5404
DG
5771 }
5772
88e3c2f5
JG
5773 if (!ua_chan) {
5774 /* ua_chan will be NULL for the metadata channel */
5775 continue;
5776 }
727d5404 5777
88e3c2f5 5778 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
bec39940 5779 node.node) {
88e3c2f5
JG
5780 ret = ust_app_channel_synchronize_event(ua_chan,
5781 uevent, ua_sess, app);
5782 if (ret) {
a84d1024 5783 goto end;
487cf67c 5784 }
36dc12cc 5785 }
d0b96690 5786
88e3c2f5
JG
5787 if (ua_chan->enabled != uchan->enabled) {
5788 ret = uchan->enabled ?
5789 enable_ust_app_channel(ua_sess, uchan, app) :
5790 disable_ust_app_channel(ua_sess, ua_chan, app);
5791 if (ret) {
a84d1024 5792 goto end;
88e3c2f5
JG
5793 }
5794 }
36dc12cc 5795 }
a84d1024
FD
5796end:
5797 return;
5798}
5799
5800/*
5801 * The caller must ensure that the application is compatible and is tracked
5802 * by the process attribute trackers.
5803 */
5804static
5805void ust_app_synchronize(struct ltt_ust_session *usess,
5806 struct ust_app *app)
5807{
5808 int ret = 0;
5809 struct ust_app_session *ua_sess = NULL;
5810
5811 /*
5812 * The application's configuration should only be synchronized for
5813 * active sessions.
5814 */
5815 assert(usess->active);
5816
5817 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
5818 if (ret < 0) {
5819 /* Tracer is probably gone or ENOMEM. */
5820 goto error;
5821 }
5822 assert(ua_sess);
5823
5824 pthread_mutex_lock(&ua_sess->lock);
5825 if (ua_sess->deleted) {
5826 pthread_mutex_unlock(&ua_sess->lock);
5827 goto end;
5828 }
5829
5830 rcu_read_lock();
5831
5832 ust_app_synchronize_all_channels(usess, ua_sess, app);
ef67c072
JG
5833
5834 /*
5835 * Create the metadata for the application. This returns gracefully if a
5836 * metadata was already set for the session.
5837 *
5838 * The metadata channel must be created after the data channels as the
5839 * consumer daemon assumes this ordering. When interacting with a relay
5840 * daemon, the consumer will use this assumption to send the
5841 * "STREAMS_SENT" message to the relay daemon.
5842 */
5843 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
5844 if (ret < 0) {
5845 goto error_unlock;
5846 }
5847
88e3c2f5 5848 rcu_read_unlock();
0498a00c 5849
a9ad0c8f 5850end:
88e3c2f5 5851 pthread_mutex_unlock(&ua_sess->lock);
ffe60014 5852 /* Everything went well at this point. */
ffe60014
DG
5853 return;
5854
d0b96690 5855error_unlock:
88e3c2f5 5856 rcu_read_unlock();
d0b96690 5857 pthread_mutex_unlock(&ua_sess->lock);
487cf67c 5858error:
ffe60014 5859 if (ua_sess) {
d0b96690 5860 destroy_app_session(app, ua_sess);
ffe60014 5861 }
487cf67c
DG
5862 return;
5863}
55cc08a6 5864
a9ad0c8f
MD
5865static
5866void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
5867{
5868 struct ust_app_session *ua_sess;
5869
5870 ua_sess = lookup_session_by_app(usess, app);
5871 if (ua_sess == NULL) {
5872 return;
5873 }
5874 destroy_app_session(app, ua_sess);
5875}
5876
5877/*
5878 * Add channels/events from UST global domain to registered apps at sock.
5879 *
5880 * Called with session lock held.
5881 * Called with RCU read-side lock held.
5882 */
5883void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
5884{
5885 assert(usess);
88e3c2f5 5886 assert(usess->active);
a9ad0c8f
MD
5887
5888 DBG2("UST app global update for app sock %d for session id %" PRIu64,
5889 app->sock, usess->id);
5890
5891 if (!app->compatible) {
5892 return;
5893 }
159b042f
JG
5894 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID,
5895 usess, app->pid) &&
55c9e7ca 5896 trace_ust_id_tracker_lookup(
159b042f
JG
5897 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID,
5898 usess, app->uid) &&
55c9e7ca 5899 trace_ust_id_tracker_lookup(
159b042f
JG
5900 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID,
5901 usess, app->gid)) {
88e3c2f5
JG
5902 /*
5903 * Synchronize the application's internal tracing configuration
5904 * and start tracing.
5905 */
5906 ust_app_synchronize(usess, app);
5907 ust_app_start_trace(usess, app);
a9ad0c8f
MD
5908 } else {
5909 ust_app_global_destroy(usess, app);
5910 }
5911}
5912
993578ff
JR
5913/*
5914 * Add all event notifiers to an application.
5915 *
5916 * Called with session lock held.
5917 * Called with RCU read-side lock held.
5918 */
5919void ust_app_global_update_event_notifier_rules(struct ust_app *app)
5920{
5921 DBG2("UST application global event notifier rules update: app = '%s' (ppid: %d)",
5922 app->name, app->ppid);
5923
5924 if (!app->compatible) {
5925 return;
5926 }
5927
5928 if (app->event_notifier_group.object == NULL) {
5929 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s' (ppid: %d)",
5930 app->name, app->ppid);
5931 return;
5932 }
5933
5934 ust_app_synchronize_event_notifier_rules(app);
5935}
5936
a9ad0c8f
MD
5937/*
5938 * Called with session lock held.
5939 */
5940void ust_app_global_update_all(struct ltt_ust_session *usess)
5941{
5942 struct lttng_ht_iter iter;
5943 struct ust_app *app;
5944
5945 rcu_read_lock();
5946 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5947 ust_app_global_update(usess, app);
5948 }
5949 rcu_read_unlock();
5950}
5951
993578ff
JR
5952void ust_app_global_update_all_event_notifier_rules(void)
5953{
5954 struct lttng_ht_iter iter;
5955 struct ust_app *app;
5956
5957 rcu_read_lock();
5958 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5959 ust_app_global_update_event_notifier_rules(app);
5960 }
5961
5962 rcu_read_unlock();
5963}
5964
55cc08a6
DG
5965/*
5966 * Add context to a specific channel for global UST domain.
5967 */
5968int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
5969 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
5970{
5971 int ret = 0;
bec39940
DG
5972 struct lttng_ht_node_str *ua_chan_node;
5973 struct lttng_ht_iter iter, uiter;
55cc08a6
DG
5974 struct ust_app_channel *ua_chan = NULL;
5975 struct ust_app_session *ua_sess;
5976 struct ust_app *app;
5977
88e3c2f5 5978 assert(usess->active);
0498a00c 5979
55cc08a6 5980 rcu_read_lock();
852d0037 5981 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
e0c7ec2b
DG
5982 if (!app->compatible) {
5983 /*
5984 * TODO: In time, we should notice the caller of this error by
5985 * telling him that this is a version error.
5986 */
5987 continue;
5988 }
55cc08a6
DG
5989 ua_sess = lookup_session_by_app(usess, app);
5990 if (ua_sess == NULL) {
5991 continue;
5992 }
5993
d0b96690 5994 pthread_mutex_lock(&ua_sess->lock);
b161602a
MD
5995
5996 if (ua_sess->deleted) {
5997 pthread_mutex_unlock(&ua_sess->lock);
5998 continue;
5999 }
6000
55cc08a6 6001 /* Lookup channel in the ust app session */
bec39940
DG
6002 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
6003 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
55cc08a6 6004 if (ua_chan_node == NULL) {
d0b96690 6005 goto next_app;
55cc08a6
DG
6006 }
6007 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
6008 node);
c9edf082 6009 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
55cc08a6 6010 if (ret < 0) {
d0b96690 6011 goto next_app;
55cc08a6 6012 }
d0b96690
DG
6013 next_app:
6014 pthread_mutex_unlock(&ua_sess->lock);
55cc08a6
DG
6015 }
6016
55cc08a6 6017 rcu_read_unlock();
76d45b40
DG
6018 return ret;
6019}
7f79d3a1 6020
d0b96690
DG
6021/*
6022 * Receive registration and populate the given msg structure.
6023 *
6024 * On success return 0 else a negative value returned by the ustctl call.
6025 */
6026int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
6027{
6028 int ret;
6029 uint32_t pid, ppid, uid, gid;
6030
6031 assert(msg);
6032
6033 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
6034 &pid, &ppid, &uid, &gid,
6035 &msg->bits_per_long,
6036 &msg->uint8_t_alignment,
6037 &msg->uint16_t_alignment,
6038 &msg->uint32_t_alignment,
6039 &msg->uint64_t_alignment,
6040 &msg->long_alignment,
6041 &msg->byte_order,
6042 msg->name);
6043 if (ret < 0) {
6044 switch (-ret) {
6045 case EPIPE:
6046 case ECONNRESET:
6047 case LTTNG_UST_ERR_EXITING:
6048 DBG3("UST app recv reg message failed. Application died");
6049 break;
6050 case LTTNG_UST_ERR_UNSUP_MAJOR:
6051 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6052 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
6053 LTTNG_UST_ABI_MINOR_VERSION);
6054 break;
6055 default:
6056 ERR("UST app recv reg message failed with ret %d", ret);
6057 break;
6058 }
6059 goto error;
6060 }
6061 msg->pid = (pid_t) pid;
6062 msg->ppid = (pid_t) ppid;
6063 msg->uid = (uid_t) uid;
6064 msg->gid = (gid_t) gid;
6065
6066error:
6067 return ret;
6068}
6069
10b56aef
MD
6070/*
6071 * Return a ust app session object using the application object and the
6072 * session object descriptor has a key. If not found, NULL is returned.
6073 * A RCU read side lock MUST be acquired when calling this function.
6074*/
6075static struct ust_app_session *find_session_by_objd(struct ust_app *app,
6076 int objd)
6077{
6078 struct lttng_ht_node_ulong *node;
6079 struct lttng_ht_iter iter;
6080 struct ust_app_session *ua_sess = NULL;
6081
6082 assert(app);
6083
6084 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
6085 node = lttng_ht_iter_get_node_ulong(&iter);
6086 if (node == NULL) {
6087 DBG2("UST app session find by objd %d not found", objd);
6088 goto error;
6089 }
6090
6091 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
6092
6093error:
6094 return ua_sess;
6095}
6096
d88aee68
DG
6097/*
6098 * Return a ust app channel object using the application object and the channel
6099 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6100 * lock MUST be acquired before calling this function.
6101 */
d0b96690
DG
6102static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
6103 int objd)
6104{
6105 struct lttng_ht_node_ulong *node;
6106 struct lttng_ht_iter iter;
6107 struct ust_app_channel *ua_chan = NULL;
6108
6109 assert(app);
6110
6111 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
6112 node = lttng_ht_iter_get_node_ulong(&iter);
6113 if (node == NULL) {
6114 DBG2("UST app channel find by objd %d not found", objd);
6115 goto error;
6116 }
6117
6118 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
6119
6120error:
6121 return ua_chan;
6122}
6123
d88aee68
DG
6124/*
6125 * Reply to a register channel notification from an application on the notify
6126 * socket. The channel metadata is also created.
6127 *
6128 * The session UST registry lock is acquired in this function.
6129 *
6130 * On success 0 is returned else a negative value.
6131 */
8eede835 6132static int reply_ust_register_channel(int sock, int cobjd,
d0b96690
DG
6133 size_t nr_fields, struct ustctl_field *fields)
6134{
6135 int ret, ret_code = 0;
294e218e 6136 uint32_t chan_id;
7972aab2 6137 uint64_t chan_reg_key;
d0b96690
DG
6138 enum ustctl_channel_header type;
6139 struct ust_app *app;
6140 struct ust_app_channel *ua_chan;
6141 struct ust_app_session *ua_sess;
7972aab2 6142 struct ust_registry_session *registry;
3273699d 6143 struct ust_registry_channel *ust_reg_chan;
d0b96690
DG
6144
6145 rcu_read_lock();
6146
6147 /* Lookup application. If not found, there is a code flow error. */
6148 app = find_app_by_notify_sock(sock);
d88aee68 6149 if (!app) {
fad1ed2f 6150 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68
DG
6151 sock);
6152 ret = 0;
6153 goto error_rcu_unlock;
6154 }
d0b96690 6155
4950b860 6156 /* Lookup channel by UST object descriptor. */
d0b96690 6157 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 6158 if (!ua_chan) {
fad1ed2f 6159 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
6160 ret = 0;
6161 goto error_rcu_unlock;
6162 }
6163
d0b96690
DG
6164 assert(ua_chan->session);
6165 ua_sess = ua_chan->session;
d0b96690 6166
7972aab2
DG
6167 /* Get right session registry depending on the session buffer type. */
6168 registry = get_session_registry(ua_sess);
fad1ed2f
JR
6169 if (!registry) {
6170 DBG("Application session is being torn down. Abort event notify");
6171 ret = 0;
6172 goto error_rcu_unlock;
6173 };
45893984 6174
7972aab2
DG
6175 /* Depending on the buffer type, a different channel key is used. */
6176 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6177 chan_reg_key = ua_chan->tracing_channel_id;
d0b96690 6178 } else {
7972aab2 6179 chan_reg_key = ua_chan->key;
d0b96690
DG
6180 }
6181
7972aab2
DG
6182 pthread_mutex_lock(&registry->lock);
6183
3273699d
FD
6184 ust_reg_chan = ust_registry_channel_find(registry, chan_reg_key);
6185 assert(ust_reg_chan);
7972aab2 6186
3273699d 6187 if (!ust_reg_chan->register_done) {
294e218e
MD
6188 /*
6189 * TODO: eventually use the registry event count for
6190 * this channel to better guess header type for per-pid
6191 * buffers.
6192 */
6193 type = USTCTL_CHANNEL_HEADER_LARGE;
3273699d
FD
6194 ust_reg_chan->nr_ctx_fields = nr_fields;
6195 ust_reg_chan->ctx_fields = fields;
fad1ed2f 6196 fields = NULL;
3273699d 6197 ust_reg_chan->header_type = type;
d0b96690 6198 } else {
7972aab2 6199 /* Get current already assigned values. */
3273699d 6200 type = ust_reg_chan->header_type;
d0b96690 6201 }
7972aab2 6202 /* Channel id is set during the object creation. */
3273699d 6203 chan_id = ust_reg_chan->chan_id;
d0b96690
DG
6204
6205 /* Append to metadata */
3273699d
FD
6206 if (!ust_reg_chan->metadata_dumped) {
6207 ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan);
d0b96690
DG
6208 if (ret_code) {
6209 ERR("Error appending channel metadata (errno = %d)", ret_code);
6210 goto reply;
6211 }
6212 }
6213
6214reply:
7972aab2
DG
6215 DBG3("UST app replying to register channel key %" PRIu64
6216 " with id %u, type: %d, ret: %d", chan_reg_key, chan_id, type,
6217 ret_code);
d0b96690
DG
6218
6219 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
6220 if (ret < 0) {
6221 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6222 ERR("UST app reply channel failed with ret %d", ret);
6223 } else {
6224 DBG3("UST app reply channel failed. Application died");
6225 }
6226 goto error;
6227 }
6228
7972aab2 6229 /* This channel registry registration is completed. */
3273699d 6230 ust_reg_chan->register_done = 1;
7972aab2 6231
d0b96690 6232error:
7972aab2 6233 pthread_mutex_unlock(&registry->lock);
d88aee68 6234error_rcu_unlock:
d0b96690 6235 rcu_read_unlock();
fad1ed2f 6236 free(fields);
d0b96690
DG
6237 return ret;
6238}
6239
d88aee68
DG
6240/*
6241 * Add event to the UST channel registry. When the event is added to the
6242 * registry, the metadata is also created. Once done, this replies to the
6243 * application with the appropriate error code.
6244 *
6245 * The session UST registry lock is acquired in the function.
6246 *
6247 * On success 0 is returned else a negative value.
6248 */
d0b96690 6249static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
2106efa0
PP
6250 char *sig, size_t nr_fields, struct ustctl_field *fields,
6251 int loglevel_value, char *model_emf_uri)
d0b96690
DG
6252{
6253 int ret, ret_code;
6254 uint32_t event_id = 0;
7972aab2 6255 uint64_t chan_reg_key;
d0b96690
DG
6256 struct ust_app *app;
6257 struct ust_app_channel *ua_chan;
6258 struct ust_app_session *ua_sess;
7972aab2 6259 struct ust_registry_session *registry;
d0b96690
DG
6260
6261 rcu_read_lock();
6262
6263 /* Lookup application. If not found, there is a code flow error. */
6264 app = find_app_by_notify_sock(sock);
d88aee68 6265 if (!app) {
fad1ed2f 6266 DBG("Application socket %d is being torn down. Abort event notify",
d88aee68
DG
6267 sock);
6268 ret = 0;
6269 goto error_rcu_unlock;
6270 }
d0b96690 6271
4950b860 6272 /* Lookup channel by UST object descriptor. */
d0b96690 6273 ua_chan = find_channel_by_objd(app, cobjd);
4950b860 6274 if (!ua_chan) {
fad1ed2f 6275 DBG("Application channel is being torn down. Abort event notify");
4950b860
MD
6276 ret = 0;
6277 goto error_rcu_unlock;
6278 }
6279
d0b96690
DG
6280 assert(ua_chan->session);
6281 ua_sess = ua_chan->session;
6282
7972aab2 6283 registry = get_session_registry(ua_sess);
fad1ed2f
JR
6284 if (!registry) {
6285 DBG("Application session is being torn down. Abort event notify");
6286 ret = 0;
6287 goto error_rcu_unlock;
6288 }
7972aab2
DG
6289
6290 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6291 chan_reg_key = ua_chan->tracing_channel_id;
6292 } else {
6293 chan_reg_key = ua_chan->key;
6294 }
6295
6296 pthread_mutex_lock(&registry->lock);
d0b96690 6297
d5d629b5
DG
6298 /*
6299 * From this point on, this call acquires the ownership of the sig, fields
6300 * and model_emf_uri meaning any free are done inside it if needed. These
6301 * three variables MUST NOT be read/write after this.
6302 */
7972aab2 6303 ret_code = ust_registry_create_event(registry, chan_reg_key,
2106efa0
PP
6304 sobjd, cobjd, name, sig, nr_fields, fields,
6305 loglevel_value, model_emf_uri, ua_sess->buffer_type,
6306 &event_id, app);
fad1ed2f
JR
6307 sig = NULL;
6308 fields = NULL;
6309 model_emf_uri = NULL;
d0b96690
DG
6310
6311 /*
6312 * The return value is returned to ustctl so in case of an error, the
6313 * application can be notified. In case of an error, it's important not to
6314 * return a negative error or else the application will get closed.
6315 */
6316 ret = ustctl_reply_register_event(sock, event_id, ret_code);
6317 if (ret < 0) {
6318 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6319 ERR("UST app reply event failed with ret %d", ret);
6320 } else {
6321 DBG3("UST app reply event failed. Application died");
6322 }
6323 /*
6324 * No need to wipe the create event since the application socket will
6325 * get close on error hence cleaning up everything by itself.
6326 */
6327 goto error;
6328 }
6329
7972aab2
DG
6330 DBG3("UST registry event %s with id %" PRId32 " added successfully",
6331 name, event_id);
d88aee68 6332
d0b96690 6333error:
7972aab2 6334 pthread_mutex_unlock(&registry->lock);
d88aee68 6335error_rcu_unlock:
d0b96690 6336 rcu_read_unlock();
fad1ed2f
JR
6337 free(sig);
6338 free(fields);
6339 free(model_emf_uri);
d0b96690
DG
6340 return ret;
6341}
6342
10b56aef
MD
6343/*
6344 * Add enum to the UST session registry. Once done, this replies to the
6345 * application with the appropriate error code.
6346 *
6347 * The session UST registry lock is acquired within this function.
6348 *
6349 * On success 0 is returned else a negative value.
6350 */
6351static int add_enum_ust_registry(int sock, int sobjd, char *name,
6352 struct ustctl_enum_entry *entries, size_t nr_entries)
6353{
6354 int ret = 0, ret_code;
6355 struct ust_app *app;
6356 struct ust_app_session *ua_sess;
6357 struct ust_registry_session *registry;
6358 uint64_t enum_id = -1ULL;
6359
6360 rcu_read_lock();
6361
6362 /* Lookup application. If not found, there is a code flow error. */
6363 app = find_app_by_notify_sock(sock);
6364 if (!app) {
6365 /* Return an error since this is not an error */
6366 DBG("Application socket %d is being torn down. Aborting enum registration",
6367 sock);
6368 free(entries);
6369 goto error_rcu_unlock;
6370 }
6371
6372 /* Lookup session by UST object descriptor. */
6373 ua_sess = find_session_by_objd(app, sobjd);
6374 if (!ua_sess) {
6375 /* Return an error since this is not an error */
fad1ed2f 6376 DBG("Application session is being torn down (session not found). Aborting enum registration.");
10b56aef
MD
6377 free(entries);
6378 goto error_rcu_unlock;
6379 }
6380
6381 registry = get_session_registry(ua_sess);
fad1ed2f
JR
6382 if (!registry) {
6383 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6384 free(entries);
6385 goto error_rcu_unlock;
6386 }
10b56aef
MD
6387
6388 pthread_mutex_lock(&registry->lock);
6389
6390 /*
6391 * From this point on, the callee acquires the ownership of
6392 * entries. The variable entries MUST NOT be read/written after
6393 * call.
6394 */
6395 ret_code = ust_registry_create_or_find_enum(registry, sobjd, name,
6396 entries, nr_entries, &enum_id);
6397 entries = NULL;
6398
6399 /*
6400 * The return value is returned to ustctl so in case of an error, the
6401 * application can be notified. In case of an error, it's important not to
6402 * return a negative error or else the application will get closed.
6403 */
6404 ret = ustctl_reply_register_enum(sock, enum_id, ret_code);
6405 if (ret < 0) {
6406 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6407 ERR("UST app reply enum failed with ret %d", ret);
6408 } else {
6409 DBG3("UST app reply enum failed. Application died");
6410 }
6411 /*
6412 * No need to wipe the create enum since the application socket will
6413 * get close on error hence cleaning up everything by itself.
6414 */
6415 goto error;
6416 }
6417
6418 DBG3("UST registry enum %s added successfully or already found", name);
6419
6420error:
6421 pthread_mutex_unlock(&registry->lock);
6422error_rcu_unlock:
6423 rcu_read_unlock();
6424 return ret;
6425}
6426
d88aee68
DG
6427/*
6428 * Handle application notification through the given notify socket.
6429 *
6430 * Return 0 on success or else a negative value.
6431 */
d0b96690
DG
6432int ust_app_recv_notify(int sock)
6433{
6434 int ret;
6435 enum ustctl_notify_cmd cmd;
6436
6437 DBG3("UST app receiving notify from sock %d", sock);
6438
6439 ret = ustctl_recv_notify(sock, &cmd);
6440 if (ret < 0) {
6441 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6442 ERR("UST app recv notify failed with ret %d", ret);
6443 } else {
6444 DBG3("UST app recv notify failed. Application died");
6445 }
6446 goto error;
6447 }
6448
6449 switch (cmd) {
6450 case USTCTL_NOTIFY_CMD_EVENT:
6451 {
2106efa0 6452 int sobjd, cobjd, loglevel_value;
fc4b93fa 6453 char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri;
d0b96690
DG
6454 size_t nr_fields;
6455 struct ustctl_field *fields;
6456
6457 DBG2("UST app ustctl register event received");
6458
2106efa0
PP
6459 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name,
6460 &loglevel_value, &sig, &nr_fields, &fields,
6461 &model_emf_uri);
d0b96690
DG
6462 if (ret < 0) {
6463 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6464 ERR("UST app recv event failed with ret %d", ret);
6465 } else {
6466 DBG3("UST app recv event failed. Application died");
6467 }
6468 goto error;
6469 }
6470
d5d629b5
DG
6471 /*
6472 * Add event to the UST registry coming from the notify socket. This
6473 * call will free if needed the sig, fields and model_emf_uri. This
6474 * code path loses the ownsership of these variables and transfer them
6475 * to the this function.
6476 */
d0b96690 6477 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
2106efa0 6478 fields, loglevel_value, model_emf_uri);
d0b96690
DG
6479 if (ret < 0) {
6480 goto error;
6481 }
6482
6483 break;
6484 }
6485 case USTCTL_NOTIFY_CMD_CHANNEL:
6486 {
6487 int sobjd, cobjd;
6488 size_t nr_fields;
6489 struct ustctl_field *fields;
6490
6491 DBG2("UST app ustctl register channel received");
6492
6493 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
6494 &fields);
6495 if (ret < 0) {
6496 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6497 ERR("UST app recv channel failed with ret %d", ret);
6498 } else {
6499 DBG3("UST app recv channel failed. Application died");
6500 }
6501 goto error;
6502 }
6503
d5d629b5
DG
6504 /*
6505 * The fields ownership are transfered to this function call meaning
6506 * that if needed it will be freed. After this, it's invalid to access
6507 * fields or clean it up.
6508 */
8eede835 6509 ret = reply_ust_register_channel(sock, cobjd, nr_fields,
d0b96690
DG
6510 fields);
6511 if (ret < 0) {
6512 goto error;
6513 }
6514
6515 break;
6516 }
10b56aef
MD
6517 case USTCTL_NOTIFY_CMD_ENUM:
6518 {
6519 int sobjd;
fc4b93fa 6520 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
10b56aef
MD
6521 size_t nr_entries;
6522 struct ustctl_enum_entry *entries;
6523
6524 DBG2("UST app ustctl register enum received");
6525
6526 ret = ustctl_recv_register_enum(sock, &sobjd, name,
6527 &entries, &nr_entries);
6528 if (ret < 0) {
6529 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
6530 ERR("UST app recv enum failed with ret %d", ret);
6531 } else {
6532 DBG3("UST app recv enum failed. Application died");
6533 }
6534 goto error;
6535 }
6536
6537 /* Callee assumes ownership of entries */
6538 ret = add_enum_ust_registry(sock, sobjd, name,
6539 entries, nr_entries);
6540 if (ret < 0) {
6541 goto error;
6542 }
6543
6544 break;
6545 }
d0b96690
DG
6546 default:
6547 /* Should NEVER happen. */
6548 assert(0);
6549 }
6550
6551error:
6552 return ret;
6553}
d88aee68
DG
6554
6555/*
6556 * Once the notify socket hangs up, this is called. First, it tries to find the
6557 * corresponding application. On failure, the call_rcu to close the socket is
6558 * executed. If an application is found, it tries to delete it from the notify
6559 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6560 *
6561 * Note that an object needs to be allocated here so on ENOMEM failure, the
6562 * call RCU is not done but the rest of the cleanup is.
6563 */
6564void ust_app_notify_sock_unregister(int sock)
6565{
6566 int err_enomem = 0;
6567 struct lttng_ht_iter iter;
6568 struct ust_app *app;
6569 struct ust_app_notify_sock_obj *obj;
6570
6571 assert(sock >= 0);
6572
6573 rcu_read_lock();
6574
6575 obj = zmalloc(sizeof(*obj));
6576 if (!obj) {
6577 /*
6578 * An ENOMEM is kind of uncool. If this strikes we continue the
6579 * procedure but the call_rcu will not be called. In this case, we
6580 * accept the fd leak rather than possibly creating an unsynchronized
6581 * state between threads.
6582 *
6583 * TODO: The notify object should be created once the notify socket is
6584 * registered and stored independantely from the ust app object. The
6585 * tricky part is to synchronize the teardown of the application and
6586 * this notify object. Let's keep that in mind so we can avoid this
6587 * kind of shenanigans with ENOMEM in the teardown path.
6588 */
6589 err_enomem = 1;
6590 } else {
6591 obj->fd = sock;
6592 }
6593
6594 DBG("UST app notify socket unregister %d", sock);
6595
6596 /*
6597 * Lookup application by notify socket. If this fails, this means that the
6598 * hash table delete has already been done by the application
6599 * unregistration process so we can safely close the notify socket in a
6600 * call RCU.
6601 */
6602 app = find_app_by_notify_sock(sock);
6603 if (!app) {
6604 goto close_socket;
6605 }
6606
6607 iter.iter.node = &app->notify_sock_n.node;
6608
6609 /*
6610 * Whatever happens here either we fail or succeed, in both cases we have
6611 * to close the socket after a grace period to continue to the call RCU
6612 * here. If the deletion is successful, the application is not visible
6613 * anymore by other threads and is it fails it means that it was already
6614 * deleted from the hash table so either way we just have to close the
6615 * socket.
6616 */
6617 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
6618
6619close_socket:
6620 rcu_read_unlock();
6621
6622 /*
6623 * Close socket after a grace period to avoid for the socket to be reused
6624 * before the application object is freed creating potential race between
6625 * threads trying to add unique in the global hash table.
6626 */
6627 if (!err_enomem) {
6628 call_rcu(&obj->head, close_notify_sock_rcu);
6629 }
6630}
f45e313d
DG
6631
6632/*
6633 * Destroy a ust app data structure and free its memory.
6634 */
6635void ust_app_destroy(struct ust_app *app)
6636{
6637 if (!app) {
6638 return;
6639 }
6640
6641 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
6642}
6dc3064a
DG
6643
6644/*
6645 * Take a snapshot for a given UST session. The snapshot is sent to the given
6646 * output.
6647 *
9a654598 6648 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6dc3064a 6649 */
fb9a95c4
JG
6650enum lttng_error_code ust_app_snapshot_record(
6651 const struct ltt_ust_session *usess,
348a81dc 6652 const struct consumer_output *output, int wait,
d07ceecd 6653 uint64_t nb_packets_per_stream)
6dc3064a
DG
6654{
6655 int ret = 0;
9a654598 6656 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
6657 struct lttng_ht_iter iter;
6658 struct ust_app *app;
affce97e 6659 char *trace_path = NULL;
6dc3064a
DG
6660
6661 assert(usess);
6662 assert(output);
6663
6664 rcu_read_lock();
6665
8c924c7b
MD
6666 switch (usess->buffer_type) {
6667 case LTTNG_BUFFER_PER_UID:
6668 {
6669 struct buffer_reg_uid *reg;
6dc3064a 6670
8c924c7b 6671 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
3273699d 6672 struct buffer_reg_channel *buf_reg_chan;
8c924c7b 6673 struct consumer_socket *socket;
3b967712 6674 char pathname[PATH_MAX];
5da88b0f 6675 size_t consumer_path_offset = 0;
6dc3064a 6676
2b269489
JR
6677 if (!reg->registry->reg.ust->metadata_key) {
6678 /* Skip since no metadata is present */
6679 continue;
6680 }
6681
8c924c7b
MD
6682 /* Get consumer socket to use to push the metadata.*/
6683 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6684 usess->consumer);
6685 if (!socket) {
9a654598 6686 status = LTTNG_ERR_INVALID;
8c924c7b
MD
6687 goto error;
6688 }
6dc3064a 6689
8c924c7b
MD
6690 memset(pathname, 0, sizeof(pathname));
6691 ret = snprintf(pathname, sizeof(pathname),
5da88b0f 6692 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH,
8c924c7b
MD
6693 reg->uid, reg->bits_per_long);
6694 if (ret < 0) {
6695 PERROR("snprintf snapshot path");
9a654598 6696 status = LTTNG_ERR_INVALID;
8c924c7b
MD
6697 goto error;
6698 }
affce97e
JG
6699 /* Free path allowed on previous iteration. */
6700 free(trace_path);
5da88b0f
MD
6701 trace_path = setup_channel_trace_path(usess->consumer, pathname,
6702 &consumer_path_offset);
3b967712
MD
6703 if (!trace_path) {
6704 status = LTTNG_ERR_INVALID;
6705 goto error;
6706 }
f3db82be 6707 /* Add the UST default trace dir to path. */
8c924c7b 6708 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
3273699d 6709 buf_reg_chan, node.node) {
9a654598 6710 status = consumer_snapshot_channel(socket,
3273699d 6711 buf_reg_chan->consumer_key,
e098433c 6712 output, 0, usess->uid,
5da88b0f 6713 usess->gid, &trace_path[consumer_path_offset], wait,
d2956687 6714 nb_packets_per_stream);
9a654598 6715 if (status != LTTNG_OK) {
8c924c7b
MD
6716 goto error;
6717 }
6718 }
9a654598 6719 status = consumer_snapshot_channel(socket,
68808f4e 6720 reg->registry->reg.ust->metadata_key, output, 1,
5da88b0f
MD
6721 usess->uid, usess->gid, &trace_path[consumer_path_offset],
6722 wait, 0);
9a654598 6723 if (status != LTTNG_OK) {
8c924c7b
MD
6724 goto error;
6725 }
af706bb7 6726 }
8c924c7b
MD
6727 break;
6728 }
6729 case LTTNG_BUFFER_PER_PID:
6730 {
6731 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6732 struct consumer_socket *socket;
6733 struct lttng_ht_iter chan_iter;
6734 struct ust_app_channel *ua_chan;
6735 struct ust_app_session *ua_sess;
6736 struct ust_registry_session *registry;
3b967712 6737 char pathname[PATH_MAX];
5da88b0f 6738 size_t consumer_path_offset = 0;
8c924c7b
MD
6739
6740 ua_sess = lookup_session_by_app(usess, app);
6741 if (!ua_sess) {
6742 /* Session not associated with this app. */
6743 continue;
6744 }
af706bb7 6745
8c924c7b
MD
6746 /* Get the right consumer socket for the application. */
6747 socket = consumer_find_socket_by_bitness(app->bits_per_long,
348a81dc 6748 output);
8c924c7b 6749 if (!socket) {
9a654598 6750 status = LTTNG_ERR_INVALID;
5c786ded
JD
6751 goto error;
6752 }
6753
8c924c7b
MD
6754 /* Add the UST default trace dir to path. */
6755 memset(pathname, 0, sizeof(pathname));
5da88b0f 6756 ret = snprintf(pathname, sizeof(pathname), DEFAULT_UST_TRACE_DIR "/%s",
8c924c7b 6757 ua_sess->path);
6dc3064a 6758 if (ret < 0) {
9a654598 6759 status = LTTNG_ERR_INVALID;
8c924c7b 6760 PERROR("snprintf snapshot path");
6dc3064a
DG
6761 goto error;
6762 }
affce97e
JG
6763 /* Free path allowed on previous iteration. */
6764 free(trace_path);
5da88b0f
MD
6765 trace_path = setup_channel_trace_path(usess->consumer, pathname,
6766 &consumer_path_offset);
3b967712
MD
6767 if (!trace_path) {
6768 status = LTTNG_ERR_INVALID;
6769 goto error;
6770 }
f3db82be 6771 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
8c924c7b 6772 ua_chan, node.node) {
9a654598 6773 status = consumer_snapshot_channel(socket,
470cc211 6774 ua_chan->key, output, 0,
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,
d2956687 6778 nb_packets_per_stream);
9a654598
JG
6779 switch (status) {
6780 case LTTNG_OK:
6781 break;
6782 case LTTNG_ERR_CHAN_NOT_FOUND:
6783 continue;
6784 default:
8c924c7b
MD
6785 goto error;
6786 }
6787 }
6788
6789 registry = get_session_registry(ua_sess);
fad1ed2f 6790 if (!registry) {
9bbfb88c
MD
6791 DBG("Application session is being torn down. Skip application.");
6792 continue;
fad1ed2f 6793 }
9a654598 6794 status = consumer_snapshot_channel(socket,
470cc211 6795 registry->metadata_key, output, 1,
ff588497
JR
6796 lttng_credentials_get_uid(&ua_sess->effective_credentials),
6797 lttng_credentials_get_gid(&ua_sess->effective_credentials),
5da88b0f 6798 &trace_path[consumer_path_offset], wait, 0);
9a654598
JG
6799 switch (status) {
6800 case LTTNG_OK:
6801 break;
6802 case LTTNG_ERR_CHAN_NOT_FOUND:
6803 continue;
6804 default:
8c924c7b
MD
6805 goto error;
6806 }
6807 }
6808 break;
6809 }
6810 default:
6811 assert(0);
6812 break;
6dc3064a
DG
6813 }
6814
6815error:
affce97e 6816 free(trace_path);
6dc3064a 6817 rcu_read_unlock();
9a654598 6818 return status;
6dc3064a 6819}
5c786ded
JD
6820
6821/*
d07ceecd 6822 * Return the size taken by one more packet per stream.
5c786ded 6823 */
fb9a95c4
JG
6824uint64_t ust_app_get_size_one_more_packet_per_stream(
6825 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
5c786ded 6826{
d07ceecd 6827 uint64_t tot_size = 0;
5c786ded
JD
6828 struct ust_app *app;
6829 struct lttng_ht_iter iter;
6830
6831 assert(usess);
6832
6833 switch (usess->buffer_type) {
6834 case LTTNG_BUFFER_PER_UID:
6835 {
6836 struct buffer_reg_uid *reg;
6837
6838 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
3273699d 6839 struct buffer_reg_channel *buf_reg_chan;
5c786ded 6840
b7064eaa 6841 rcu_read_lock();
5c786ded 6842 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
3273699d
FD
6843 buf_reg_chan, node.node) {
6844 if (cur_nr_packets >= buf_reg_chan->num_subbuf) {
d07ceecd
MD
6845 /*
6846 * Don't take channel into account if we
6847 * already grab all its packets.
6848 */
6849 continue;
6850 }
3273699d 6851 tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count;
5c786ded 6852 }
b7064eaa 6853 rcu_read_unlock();
5c786ded
JD
6854 }
6855 break;
6856 }
6857 case LTTNG_BUFFER_PER_PID:
6858 {
6859 rcu_read_lock();
6860 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6861 struct ust_app_channel *ua_chan;
6862 struct ust_app_session *ua_sess;
6863 struct lttng_ht_iter chan_iter;
6864
6865 ua_sess = lookup_session_by_app(usess, app);
6866 if (!ua_sess) {
6867 /* Session not associated with this app. */
6868 continue;
6869 }
6870
6871 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
6872 ua_chan, node.node) {
d07ceecd
MD
6873 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
6874 /*
6875 * Don't take channel into account if we
6876 * already grab all its packets.
6877 */
6878 continue;
6879 }
6880 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
5c786ded
JD
6881 }
6882 }
6883 rcu_read_unlock();
6884 break;
6885 }
6886 default:
6887 assert(0);
6888 break;
6889 }
6890
d07ceecd 6891 return tot_size;
5c786ded 6892}
fb83fe64
JD
6893
6894int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
6895 struct cds_list_head *buffer_reg_uid_list,
6896 struct consumer_output *consumer, uint64_t uchan_id,
6897 int overwrite, uint64_t *discarded, uint64_t *lost)
6898{
6899 int ret;
6900 uint64_t consumer_chan_key;
6901
70dd8162
MD
6902 *discarded = 0;
6903 *lost = 0;
6904
fb83fe64 6905 ret = buffer_reg_uid_consumer_channel_key(
76604852 6906 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
fb83fe64 6907 if (ret < 0) {
70dd8162
MD
6908 /* Not found */
6909 ret = 0;
fb83fe64
JD
6910 goto end;
6911 }
6912
6913 if (overwrite) {
6914 ret = consumer_get_lost_packets(ust_session_id,
6915 consumer_chan_key, consumer, lost);
6916 } else {
6917 ret = consumer_get_discarded_events(ust_session_id,
6918 consumer_chan_key, consumer, discarded);
6919 }
6920
6921end:
6922 return ret;
6923}
6924
6925int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
6926 struct ltt_ust_channel *uchan,
6927 struct consumer_output *consumer, int overwrite,
6928 uint64_t *discarded, uint64_t *lost)
6929{
6930 int ret = 0;
6931 struct lttng_ht_iter iter;
6932 struct lttng_ht_node_str *ua_chan_node;
6933 struct ust_app *app;
6934 struct ust_app_session *ua_sess;
6935 struct ust_app_channel *ua_chan;
6936
70dd8162
MD
6937 *discarded = 0;
6938 *lost = 0;
6939
fb83fe64
JD
6940 rcu_read_lock();
6941 /*
70dd8162
MD
6942 * Iterate over every registered applications. Sum counters for
6943 * all applications containing requested session and channel.
fb83fe64
JD
6944 */
6945 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6946 struct lttng_ht_iter uiter;
6947
6948 ua_sess = lookup_session_by_app(usess, app);
6949 if (ua_sess == NULL) {
6950 continue;
6951 }
6952
6953 /* Get channel */
ee022399 6954 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
fb83fe64
JD
6955 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6956 /* If the session is found for the app, the channel must be there */
6957 assert(ua_chan_node);
6958
6959 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6960
6961 if (overwrite) {
70dd8162
MD
6962 uint64_t _lost;
6963
fb83fe64 6964 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
70dd8162
MD
6965 consumer, &_lost);
6966 if (ret < 0) {
6967 break;
6968 }
6969 (*lost) += _lost;
fb83fe64 6970 } else {
70dd8162
MD
6971 uint64_t _discarded;
6972
fb83fe64 6973 ret = consumer_get_discarded_events(usess->id,
70dd8162
MD
6974 ua_chan->key, consumer, &_discarded);
6975 if (ret < 0) {
6976 break;
6977 }
6978 (*discarded) += _discarded;
fb83fe64 6979 }
fb83fe64
JD
6980 }
6981
fb83fe64
JD
6982 rcu_read_unlock();
6983 return ret;
6984}
c2561365
JD
6985
6986static
6987int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
6988 struct ust_app *app)
6989{
6990 int ret = 0;
6991 struct ust_app_session *ua_sess;
6992
6993 DBG("Regenerating the metadata for ust app pid %d", app->pid);
6994
6995 rcu_read_lock();
6996
6997 ua_sess = lookup_session_by_app(usess, app);
6998 if (ua_sess == NULL) {
6999 /* The session is in teardown process. Ignore and continue. */
7000 goto end;
7001 }
7002
7003 pthread_mutex_lock(&ua_sess->lock);
7004
7005 if (ua_sess->deleted) {
7006 goto end_unlock;
7007 }
7008
7009 pthread_mutex_lock(&app->sock_lock);
7010 ret = ustctl_regenerate_statedump(app->sock, ua_sess->handle);
7011 pthread_mutex_unlock(&app->sock_lock);
7012
7013end_unlock:
7014 pthread_mutex_unlock(&ua_sess->lock);
7015
7016end:
7017 rcu_read_unlock();
7018 health_code_update();
7019 return ret;
7020}
7021
7022/*
7023 * Regenerate the statedump for each app in the session.
7024 */
7025int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
7026{
7027 int ret = 0;
7028 struct lttng_ht_iter iter;
7029 struct ust_app *app;
7030
7031 DBG("Regenerating the metadata for all UST apps");
7032
7033 rcu_read_lock();
7034
7035 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7036 if (!app->compatible) {
7037 continue;
7038 }
7039
7040 ret = ust_app_regenerate_statedump(usess, app);
7041 if (ret < 0) {
7042 /* Continue to the next app even on error */
7043 continue;
7044 }
7045 }
7046
7047 rcu_read_unlock();
7048
7049 return 0;
7050}
5c408ad8
JD
7051
7052/*
7053 * Rotate all the channels of a session.
7054 *
6f6d3b69 7055 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 7056 */
6f6d3b69 7057enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
5c408ad8 7058{
6f6d3b69
MD
7059 int ret;
7060 enum lttng_error_code cmd_ret = LTTNG_OK;
5c408ad8
JD
7061 struct lttng_ht_iter iter;
7062 struct ust_app *app;
7063 struct ltt_ust_session *usess = session->ust_session;
5c408ad8
JD
7064
7065 assert(usess);
7066
7067 rcu_read_lock();
7068
7069 switch (usess->buffer_type) {
7070 case LTTNG_BUFFER_PER_UID:
7071 {
7072 struct buffer_reg_uid *reg;
7073
7074 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
3273699d 7075 struct buffer_reg_channel *buf_reg_chan;
5c408ad8
JD
7076 struct consumer_socket *socket;
7077
14d3fca9
JR
7078 if (!reg->registry->reg.ust->metadata_key) {
7079 /* Skip since no metadata is present */
7080 continue;
7081 }
7082
5c408ad8
JD
7083 /* Get consumer socket to use to push the metadata.*/
7084 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7085 usess->consumer);
7086 if (!socket) {
6f6d3b69 7087 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
7088 goto error;
7089 }
7090
5c408ad8
JD
7091 /* Rotate the data channels. */
7092 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
3273699d 7093 buf_reg_chan, node.node) {
5c408ad8 7094 ret = consumer_rotate_channel(socket,
3273699d 7095 buf_reg_chan->consumer_key,
5c408ad8 7096 usess->uid, usess->gid,
d2956687
JG
7097 usess->consumer,
7098 /* is_metadata_channel */ false);
5c408ad8 7099 if (ret < 0) {
6f6d3b69 7100 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
7101 goto error;
7102 }
7103 }
7104
7105 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
7106
7107 ret = consumer_rotate_channel(socket,
7108 reg->registry->reg.ust->metadata_key,
7109 usess->uid, usess->gid,
d2956687
JG
7110 usess->consumer,
7111 /* is_metadata_channel */ true);
5c408ad8 7112 if (ret < 0) {
6f6d3b69 7113 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
7114 goto error;
7115 }
5c408ad8
JD
7116 }
7117 break;
7118 }
7119 case LTTNG_BUFFER_PER_PID:
7120 {
7121 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7122 struct consumer_socket *socket;
7123 struct lttng_ht_iter chan_iter;
7124 struct ust_app_channel *ua_chan;
7125 struct ust_app_session *ua_sess;
7126 struct ust_registry_session *registry;
7127
7128 ua_sess = lookup_session_by_app(usess, app);
7129 if (!ua_sess) {
7130 /* Session not associated with this app. */
7131 continue;
7132 }
5c408ad8
JD
7133
7134 /* Get the right consumer socket for the application. */
7135 socket = consumer_find_socket_by_bitness(app->bits_per_long,
7136 usess->consumer);
7137 if (!socket) {
6f6d3b69 7138 cmd_ret = LTTNG_ERR_INVALID;
5c408ad8
JD
7139 goto error;
7140 }
7141
7142 registry = get_session_registry(ua_sess);
7143 if (!registry) {
6f6d3b69
MD
7144 DBG("Application session is being torn down. Skip application.");
7145 continue;
5c408ad8
JD
7146 }
7147
5c408ad8
JD
7148 /* Rotate the data channels. */
7149 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7150 ua_chan, node.node) {
470cc211
JG
7151 ret = consumer_rotate_channel(socket,
7152 ua_chan->key,
ff588497
JR
7153 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7154 lttng_credentials_get_gid(&ua_sess->effective_credentials),
d2956687
JG
7155 ua_sess->consumer,
7156 /* is_metadata_channel */ false);
5c408ad8 7157 if (ret < 0) {
6f6d3b69
MD
7158 /* Per-PID buffer and application going away. */
7159 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7160 continue;
7161 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
7162 goto error;
7163 }
7164 }
7165
7166 /* Rotate the metadata channel. */
7167 (void) push_metadata(registry, usess->consumer);
470cc211
JG
7168 ret = consumer_rotate_channel(socket,
7169 registry->metadata_key,
ff588497
JR
7170 lttng_credentials_get_uid(&ua_sess->effective_credentials),
7171 lttng_credentials_get_gid(&ua_sess->effective_credentials),
d2956687
JG
7172 ua_sess->consumer,
7173 /* is_metadata_channel */ true);
5c408ad8 7174 if (ret < 0) {
6f6d3b69
MD
7175 /* Per-PID buffer and application going away. */
7176 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7177 continue;
7178 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
7179 goto error;
7180 }
5c408ad8
JD
7181 }
7182 break;
7183 }
7184 default:
7185 assert(0);
7186 break;
7187 }
7188
6f6d3b69 7189 cmd_ret = LTTNG_OK;
5c408ad8
JD
7190
7191error:
7192 rcu_read_unlock();
6f6d3b69 7193 return cmd_ret;
5c408ad8 7194}
d2956687
JG
7195
7196enum lttng_error_code ust_app_create_channel_subdirectories(
7197 const struct ltt_ust_session *usess)
7198{
7199 enum lttng_error_code ret = LTTNG_OK;
7200 struct lttng_ht_iter iter;
7201 enum lttng_trace_chunk_status chunk_status;
7202 char *pathname_index;
7203 int fmt_ret;
7204
7205 assert(usess->current_trace_chunk);
7206 rcu_read_lock();
7207
7208 switch (usess->buffer_type) {
7209 case LTTNG_BUFFER_PER_UID:
7210 {
7211 struct buffer_reg_uid *reg;
7212
7213 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7214 fmt_ret = asprintf(&pathname_index,
5da88b0f 7215 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
d2956687
JG
7216 reg->uid, reg->bits_per_long);
7217 if (fmt_ret < 0) {
7218 ERR("Failed to format channel index directory");
7219 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7220 goto error;
7221 }
7222
7223 /*
7224 * Create the index subdirectory which will take care
7225 * of implicitly creating the channel's path.
7226 */
7227 chunk_status = lttng_trace_chunk_create_subdirectory(
7228 usess->current_trace_chunk,
7229 pathname_index);
7230 free(pathname_index);
7231 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7232 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7233 goto error;
7234 }
7235 }
7236 break;
7237 }
7238 case LTTNG_BUFFER_PER_PID:
7239 {
7240 struct ust_app *app;
7241
495dece5
MD
7242 /*
7243 * Create the toplevel ust/ directory in case no apps are running.
7244 */
7245 chunk_status = lttng_trace_chunk_create_subdirectory(
7246 usess->current_trace_chunk,
7247 DEFAULT_UST_TRACE_DIR);
7248 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7249 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7250 goto error;
7251 }
7252
d2956687
JG
7253 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
7254 pid_n.node) {
7255 struct ust_app_session *ua_sess;
7256 struct ust_registry_session *registry;
7257
7258 ua_sess = lookup_session_by_app(usess, app);
7259 if (!ua_sess) {
7260 /* Session not associated with this app. */
7261 continue;
7262 }
7263
7264 registry = get_session_registry(ua_sess);
7265 if (!registry) {
7266 DBG("Application session is being torn down. Skip application.");
7267 continue;
7268 }
7269
7270 fmt_ret = asprintf(&pathname_index,
5da88b0f 7271 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
d2956687
JG
7272 ua_sess->path);
7273 if (fmt_ret < 0) {
7274 ERR("Failed to format channel index directory");
7275 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7276 goto error;
7277 }
7278 /*
7279 * Create the index subdirectory which will take care
7280 * of implicitly creating the channel's path.
7281 */
7282 chunk_status = lttng_trace_chunk_create_subdirectory(
7283 usess->current_trace_chunk,
7284 pathname_index);
7285 free(pathname_index);
7286 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7287 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7288 goto error;
7289 }
7290 }
7291 break;
7292 }
7293 default:
7294 abort();
7295 }
7296
7297 ret = LTTNG_OK;
7298error:
7299 rcu_read_unlock();
7300 return ret;
7301}
4a9b9759
MD
7302
7303/*
7304 * Clear all the channels of a session.
7305 *
7306 * Return LTTNG_OK on success or else an LTTng error code.
7307 */
7308enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
7309{
7310 int ret;
7311 enum lttng_error_code cmd_ret = LTTNG_OK;
7312 struct lttng_ht_iter iter;
7313 struct ust_app *app;
7314 struct ltt_ust_session *usess = session->ust_session;
7315
7316 assert(usess);
7317
7318 rcu_read_lock();
7319
7320 if (usess->active) {
7321 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
7322 cmd_ret = LTTNG_ERR_FATAL;
7323 goto end;
7324 }
7325
7326 switch (usess->buffer_type) {
7327 case LTTNG_BUFFER_PER_UID:
7328 {
7329 struct buffer_reg_uid *reg;
7330
7331 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
3273699d 7332 struct buffer_reg_channel *buf_reg_chan;
4a9b9759
MD
7333 struct consumer_socket *socket;
7334
7335 /* Get consumer socket to use to push the metadata.*/
7336 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7337 usess->consumer);
7338 if (!socket) {
7339 cmd_ret = LTTNG_ERR_INVALID;
7340 goto error_socket;
7341 }
7342
7343 /* Clear the data channels. */
7344 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
3273699d 7345 buf_reg_chan, node.node) {
4a9b9759 7346 ret = consumer_clear_channel(socket,
3273699d 7347 buf_reg_chan->consumer_key);
4a9b9759
MD
7348 if (ret < 0) {
7349 goto error;
7350 }
7351 }
7352
7353 (void) push_metadata(reg->registry->reg.ust, usess->consumer);
7354
7355 /*
7356 * Clear the metadata channel.
7357 * Metadata channel is not cleared per se but we still need to
7358 * perform a rotation operation on it behind the scene.
7359 */
7360 ret = consumer_clear_channel(socket,
7361 reg->registry->reg.ust->metadata_key);
7362 if (ret < 0) {
7363 goto error;
7364 }
7365 }
7366 break;
7367 }
7368 case LTTNG_BUFFER_PER_PID:
7369 {
7370 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7371 struct consumer_socket *socket;
7372 struct lttng_ht_iter chan_iter;
7373 struct ust_app_channel *ua_chan;
7374 struct ust_app_session *ua_sess;
7375 struct ust_registry_session *registry;
7376
7377 ua_sess = lookup_session_by_app(usess, app);
7378 if (!ua_sess) {
7379 /* Session not associated with this app. */
7380 continue;
7381 }
7382
7383 /* Get the right consumer socket for the application. */
7384 socket = consumer_find_socket_by_bitness(app->bits_per_long,
7385 usess->consumer);
7386 if (!socket) {
7387 cmd_ret = LTTNG_ERR_INVALID;
7388 goto error_socket;
7389 }
7390
7391 registry = get_session_registry(ua_sess);
7392 if (!registry) {
7393 DBG("Application session is being torn down. Skip application.");
7394 continue;
7395 }
7396
7397 /* Clear the data channels. */
7398 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7399 ua_chan, node.node) {
7400 ret = consumer_clear_channel(socket, ua_chan->key);
7401 if (ret < 0) {
7402 /* Per-PID buffer and application going away. */
7403 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7404 continue;
7405 }
7406 goto error;
7407 }
7408 }
7409
7410 (void) push_metadata(registry, usess->consumer);
7411
7412 /*
7413 * Clear the metadata channel.
7414 * Metadata channel is not cleared per se but we still need to
7415 * perform rotation operation on it behind the scene.
7416 */
7417 ret = consumer_clear_channel(socket, registry->metadata_key);
7418 if (ret < 0) {
7419 /* Per-PID buffer and application going away. */
7420 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7421 continue;
7422 }
7423 goto error;
7424 }
7425 }
7426 break;
7427 }
7428 default:
7429 assert(0);
7430 break;
7431 }
7432
7433 cmd_ret = LTTNG_OK;
7434 goto end;
7435
7436error:
7437 switch (-ret) {
7438 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
7439 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
7440 break;
7441 default:
7442 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
7443 }
7444
7445error_socket:
7446end:
7447 rcu_read_unlock();
7448 return cmd_ret;
7449}
04ed9e10
JG
7450
7451/*
7452 * This function skips the metadata channel as the begin/end timestamps of a
7453 * metadata packet are useless.
7454 *
7455 * Moreover, opening a packet after a "clear" will cause problems for live
7456 * sessions as it will introduce padding that was not part of the first trace
7457 * chunk. The relay daemon expects the content of the metadata stream of
7458 * successive metadata trace chunks to be strict supersets of one another.
7459 *
7460 * For example, flushing a packet at the beginning of the metadata stream of
7461 * a trace chunk resulting from a "clear" session command will cause the
7462 * size of the metadata stream of the new trace chunk to not match the size of
7463 * the metadata stream of the original chunk. This will confuse the relay
7464 * daemon as the same "offset" in a metadata stream will no longer point
7465 * to the same content.
7466 */
7467enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
7468{
7469 enum lttng_error_code ret = LTTNG_OK;
7470 struct lttng_ht_iter iter;
7471 struct ltt_ust_session *usess = session->ust_session;
7472
7473 assert(usess);
7474
7475 rcu_read_lock();
7476
7477 switch (usess->buffer_type) {
7478 case LTTNG_BUFFER_PER_UID:
7479 {
7480 struct buffer_reg_uid *reg;
7481
7482 cds_list_for_each_entry (
7483 reg, &usess->buffer_reg_uid_list, lnode) {
3273699d 7484 struct buffer_reg_channel *buf_reg_chan;
04ed9e10
JG
7485 struct consumer_socket *socket;
7486
7487 socket = consumer_find_socket_by_bitness(
7488 reg->bits_per_long, usess->consumer);
7489 if (!socket) {
7490 ret = LTTNG_ERR_FATAL;
7491 goto error;
7492 }
7493
7494 cds_lfht_for_each_entry(reg->registry->channels->ht,
3273699d 7495 &iter.iter, buf_reg_chan, node.node) {
04ed9e10
JG
7496 const int open_ret =
7497 consumer_open_channel_packets(
7498 socket,
3273699d 7499 buf_reg_chan->consumer_key);
04ed9e10
JG
7500
7501 if (open_ret < 0) {
7502 ret = LTTNG_ERR_UNK;
7503 goto error;
7504 }
7505 }
7506 }
7507 break;
7508 }
7509 case LTTNG_BUFFER_PER_PID:
7510 {
7511 struct ust_app *app;
7512
7513 cds_lfht_for_each_entry (
7514 ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7515 struct consumer_socket *socket;
7516 struct lttng_ht_iter chan_iter;
7517 struct ust_app_channel *ua_chan;
7518 struct ust_app_session *ua_sess;
7519 struct ust_registry_session *registry;
7520
7521 ua_sess = lookup_session_by_app(usess, app);
7522 if (!ua_sess) {
7523 /* Session not associated with this app. */
7524 continue;
7525 }
7526
7527 /* Get the right consumer socket for the application. */
7528 socket = consumer_find_socket_by_bitness(
7529 app->bits_per_long, usess->consumer);
7530 if (!socket) {
7531 ret = LTTNG_ERR_FATAL;
7532 goto error;
7533 }
7534
7535 registry = get_session_registry(ua_sess);
7536 if (!registry) {
7537 DBG("Application session is being torn down. Skip application.");
7538 continue;
7539 }
7540
7541 cds_lfht_for_each_entry(ua_sess->channels->ht,
7542 &chan_iter.iter, ua_chan, node.node) {
7543 const int open_ret =
7544 consumer_open_channel_packets(
7545 socket,
7546 ua_chan->key);
7547
7548 if (open_ret < 0) {
7549 /*
7550 * Per-PID buffer and application going
7551 * away.
7552 */
97a171e1 7553 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
04ed9e10
JG
7554 continue;
7555 }
7556
7557 ret = LTTNG_ERR_UNK;
7558 goto error;
7559 }
7560 }
7561 }
7562 break;
7563 }
7564 default:
7565 abort();
7566 break;
7567 }
7568
7569error:
7570 rcu_read_unlock();
7571 return ret;
7572}
This page took 0.607839 seconds and 4 git commands to generate.