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