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