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