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