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