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