Cleanup: rotation-thread: enforce conding standard following fix
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10
11 #include "buffer-registry.hpp"
12 #include "condition-internal.hpp"
13 #include "event-notifier-error-accounting.hpp"
14 #include "event.hpp"
15 #include "fd-limit.hpp"
16 #include "field.hpp"
17 #include "health-sessiond.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "lttng-ust-ctl.hpp"
20 #include "lttng-ust-error.hpp"
21 #include "notification-thread-commands.hpp"
22 #include "session.hpp"
23 #include "ust-app.hpp"
24 #include "ust-consumer.hpp"
25 #include "ust-field-convert.hpp"
26 #include "utils.hpp"
27
28 #include <common/bytecode/bytecode.hpp>
29 #include <common/common.hpp>
30 #include <common/compat/errno.hpp>
31 #include <common/exception.hpp>
32 #include <common/format.hpp>
33 #include <common/hashtable/utils.hpp>
34 #include <common/make-unique.hpp>
35 #include <common/sessiond-comm/sessiond-comm.hpp>
36 #include <common/urcu.hpp>
37
38 #include <lttng/condition/condition.h>
39 #include <lttng/condition/event-rule-matches-internal.hpp>
40 #include <lttng/condition/event-rule-matches.h>
41 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include <lttng/event-rule/event-rule.h>
43 #include <lttng/event-rule/user-tracepoint.h>
44 #include <lttng/trigger/trigger-internal.hpp>
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <inttypes.h>
49 #include <pthread.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/mman.h>
55 #include <sys/stat.h>
56 #include <sys/types.h>
57 #include <unistd.h>
58 #include <urcu/compiler.h>
59 #include <vector>
60
61 namespace lsu = lttng::sessiond::ust;
62 namespace lst = lttng::sessiond::trace;
63
64 struct lttng_ht *ust_app_ht;
65 struct lttng_ht *ust_app_ht_by_sock;
66 struct lttng_ht *ust_app_ht_by_notify_sock;
67
68 static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
69
70 /* Next available channel key. Access under next_channel_key_lock. */
71 static uint64_t _next_channel_key;
72 static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
73
74 /* Next available session ID. Access under next_session_id_lock. */
75 static uint64_t _next_session_id;
76 static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
77
78 namespace {
79
80 /*
81 * Return the session registry according to the buffer type of the given
82 * session.
83 *
84 * A registry per UID object MUST exists before calling this function or else
85 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
86 */
87 static lsu::registry_session *get_session_registry(const struct ust_app_session *ua_sess)
88 {
89 lsu::registry_session *registry = nullptr;
90
91 LTTNG_ASSERT(ua_sess);
92
93 switch (ua_sess->buffer_type) {
94 case LTTNG_BUFFER_PER_PID:
95 {
96 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
97 if (!reg_pid) {
98 goto error;
99 }
100 registry = reg_pid->registry->reg.ust;
101 break;
102 }
103 case LTTNG_BUFFER_PER_UID:
104 {
105 struct buffer_reg_uid *reg_uid =
106 buffer_reg_uid_find(ua_sess->tracing_id,
107 ua_sess->bits_per_long,
108 lttng_credentials_get_uid(&ua_sess->real_credentials));
109 if (!reg_uid) {
110 goto error;
111 }
112 registry = reg_uid->registry->reg.ust;
113 break;
114 }
115 default:
116 abort();
117 };
118
119 error:
120 return registry;
121 }
122
123 lsu::registry_session::locked_ptr get_locked_session_registry(const struct ust_app_session *ua_sess)
124 {
125 auto session = get_session_registry(ua_sess);
126 if (session) {
127 pthread_mutex_lock(&session->_lock);
128 }
129
130 return lsu::registry_session::locked_ptr{ session };
131 }
132 } /* namespace */
133
134 /*
135 * Return the incremented value of next_channel_key.
136 */
137 static uint64_t get_next_channel_key()
138 {
139 uint64_t ret;
140
141 pthread_mutex_lock(&next_channel_key_lock);
142 ret = ++_next_channel_key;
143 pthread_mutex_unlock(&next_channel_key_lock);
144 return ret;
145 }
146
147 /*
148 * Return the atomically incremented value of next_session_id.
149 */
150 static uint64_t get_next_session_id()
151 {
152 uint64_t ret;
153
154 pthread_mutex_lock(&next_session_id_lock);
155 ret = ++_next_session_id;
156 pthread_mutex_unlock(&next_session_id_lock);
157 return ret;
158 }
159
160 static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr *attr,
161 struct lttng_ust_abi_channel_attr *uattr)
162 {
163 /* Copy event attributes since the layout is different. */
164 attr->subbuf_size = uattr->subbuf_size;
165 attr->num_subbuf = uattr->num_subbuf;
166 attr->overwrite = uattr->overwrite;
167 attr->switch_timer_interval = uattr->switch_timer_interval;
168 attr->read_timer_interval = uattr->read_timer_interval;
169 attr->output = (lttng_ust_abi_output) uattr->output;
170 attr->blocking_timeout = uattr->u.s.blocking_timeout;
171 }
172
173 /*
174 * Match function for the hash table lookup.
175 *
176 * It matches an ust app event based on three attributes which are the event
177 * name, the filter bytecode and the loglevel.
178 */
179 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
180 {
181 struct ust_app_event *event;
182 const struct ust_app_ht_key *key;
183 int ev_loglevel_value;
184
185 LTTNG_ASSERT(node);
186 LTTNG_ASSERT(_key);
187
188 event = caa_container_of(node, struct ust_app_event, node.node);
189 key = (ust_app_ht_key *) _key;
190 ev_loglevel_value = event->attr.loglevel;
191
192 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
193
194 /* Event name */
195 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
196 goto no_match;
197 }
198
199 /* Event loglevel. */
200 if (ev_loglevel_value != key->loglevel_type) {
201 if (event->attr.loglevel_type == LTTNG_UST_ABI_LOGLEVEL_ALL &&
202 key->loglevel_type == 0 && ev_loglevel_value == -1) {
203 /*
204 * Match is accepted. This is because on event creation, the
205 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
206 * -1 are accepted for this loglevel type since 0 is the one set by
207 * the API when receiving an enable event.
208 */
209 } else {
210 goto no_match;
211 }
212 }
213
214 /* One of the filters is NULL, fail. */
215 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
216 goto no_match;
217 }
218
219 if (key->filter && event->filter) {
220 /* Both filters exists, check length followed by the bytecode. */
221 if (event->filter->len != key->filter->len ||
222 memcmp(event->filter->data, key->filter->data, event->filter->len) != 0) {
223 goto no_match;
224 }
225 }
226
227 /* One of the exclusions is NULL, fail. */
228 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
229 goto no_match;
230 }
231
232 if (key->exclusion && event->exclusion) {
233 /* Both exclusions exists, check count followed by the names. */
234 if (event->exclusion->count != key->exclusion->count ||
235 memcmp(event->exclusion->names,
236 key->exclusion->names,
237 event->exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) != 0) {
238 goto no_match;
239 }
240 }
241
242 /* Match. */
243 return 1;
244
245 no_match:
246 return 0;
247 }
248
249 /*
250 * Unique add of an ust app event in the given ht. This uses the custom
251 * ht_match_ust_app_event match function and the event name as hash.
252 */
253 static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, struct ust_app_event *event)
254 {
255 struct cds_lfht_node *node_ptr;
256 struct ust_app_ht_key key;
257 struct lttng_ht *ht;
258
259 LTTNG_ASSERT(ua_chan);
260 LTTNG_ASSERT(ua_chan->events);
261 LTTNG_ASSERT(event);
262
263 ht = ua_chan->events;
264 key.name = event->attr.name;
265 key.filter = event->filter;
266 key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel;
267 key.exclusion = event->exclusion;
268
269 node_ptr = cds_lfht_add_unique(ht->ht,
270 ht->hash_fct(event->node.key, lttng_ht_seed),
271 ht_match_ust_app_event,
272 &key,
273 &event->node.node);
274 LTTNG_ASSERT(node_ptr == &event->node.node);
275 }
276
277 /*
278 * Close the notify socket from the given RCU head object. This MUST be called
279 * through a call_rcu().
280 */
281 static void close_notify_sock_rcu(struct rcu_head *head)
282 {
283 int ret;
284 struct ust_app_notify_sock_obj *obj =
285 lttng::utils::container_of(head, &ust_app_notify_sock_obj::head);
286
287 /* Must have a valid fd here. */
288 LTTNG_ASSERT(obj->fd >= 0);
289
290 ret = close(obj->fd);
291 if (ret) {
292 ERR("close notify sock %d RCU", obj->fd);
293 }
294 lttng_fd_put(LTTNG_FD_APPS, 1);
295
296 free(obj);
297 }
298
299 /*
300 * Delete ust context safely. RCU read lock must be held before calling
301 * this function.
302 */
303 static void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, struct ust_app *app)
304 {
305 int ret;
306
307 LTTNG_ASSERT(ua_ctx);
308 ASSERT_RCU_READ_LOCKED();
309
310 if (ua_ctx->obj) {
311 pthread_mutex_lock(&app->sock_lock);
312 ret = lttng_ust_ctl_release_object(sock, ua_ctx->obj);
313 pthread_mutex_unlock(&app->sock_lock);
314 if (ret < 0) {
315 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
316 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
317 app->pid,
318 app->sock);
319 } else if (ret == -EAGAIN) {
320 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
321 app->pid,
322 app->sock);
323 } else {
324 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
325 ua_ctx->obj->handle,
326 ret,
327 app->pid,
328 app->sock);
329 }
330 }
331 free(ua_ctx->obj);
332 }
333 free(ua_ctx);
334 }
335
336 /*
337 * Delete ust app event safely. RCU read lock must be held before calling
338 * this function.
339 */
340 static void delete_ust_app_event(int sock, struct ust_app_event *ua_event, struct ust_app *app)
341 {
342 int ret;
343
344 LTTNG_ASSERT(ua_event);
345 ASSERT_RCU_READ_LOCKED();
346
347 free(ua_event->filter);
348 if (ua_event->exclusion != nullptr)
349 free(ua_event->exclusion);
350 if (ua_event->obj != nullptr) {
351 pthread_mutex_lock(&app->sock_lock);
352 ret = lttng_ust_ctl_release_object(sock, ua_event->obj);
353 pthread_mutex_unlock(&app->sock_lock);
354 if (ret < 0) {
355 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
356 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
357 app->pid,
358 app->sock);
359 } else if (ret == -EAGAIN) {
360 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
361 app->pid,
362 app->sock);
363 } else {
364 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
365 ret,
366 app->pid,
367 app->sock);
368 }
369 }
370 free(ua_event->obj);
371 }
372 free(ua_event);
373 }
374
375 /*
376 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
377 * through a call_rcu().
378 */
379 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head)
380 {
381 struct ust_app_event_notifier_rule *obj =
382 lttng::utils::container_of(head, &ust_app_event_notifier_rule::rcu_head);
383
384 free(obj);
385 }
386
387 /*
388 * Delete ust app event notifier rule safely.
389 */
390 static void delete_ust_app_event_notifier_rule(
391 int sock, struct ust_app_event_notifier_rule *ua_event_notifier_rule, struct ust_app *app)
392 {
393 int ret;
394
395 LTTNG_ASSERT(ua_event_notifier_rule);
396
397 if (ua_event_notifier_rule->exclusion != nullptr) {
398 free(ua_event_notifier_rule->exclusion);
399 }
400
401 if (ua_event_notifier_rule->obj != nullptr) {
402 pthread_mutex_lock(&app->sock_lock);
403 ret = lttng_ust_ctl_release_object(sock, ua_event_notifier_rule->obj);
404 pthread_mutex_unlock(&app->sock_lock);
405 if (ret < 0) {
406 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
407 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
408 app->pid,
409 app->sock);
410 } else if (ret == -EAGAIN) {
411 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
412 app->pid,
413 app->sock);
414 } else {
415 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
416 ret,
417 app->pid,
418 app->sock);
419 }
420 }
421
422 free(ua_event_notifier_rule->obj);
423 }
424
425 lttng_trigger_put(ua_event_notifier_rule->trigger);
426 call_rcu(&ua_event_notifier_rule->rcu_head, free_ust_app_event_notifier_rule_rcu);
427 }
428
429 /*
430 * Release ust data object of the given stream.
431 *
432 * Return 0 on success or else a negative value.
433 */
434 static int release_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app)
435 {
436 int ret = 0;
437
438 LTTNG_ASSERT(stream);
439
440 if (stream->obj) {
441 pthread_mutex_lock(&app->sock_lock);
442 ret = lttng_ust_ctl_release_object(sock, stream->obj);
443 pthread_mutex_unlock(&app->sock_lock);
444 if (ret < 0) {
445 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
446 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
447 app->pid,
448 app->sock);
449 } else if (ret == -EAGAIN) {
450 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
451 app->pid,
452 app->sock);
453 } else {
454 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
455 ret,
456 app->pid,
457 app->sock);
458 }
459 }
460 lttng_fd_put(LTTNG_FD_APPS, 2);
461 free(stream->obj);
462 }
463
464 return ret;
465 }
466
467 /*
468 * Delete ust app stream safely. RCU read lock must be held before calling
469 * this function.
470 */
471 static void delete_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app)
472 {
473 LTTNG_ASSERT(stream);
474 ASSERT_RCU_READ_LOCKED();
475
476 (void) release_ust_app_stream(sock, stream, app);
477 free(stream);
478 }
479
480 static void delete_ust_app_channel_rcu(struct rcu_head *head)
481 {
482 struct ust_app_channel *ua_chan =
483 lttng::utils::container_of(head, &ust_app_channel::rcu_head);
484
485 lttng_ht_destroy(ua_chan->ctx);
486 lttng_ht_destroy(ua_chan->events);
487 free(ua_chan);
488 }
489
490 /*
491 * Extract the lost packet or discarded events counter when the channel is
492 * being deleted and store the value in the parent channel so we can
493 * access it from lttng list and at stop/destroy.
494 *
495 * The session list lock must be held by the caller.
496 */
497 static void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
498 {
499 uint64_t discarded = 0, lost = 0;
500 struct ltt_session *session;
501 struct ltt_ust_channel *uchan;
502
503 if (ua_chan->attr.type != LTTNG_UST_ABI_CHAN_PER_CPU) {
504 return;
505 }
506
507 lttng::urcu::read_lock_guard read_lock;
508 session = session_find_by_id(ua_chan->session->tracing_id);
509 if (!session || !session->ust_session) {
510 /*
511 * Not finding the session is not an error because there are
512 * multiple ways the channels can be torn down.
513 *
514 * 1) The session daemon can initiate the destruction of the
515 * ust app session after receiving a destroy command or
516 * during its shutdown/teardown.
517 * 2) The application, since we are in per-pid tracing, is
518 * unregistering and tearing down its ust app session.
519 *
520 * Both paths are protected by the session list lock which
521 * ensures that the accounting of lost packets and discarded
522 * events is done exactly once. The session is then unpublished
523 * from the session list, resulting in this condition.
524 */
525 goto end;
526 }
527
528 if (ua_chan->attr.overwrite) {
529 consumer_get_lost_packets(ua_chan->session->tracing_id,
530 ua_chan->key,
531 session->ust_session->consumer,
532 &lost);
533 } else {
534 consumer_get_discarded_events(ua_chan->session->tracing_id,
535 ua_chan->key,
536 session->ust_session->consumer,
537 &discarded);
538 }
539 uchan = trace_ust_find_channel_by_name(session->ust_session->domain_global.channels,
540 ua_chan->name);
541 if (!uchan) {
542 ERR("Missing UST channel to store discarded counters");
543 goto end;
544 }
545
546 uchan->per_pid_closed_app_discarded += discarded;
547 uchan->per_pid_closed_app_lost += lost;
548
549 end:
550 if (session) {
551 session_put(session);
552 }
553 }
554
555 /*
556 * Delete ust app channel safely. RCU read lock must be held before calling
557 * this function.
558 *
559 * The session list lock must be held by the caller.
560 */
561 static void delete_ust_app_channel(int sock,
562 struct ust_app_channel *ua_chan,
563 struct ust_app *app,
564 const lsu::registry_session::locked_ptr& locked_registry)
565 {
566 int ret;
567 struct lttng_ht_iter iter;
568 struct ust_app_event *ua_event;
569 struct ust_app_ctx *ua_ctx;
570 struct ust_app_stream *stream, *stmp;
571
572 LTTNG_ASSERT(ua_chan);
573 ASSERT_RCU_READ_LOCKED();
574
575 DBG3("UST app deleting channel %s", ua_chan->name);
576
577 /* Wipe stream */
578 cds_list_for_each_entry_safe (stream, stmp, &ua_chan->streams.head, list) {
579 cds_list_del(&stream->list);
580 delete_ust_app_stream(sock, stream, app);
581 }
582
583 /* Wipe context */
584 cds_lfht_for_each_entry (ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
585 cds_list_del(&ua_ctx->list);
586 ret = lttng_ht_del(ua_chan->ctx, &iter);
587 LTTNG_ASSERT(!ret);
588 delete_ust_app_ctx(sock, ua_ctx, app);
589 }
590
591 /* Wipe events */
592 cds_lfht_for_each_entry (ua_chan->events->ht, &iter.iter, ua_event, node.node) {
593 ret = lttng_ht_del(ua_chan->events, &iter);
594 LTTNG_ASSERT(!ret);
595 delete_ust_app_event(sock, ua_event, app);
596 }
597
598 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
599 /* Wipe and free registry from session registry. */
600 if (locked_registry) {
601 try {
602 locked_registry->remove_channel(ua_chan->key, sock >= 0);
603 } catch (const std::exception& ex) {
604 DBG("Could not find channel for removal: %s", ex.what());
605 }
606 }
607
608 /*
609 * A negative socket can be used by the caller when
610 * cleaning-up a ua_chan in an error path. Skip the
611 * accounting in this case.
612 */
613 if (sock >= 0) {
614 save_per_pid_lost_discarded_counters(ua_chan);
615 }
616 }
617
618 if (ua_chan->obj != nullptr) {
619 /* Remove channel from application UST object descriptor. */
620 iter.iter.node = &ua_chan->ust_objd_node.node;
621 ret = lttng_ht_del(app->ust_objd, &iter);
622 LTTNG_ASSERT(!ret);
623 pthread_mutex_lock(&app->sock_lock);
624 ret = lttng_ust_ctl_release_object(sock, ua_chan->obj);
625 pthread_mutex_unlock(&app->sock_lock);
626 if (ret < 0) {
627 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
628 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
629 ua_chan->name,
630 app->pid,
631 app->sock);
632 } else if (ret == -EAGAIN) {
633 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
634 ua_chan->name,
635 app->pid,
636 app->sock);
637 } else {
638 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
639 ua_chan->name,
640 ret,
641 app->pid,
642 app->sock);
643 }
644 }
645 lttng_fd_put(LTTNG_FD_APPS, 1);
646 free(ua_chan->obj);
647 }
648 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
649 }
650
651 int ust_app_register_done(struct ust_app *app)
652 {
653 int ret;
654
655 pthread_mutex_lock(&app->sock_lock);
656 ret = lttng_ust_ctl_register_done(app->sock);
657 pthread_mutex_unlock(&app->sock_lock);
658 return ret;
659 }
660
661 int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data)
662 {
663 int ret, sock;
664
665 if (app) {
666 pthread_mutex_lock(&app->sock_lock);
667 sock = app->sock;
668 } else {
669 sock = -1;
670 }
671 ret = lttng_ust_ctl_release_object(sock, data);
672 if (app) {
673 pthread_mutex_unlock(&app->sock_lock);
674 }
675 return ret;
676 }
677
678 /*
679 * Push metadata to consumer socket.
680 *
681 * RCU read-side lock must be held to guarantee existence of socket.
682 * Must be called with the ust app session lock held.
683 * Must be called with the registry lock held.
684 *
685 * On success, return the len of metadata pushed or else a negative value.
686 * Returning a -EPIPE return value means we could not send the metadata,
687 * but it can be caused by recoverable errors (e.g. the application has
688 * terminated concurrently).
689 */
690 ssize_t ust_app_push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
691 struct consumer_socket *socket,
692 int send_zero_data)
693 {
694 int ret;
695 char *metadata_str = nullptr;
696 size_t len, offset, new_metadata_len_sent;
697 ssize_t ret_val;
698 uint64_t metadata_key, metadata_version;
699
700 LTTNG_ASSERT(locked_registry);
701 LTTNG_ASSERT(socket);
702 ASSERT_RCU_READ_LOCKED();
703
704 metadata_key = locked_registry->_metadata_key;
705
706 /*
707 * Means that no metadata was assigned to the session. This can
708 * happens if no start has been done previously.
709 */
710 if (!metadata_key) {
711 return 0;
712 }
713
714 offset = locked_registry->_metadata_len_sent;
715 len = locked_registry->_metadata_len - locked_registry->_metadata_len_sent;
716 new_metadata_len_sent = locked_registry->_metadata_len;
717 metadata_version = locked_registry->_metadata_version;
718 if (len == 0) {
719 DBG3("No metadata to push for metadata key %" PRIu64,
720 locked_registry->_metadata_key);
721 ret_val = len;
722 if (send_zero_data) {
723 DBG("No metadata to push");
724 goto push_data;
725 }
726 goto end;
727 }
728
729 /* Allocate only what we have to send. */
730 metadata_str = calloc<char>(len);
731 if (!metadata_str) {
732 PERROR("zmalloc ust app metadata string");
733 ret_val = -ENOMEM;
734 goto error;
735 }
736 /* Copy what we haven't sent out. */
737 memcpy(metadata_str, locked_registry->_metadata + offset, len);
738
739 push_data:
740 pthread_mutex_unlock(&locked_registry->_lock);
741 /*
742 * We need to unlock the registry while we push metadata to
743 * break a circular dependency between the consumerd metadata
744 * lock and the sessiond registry lock. Indeed, pushing metadata
745 * to the consumerd awaits that it gets pushed all the way to
746 * relayd, but doing so requires grabbing the metadata lock. If
747 * a concurrent metadata request is being performed by
748 * consumerd, this can try to grab the registry lock on the
749 * sessiond while holding the metadata lock on the consumer
750 * daemon. Those push and pull schemes are performed on two
751 * different bidirectionnal communication sockets.
752 */
753 ret = consumer_push_metadata(
754 socket, metadata_key, metadata_str, len, offset, metadata_version);
755 pthread_mutex_lock(&locked_registry->_lock);
756 if (ret < 0) {
757 /*
758 * There is an acceptable race here between the registry
759 * metadata key assignment and the creation on the
760 * consumer. The session daemon can concurrently push
761 * metadata for this registry while being created on the
762 * consumer since the metadata key of the registry is
763 * assigned *before* it is setup to avoid the consumer
764 * to ask for metadata that could possibly be not found
765 * in the session daemon.
766 *
767 * The metadata will get pushed either by the session
768 * being stopped or the consumer requesting metadata if
769 * that race is triggered.
770 */
771 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
772 ret = 0;
773 } else {
774 ERR("Error pushing metadata to consumer");
775 }
776 ret_val = ret;
777 goto error_push;
778 } else {
779 /*
780 * Metadata may have been concurrently pushed, since
781 * we're not holding the registry lock while pushing to
782 * consumer. This is handled by the fact that we send
783 * the metadata content, size, and the offset at which
784 * that metadata belongs. This may arrive out of order
785 * on the consumer side, and the consumer is able to
786 * deal with overlapping fragments. The consumer
787 * supports overlapping fragments, which must be
788 * contiguous starting from offset 0. We keep the
789 * largest metadata_len_sent value of the concurrent
790 * send.
791 */
792 locked_registry->_metadata_len_sent =
793 std::max(locked_registry->_metadata_len_sent, new_metadata_len_sent);
794 }
795 free(metadata_str);
796 return len;
797
798 end:
799 error:
800 if (ret_val) {
801 /*
802 * On error, flag the registry that the metadata is
803 * closed. We were unable to push anything and this
804 * means that either the consumer is not responding or
805 * the metadata cache has been destroyed on the
806 * consumer.
807 */
808 locked_registry->_metadata_closed = true;
809 }
810 error_push:
811 free(metadata_str);
812 return ret_val;
813 }
814
815 /*
816 * For a given application and session, push metadata to consumer.
817 * Either sock or consumer is required : if sock is NULL, the default
818 * socket to send the metadata is retrieved from consumer, if sock
819 * is not NULL we use it to send the metadata.
820 * RCU read-side lock must be held while calling this function,
821 * therefore ensuring existence of registry. It also ensures existence
822 * of socket throughout this function.
823 *
824 * Return 0 on success else a negative error.
825 * Returning a -EPIPE return value means we could not send the metadata,
826 * but it can be caused by recoverable errors (e.g. the application has
827 * terminated concurrently).
828 */
829 static int push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
830 struct consumer_output *consumer)
831 {
832 int ret_val;
833 ssize_t ret;
834 struct consumer_socket *socket;
835
836 LTTNG_ASSERT(locked_registry);
837 LTTNG_ASSERT(consumer);
838 ASSERT_RCU_READ_LOCKED();
839
840 if (locked_registry->_metadata_closed) {
841 ret_val = -EPIPE;
842 goto error;
843 }
844
845 /* Get consumer socket to use to push the metadata.*/
846 socket = consumer_find_socket_by_bitness(locked_registry->abi.bits_per_long, consumer);
847 if (!socket) {
848 ret_val = -1;
849 goto error;
850 }
851
852 ret = ust_app_push_metadata(locked_registry, socket, 0);
853 if (ret < 0) {
854 ret_val = ret;
855 goto error;
856 }
857 return 0;
858
859 error:
860 return ret_val;
861 }
862
863 /*
864 * Send to the consumer a close metadata command for the given session. Once
865 * done, the metadata channel is deleted and the session metadata pointer is
866 * nullified. The session lock MUST be held unless the application is
867 * in the destroy path.
868 *
869 * Do not hold the registry lock while communicating with the consumerd, because
870 * doing so causes inter-process deadlocks between consumerd and sessiond with
871 * the metadata request notification.
872 *
873 * Return 0 on success else a negative value.
874 */
875 static int close_metadata(uint64_t metadata_key,
876 unsigned int consumer_bitness,
877 struct consumer_output *consumer)
878 {
879 int ret;
880 struct consumer_socket *socket;
881 lttng::urcu::read_lock_guard read_lock_guard;
882
883 LTTNG_ASSERT(consumer);
884
885 /* Get consumer socket to use to push the metadata. */
886 socket = consumer_find_socket_by_bitness(consumer_bitness, consumer);
887 if (!socket) {
888 ret = -1;
889 goto end;
890 }
891
892 ret = consumer_close_metadata(socket, metadata_key);
893 if (ret < 0) {
894 goto end;
895 }
896
897 end:
898 return ret;
899 }
900
901 static void delete_ust_app_session_rcu(struct rcu_head *head)
902 {
903 struct ust_app_session *ua_sess =
904 lttng::utils::container_of(head, &ust_app_session::rcu_head);
905
906 lttng_ht_destroy(ua_sess->channels);
907 free(ua_sess);
908 }
909
910 /*
911 * Delete ust app session safely. RCU read lock must be held before calling
912 * this function.
913 *
914 * The session list lock must be held by the caller.
915 */
916 static void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, struct ust_app *app)
917 {
918 int ret;
919 struct lttng_ht_iter iter;
920 struct ust_app_channel *ua_chan;
921
922 LTTNG_ASSERT(ua_sess);
923 ASSERT_RCU_READ_LOCKED();
924
925 pthread_mutex_lock(&ua_sess->lock);
926
927 LTTNG_ASSERT(!ua_sess->deleted);
928 ua_sess->deleted = true;
929
930 auto locked_registry = get_locked_session_registry(ua_sess);
931 /* Registry can be null on error path during initialization. */
932 if (locked_registry) {
933 /* Push metadata for application before freeing the application. */
934 (void) push_metadata(locked_registry, ua_sess->consumer);
935 }
936
937 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
938 ret = lttng_ht_del(ua_sess->channels, &iter);
939 LTTNG_ASSERT(!ret);
940 delete_ust_app_channel(sock, ua_chan, app, locked_registry);
941 }
942
943 if (locked_registry) {
944 /*
945 * Don't ask to close metadata for global per UID buffers. Close
946 * metadata only on destroy trace session in this case. Also, the
947 * previous push metadata could have flag the metadata registry to
948 * close so don't send a close command if closed.
949 */
950 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
951 const auto metadata_key = locked_registry->_metadata_key;
952 const auto consumer_bitness = locked_registry->abi.bits_per_long;
953
954 if (!locked_registry->_metadata_closed && metadata_key != 0) {
955 locked_registry->_metadata_closed = true;
956 }
957
958 /* Release lock before communication, see comments in close_metadata(). */
959 locked_registry.reset();
960 (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer);
961 }
962 }
963
964 /* In case of per PID, the registry is kept in the session. */
965 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
966 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
967 if (reg_pid) {
968 /*
969 * Registry can be null on error path during
970 * initialization.
971 */
972 buffer_reg_pid_remove(reg_pid);
973 buffer_reg_pid_destroy(reg_pid);
974 }
975 }
976
977 if (ua_sess->handle != -1) {
978 pthread_mutex_lock(&app->sock_lock);
979 ret = lttng_ust_ctl_release_handle(sock, ua_sess->handle);
980 pthread_mutex_unlock(&app->sock_lock);
981 if (ret < 0) {
982 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
983 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
984 app->pid,
985 app->sock);
986 } else if (ret == -EAGAIN) {
987 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
988 app->pid,
989 app->sock);
990 } else {
991 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
992 ret,
993 app->pid,
994 app->sock);
995 }
996 }
997
998 /* Remove session from application UST object descriptor. */
999 iter.iter.node = &ua_sess->ust_objd_node.node;
1000 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
1001 LTTNG_ASSERT(!ret);
1002 }
1003
1004 pthread_mutex_unlock(&ua_sess->lock);
1005
1006 consumer_output_put(ua_sess->consumer);
1007
1008 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
1009 }
1010
1011 /*
1012 * Delete a traceable application structure from the global list. Never call
1013 * this function outside of a call_rcu call.
1014 */
1015 static void delete_ust_app(struct ust_app *app)
1016 {
1017 int ret, sock;
1018 struct ust_app_session *ua_sess, *tmp_ua_sess;
1019 struct lttng_ht_iter iter;
1020 struct ust_app_event_notifier_rule *event_notifier_rule;
1021 bool event_notifier_write_fd_is_open;
1022
1023 /*
1024 * The session list lock must be held during this function to guarantee
1025 * the existence of ua_sess.
1026 */
1027 session_lock_list();
1028 /* Delete ust app sessions info */
1029 sock = app->sock;
1030 app->sock = -1;
1031
1032 /* Wipe sessions */
1033 cds_list_for_each_entry_safe (ua_sess, tmp_ua_sess, &app->teardown_head, teardown_node) {
1034 /* Free every object in the session and the session. */
1035 lttng::urcu::read_lock_guard read_lock;
1036 delete_ust_app_session(sock, ua_sess, app);
1037 }
1038
1039 /* Remove the event notifier rules associated with this app. */
1040 {
1041 lttng::urcu::read_lock_guard read_lock;
1042
1043 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
1044 &iter.iter,
1045 event_notifier_rule,
1046 node.node) {
1047 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter);
1048 LTTNG_ASSERT(!ret);
1049
1050 delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app);
1051 }
1052 }
1053
1054 lttng_ht_destroy(app->sessions);
1055 lttng_ht_destroy(app->ust_sessions_objd);
1056 lttng_ht_destroy(app->ust_objd);
1057 lttng_ht_destroy(app->token_to_event_notifier_rule_ht);
1058
1059 /*
1060 * This could be NULL if the event notifier setup failed (e.g the app
1061 * was killed or the tracer does not support this feature).
1062 */
1063 if (app->event_notifier_group.object) {
1064 enum lttng_error_code ret_code;
1065 enum event_notifier_error_accounting_status status;
1066
1067 const int event_notifier_read_fd =
1068 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe);
1069
1070 ret_code = notification_thread_command_remove_tracer_event_source(
1071 the_notification_thread_handle, event_notifier_read_fd);
1072 if (ret_code != LTTNG_OK) {
1073 ERR("Failed to remove application tracer event source from notification thread");
1074 }
1075
1076 status = event_notifier_error_accounting_unregister_app(app);
1077 if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
1078 ERR("Error unregistering app from event notifier error accounting");
1079 }
1080
1081 lttng_ust_ctl_release_object(sock, app->event_notifier_group.object);
1082 free(app->event_notifier_group.object);
1083 }
1084
1085 event_notifier_write_fd_is_open =
1086 lttng_pipe_is_write_open(app->event_notifier_group.event_pipe);
1087 lttng_pipe_destroy(app->event_notifier_group.event_pipe);
1088 /*
1089 * Release the file descriptors reserved for the event notifier pipe.
1090 * The app could be destroyed before the write end of the pipe could be
1091 * passed to the application (and closed). In that case, both file
1092 * descriptors must be released.
1093 */
1094 lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1);
1095
1096 /*
1097 * Wait until we have deleted the application from the sock hash table
1098 * before closing this socket, otherwise an application could re-use the
1099 * socket ID and race with the teardown, using the same hash table entry.
1100 *
1101 * It's OK to leave the close in call_rcu. We want it to stay unique for
1102 * all RCU readers that could run concurrently with unregister app,
1103 * therefore we _need_ to only close that socket after a grace period. So
1104 * it should stay in this RCU callback.
1105 *
1106 * This close() is a very important step of the synchronization model so
1107 * every modification to this function must be carefully reviewed.
1108 */
1109 ret = close(sock);
1110 if (ret) {
1111 PERROR("close");
1112 }
1113 lttng_fd_put(LTTNG_FD_APPS, 1);
1114
1115 DBG2("UST app pid %d deleted", app->pid);
1116 free(app);
1117 session_unlock_list();
1118 }
1119
1120 /*
1121 * URCU intermediate call to delete an UST app.
1122 */
1123 static void delete_ust_app_rcu(struct rcu_head *head)
1124 {
1125 struct lttng_ht_node_ulong *node =
1126 lttng::utils::container_of(head, &lttng_ht_node_ulong::head);
1127 struct ust_app *app = lttng::utils::container_of(node, &ust_app::pid_n);
1128
1129 DBG3("Call RCU deleting app PID %d", app->pid);
1130 delete_ust_app(app);
1131 }
1132
1133 /*
1134 * Delete the session from the application ht and delete the data structure by
1135 * freeing every object inside and releasing them.
1136 *
1137 * The session list lock must be held by the caller.
1138 */
1139 static void destroy_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
1140 {
1141 int ret;
1142 struct lttng_ht_iter iter;
1143
1144 LTTNG_ASSERT(app);
1145 LTTNG_ASSERT(ua_sess);
1146
1147 iter.iter.node = &ua_sess->node.node;
1148 ret = lttng_ht_del(app->sessions, &iter);
1149 if (ret) {
1150 /* Already scheduled for teardown. */
1151 goto end;
1152 }
1153
1154 /* Once deleted, free the data structure. */
1155 delete_ust_app_session(app->sock, ua_sess, app);
1156
1157 end:
1158 return;
1159 }
1160
1161 /*
1162 * Alloc new UST app session.
1163 */
1164 static struct ust_app_session *alloc_ust_app_session()
1165 {
1166 struct ust_app_session *ua_sess;
1167
1168 /* Init most of the default value by allocating and zeroing */
1169 ua_sess = zmalloc<ust_app_session>();
1170 if (ua_sess == nullptr) {
1171 PERROR("malloc");
1172 goto error_free;
1173 }
1174
1175 ua_sess->handle = -1;
1176 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1177 ua_sess->metadata_attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1178 pthread_mutex_init(&ua_sess->lock, nullptr);
1179
1180 return ua_sess;
1181
1182 error_free:
1183 return nullptr;
1184 }
1185
1186 /*
1187 * Alloc new UST app channel.
1188 */
1189 static struct ust_app_channel *alloc_ust_app_channel(const char *name,
1190 struct ust_app_session *ua_sess,
1191 struct lttng_ust_abi_channel_attr *attr)
1192 {
1193 struct ust_app_channel *ua_chan;
1194
1195 /* Init most of the default value by allocating and zeroing */
1196 ua_chan = zmalloc<ust_app_channel>();
1197 if (ua_chan == nullptr) {
1198 PERROR("malloc");
1199 goto error;
1200 }
1201
1202 /* Setup channel name */
1203 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
1204 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1205
1206 ua_chan->enabled = true;
1207 ua_chan->handle = -1;
1208 ua_chan->session = ua_sess;
1209 ua_chan->key = get_next_channel_key();
1210 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1211 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1212 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
1213
1214 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
1215 CDS_INIT_LIST_HEAD(&ua_chan->ctx_list);
1216
1217 /* Copy attributes */
1218 if (attr) {
1219 /* Translate from lttng_ust_channel to lttng_ust_ctl_consumer_channel_attr. */
1220 ua_chan->attr.subbuf_size = attr->subbuf_size;
1221 ua_chan->attr.num_subbuf = attr->num_subbuf;
1222 ua_chan->attr.overwrite = attr->overwrite;
1223 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
1224 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
1225 ua_chan->attr.output = (lttng_ust_abi_output) attr->output;
1226 ua_chan->attr.blocking_timeout = attr->u.s.blocking_timeout;
1227 }
1228 /* By default, the channel is a per cpu channel. */
1229 ua_chan->attr.type = LTTNG_UST_ABI_CHAN_PER_CPU;
1230
1231 DBG3("UST app channel %s allocated", ua_chan->name);
1232
1233 return ua_chan;
1234
1235 error:
1236 return nullptr;
1237 }
1238
1239 /*
1240 * Allocate and initialize a UST app stream.
1241 *
1242 * Return newly allocated stream pointer or NULL on error.
1243 */
1244 struct ust_app_stream *ust_app_alloc_stream()
1245 {
1246 struct ust_app_stream *stream = nullptr;
1247
1248 stream = zmalloc<ust_app_stream>();
1249 if (stream == nullptr) {
1250 PERROR("zmalloc ust app stream");
1251 goto error;
1252 }
1253
1254 /* Zero could be a valid value for a handle so flag it to -1. */
1255 stream->handle = -1;
1256
1257 error:
1258 return stream;
1259 }
1260
1261 /*
1262 * Alloc new UST app event.
1263 */
1264 static struct ust_app_event *alloc_ust_app_event(char *name, struct lttng_ust_abi_event *attr)
1265 {
1266 struct ust_app_event *ua_event;
1267
1268 /* Init most of the default value by allocating and zeroing */
1269 ua_event = zmalloc<ust_app_event>();
1270 if (ua_event == nullptr) {
1271 PERROR("Failed to allocate ust_app_event structure");
1272 goto error;
1273 }
1274
1275 ua_event->enabled = true;
1276 strncpy(ua_event->name, name, sizeof(ua_event->name));
1277 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1278 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
1279
1280 /* Copy attributes */
1281 if (attr) {
1282 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
1283 }
1284
1285 DBG3("UST app event %s allocated", ua_event->name);
1286
1287 return ua_event;
1288
1289 error:
1290 return nullptr;
1291 }
1292
1293 /*
1294 * Allocate a new UST app event notifier rule.
1295 */
1296 static struct ust_app_event_notifier_rule *
1297 alloc_ust_app_event_notifier_rule(struct lttng_trigger *trigger)
1298 {
1299 enum lttng_event_rule_generate_exclusions_status generate_exclusion_status;
1300 enum lttng_condition_status cond_status;
1301 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
1302 struct lttng_condition *condition = nullptr;
1303 const struct lttng_event_rule *event_rule = nullptr;
1304
1305 ua_event_notifier_rule = zmalloc<ust_app_event_notifier_rule>();
1306 if (ua_event_notifier_rule == nullptr) {
1307 PERROR("Failed to allocate ust_app_event_notifier_rule structure");
1308 goto error;
1309 }
1310
1311 ua_event_notifier_rule->enabled = true;
1312 ua_event_notifier_rule->token = lttng_trigger_get_tracer_token(trigger);
1313 lttng_ht_node_init_u64(&ua_event_notifier_rule->node, ua_event_notifier_rule->token);
1314
1315 condition = lttng_trigger_get_condition(trigger);
1316 LTTNG_ASSERT(condition);
1317 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
1318 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
1319
1320 cond_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
1321 LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK);
1322 LTTNG_ASSERT(event_rule);
1323
1324 ua_event_notifier_rule->error_counter_index =
1325 lttng_condition_event_rule_matches_get_error_counter_index(condition);
1326 /* Acquire the event notifier's reference to the trigger. */
1327 lttng_trigger_get(trigger);
1328
1329 ua_event_notifier_rule->trigger = trigger;
1330 ua_event_notifier_rule->filter = lttng_event_rule_get_filter_bytecode(event_rule);
1331 generate_exclusion_status = lttng_event_rule_generate_exclusions(
1332 event_rule, &ua_event_notifier_rule->exclusion);
1333 switch (generate_exclusion_status) {
1334 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_OK:
1335 case LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE:
1336 break;
1337 default:
1338 /* Error occurred. */
1339 ERR("Failed to generate exclusions from trigger while allocating an event notifier rule");
1340 goto error_put_trigger;
1341 }
1342
1343 DBG3("UST app event notifier rule allocated: token = %" PRIu64,
1344 ua_event_notifier_rule->token);
1345
1346 return ua_event_notifier_rule;
1347
1348 error_put_trigger:
1349 lttng_trigger_put(trigger);
1350 error:
1351 free(ua_event_notifier_rule);
1352 return nullptr;
1353 }
1354
1355 /*
1356 * Alloc new UST app context.
1357 */
1358 static struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context_attr *uctx)
1359 {
1360 struct ust_app_ctx *ua_ctx;
1361
1362 ua_ctx = zmalloc<ust_app_ctx>();
1363 if (ua_ctx == nullptr) {
1364 goto error;
1365 }
1366
1367 CDS_INIT_LIST_HEAD(&ua_ctx->list);
1368
1369 if (uctx) {
1370 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
1371 if (uctx->ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) {
1372 char *provider_name = nullptr, *ctx_name = nullptr;
1373
1374 provider_name = strdup(uctx->u.app_ctx.provider_name);
1375 ctx_name = strdup(uctx->u.app_ctx.ctx_name);
1376 if (!provider_name || !ctx_name) {
1377 free(provider_name);
1378 free(ctx_name);
1379 goto error;
1380 }
1381
1382 ua_ctx->ctx.u.app_ctx.provider_name = provider_name;
1383 ua_ctx->ctx.u.app_ctx.ctx_name = ctx_name;
1384 }
1385 }
1386
1387 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
1388 return ua_ctx;
1389 error:
1390 free(ua_ctx);
1391 return nullptr;
1392 }
1393
1394 /*
1395 * Create a liblttng-ust filter bytecode from given bytecode.
1396 *
1397 * Return allocated filter or NULL on error.
1398 */
1399 static struct lttng_ust_abi_filter_bytecode *
1400 create_ust_filter_bytecode_from_bytecode(const struct lttng_bytecode *orig_f)
1401 {
1402 struct lttng_ust_abi_filter_bytecode *filter = nullptr;
1403
1404 /* Copy filter bytecode. */
1405 filter = zmalloc<lttng_ust_abi_filter_bytecode>(sizeof(*filter) + orig_f->len);
1406 if (!filter) {
1407 PERROR("Failed to allocate lttng_ust_filter_bytecode: bytecode len = %" PRIu32
1408 " bytes",
1409 orig_f->len);
1410 goto error;
1411 }
1412
1413 LTTNG_ASSERT(sizeof(struct lttng_bytecode) == sizeof(struct lttng_ust_abi_filter_bytecode));
1414 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
1415 error:
1416 return filter;
1417 }
1418
1419 /*
1420 * Create a liblttng-ust capture bytecode from given bytecode.
1421 *
1422 * Return allocated filter or NULL on error.
1423 */
1424 static struct lttng_ust_abi_capture_bytecode *
1425 create_ust_capture_bytecode_from_bytecode(const struct lttng_bytecode *orig_f)
1426 {
1427 struct lttng_ust_abi_capture_bytecode *capture = nullptr;
1428
1429 /* Copy capture bytecode. */
1430 capture = zmalloc<lttng_ust_abi_capture_bytecode>(sizeof(*capture) + orig_f->len);
1431 if (!capture) {
1432 PERROR("Failed to allocate lttng_ust_abi_capture_bytecode: bytecode len = %" PRIu32
1433 " bytes",
1434 orig_f->len);
1435 goto error;
1436 }
1437
1438 LTTNG_ASSERT(sizeof(struct lttng_bytecode) ==
1439 sizeof(struct lttng_ust_abi_capture_bytecode));
1440 memcpy(capture, orig_f, sizeof(*capture) + orig_f->len);
1441 error:
1442 return capture;
1443 }
1444
1445 /*
1446 * Find an ust_app using the sock and return it. RCU read side lock must be
1447 * held before calling this helper function.
1448 */
1449 struct ust_app *ust_app_find_by_sock(int sock)
1450 {
1451 struct lttng_ht_node_ulong *node;
1452 struct lttng_ht_iter iter;
1453
1454 ASSERT_RCU_READ_LOCKED();
1455
1456 lttng_ht_lookup(ust_app_ht_by_sock, (void *) ((unsigned long) sock), &iter);
1457 node = lttng_ht_iter_get_node_ulong(&iter);
1458 if (node == nullptr) {
1459 DBG2("UST app find by sock %d not found", sock);
1460 goto error;
1461 }
1462
1463 return lttng::utils::container_of(node, &ust_app::sock_n);
1464
1465 error:
1466 return nullptr;
1467 }
1468
1469 /*
1470 * Find an ust_app using the notify sock and return it. RCU read side lock must
1471 * be held before calling this helper function.
1472 */
1473 static struct ust_app *find_app_by_notify_sock(int sock)
1474 {
1475 struct lttng_ht_node_ulong *node;
1476 struct lttng_ht_iter iter;
1477
1478 ASSERT_RCU_READ_LOCKED();
1479
1480 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *) ((unsigned long) sock), &iter);
1481 node = lttng_ht_iter_get_node_ulong(&iter);
1482 if (node == nullptr) {
1483 DBG2("UST app find by notify sock %d not found", sock);
1484 goto error;
1485 }
1486
1487 return lttng::utils::container_of(node, &ust_app::notify_sock_n);
1488
1489 error:
1490 return nullptr;
1491 }
1492
1493 /*
1494 * Lookup for an ust app event based on event name, filter bytecode and the
1495 * event loglevel.
1496 *
1497 * Return an ust_app_event object or NULL on error.
1498 */
1499 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
1500 const char *name,
1501 const struct lttng_bytecode *filter,
1502 int loglevel_value,
1503 const struct lttng_event_exclusion *exclusion)
1504 {
1505 struct lttng_ht_iter iter;
1506 struct lttng_ht_node_str *node;
1507 struct ust_app_event *event = nullptr;
1508 struct ust_app_ht_key key;
1509
1510 LTTNG_ASSERT(name);
1511 LTTNG_ASSERT(ht);
1512
1513 /* Setup key for event lookup. */
1514 key.name = name;
1515 key.filter = filter;
1516 key.loglevel_type = (lttng_ust_abi_loglevel_type) loglevel_value;
1517 /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */
1518 key.exclusion = exclusion;
1519
1520 /* Lookup using the event name as hash and a custom match fct. */
1521 cds_lfht_lookup(ht->ht,
1522 ht->hash_fct((void *) name, lttng_ht_seed),
1523 ht_match_ust_app_event,
1524 &key,
1525 &iter.iter);
1526 node = lttng_ht_iter_get_node_str(&iter);
1527 if (node == nullptr) {
1528 goto end;
1529 }
1530
1531 event = lttng::utils::container_of(node, &ust_app_event::node);
1532
1533 end:
1534 return event;
1535 }
1536
1537 /*
1538 * Look-up an event notifier rule based on its token id.
1539 *
1540 * Must be called with the RCU read lock held.
1541 * Return an ust_app_event_notifier_rule object or NULL on error.
1542 */
1543 static struct ust_app_event_notifier_rule *find_ust_app_event_notifier_rule(struct lttng_ht *ht,
1544 uint64_t token)
1545 {
1546 struct lttng_ht_iter iter;
1547 struct lttng_ht_node_u64 *node;
1548 struct ust_app_event_notifier_rule *event_notifier_rule = nullptr;
1549
1550 LTTNG_ASSERT(ht);
1551 ASSERT_RCU_READ_LOCKED();
1552
1553 lttng_ht_lookup(ht, &token, &iter);
1554 node = lttng_ht_iter_get_node_u64(&iter);
1555 if (node == nullptr) {
1556 DBG2("UST app event notifier rule token not found: token = %" PRIu64, token);
1557 goto end;
1558 }
1559
1560 event_notifier_rule = lttng::utils::container_of(node, &ust_app_event_notifier_rule::node);
1561 end:
1562 return event_notifier_rule;
1563 }
1564
1565 /*
1566 * Create the channel context on the tracer.
1567 *
1568 * Called with UST app session lock held.
1569 */
1570 static int create_ust_channel_context(struct ust_app_channel *ua_chan,
1571 struct ust_app_ctx *ua_ctx,
1572 struct ust_app *app)
1573 {
1574 int ret;
1575
1576 health_code_update();
1577
1578 pthread_mutex_lock(&app->sock_lock);
1579 ret = lttng_ust_ctl_add_context(app->sock, &ua_ctx->ctx, ua_chan->obj, &ua_ctx->obj);
1580 pthread_mutex_unlock(&app->sock_lock);
1581 if (ret < 0) {
1582 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1583 ret = 0;
1584 DBG3("UST app create channel context failed. Application is dead: pid = %d, sock = %d",
1585 app->pid,
1586 app->sock);
1587 } else if (ret == -EAGAIN) {
1588 ret = 0;
1589 WARN("UST app create channel context failed. Communication time out: pid = %d, sock = %d",
1590 app->pid,
1591 app->sock);
1592 } else {
1593 ERR("UST app create channel context failed with ret %d: pid = %d, sock = %d",
1594 ret,
1595 app->pid,
1596 app->sock);
1597 }
1598 goto error;
1599 }
1600
1601 ua_ctx->handle = ua_ctx->obj->handle;
1602
1603 DBG2("UST app context handle %d created successfully for channel %s",
1604 ua_ctx->handle,
1605 ua_chan->name);
1606
1607 error:
1608 health_code_update();
1609 return ret;
1610 }
1611
1612 /*
1613 * Set the filter on the tracer.
1614 */
1615 static int set_ust_object_filter(struct ust_app *app,
1616 const struct lttng_bytecode *bytecode,
1617 struct lttng_ust_abi_object_data *ust_object)
1618 {
1619 int ret;
1620 struct lttng_ust_abi_filter_bytecode *ust_bytecode = nullptr;
1621
1622 health_code_update();
1623
1624 ust_bytecode = create_ust_filter_bytecode_from_bytecode(bytecode);
1625 if (!ust_bytecode) {
1626 ret = -LTTNG_ERR_NOMEM;
1627 goto error;
1628 }
1629 pthread_mutex_lock(&app->sock_lock);
1630 ret = lttng_ust_ctl_set_filter(app->sock, ust_bytecode, ust_object);
1631 pthread_mutex_unlock(&app->sock_lock);
1632 if (ret < 0) {
1633 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1634 ret = 0;
1635 DBG3("UST app set filter failed. Application is dead: pid = %d, sock = %d",
1636 app->pid,
1637 app->sock);
1638 } else if (ret == -EAGAIN) {
1639 ret = 0;
1640 WARN("UST app set filter failed. Communication time out: pid = %d, sock = %d",
1641 app->pid,
1642 app->sock);
1643 } else {
1644 ERR("UST app set filter failed with ret %d: pid = %d, sock = %d, object = %p",
1645 ret,
1646 app->pid,
1647 app->sock,
1648 ust_object);
1649 }
1650 goto error;
1651 }
1652
1653 DBG2("UST filter successfully set: object = %p", ust_object);
1654
1655 error:
1656 health_code_update();
1657 free(ust_bytecode);
1658 return ret;
1659 }
1660
1661 /*
1662 * Set a capture bytecode for the passed object.
1663 * The sequence number enforces the ordering at runtime and on reception of
1664 * the captured payloads.
1665 */
1666 static int set_ust_capture(struct ust_app *app,
1667 const struct lttng_bytecode *bytecode,
1668 unsigned int capture_seqnum,
1669 struct lttng_ust_abi_object_data *ust_object)
1670 {
1671 int ret;
1672 struct lttng_ust_abi_capture_bytecode *ust_bytecode = nullptr;
1673
1674 health_code_update();
1675
1676 ust_bytecode = create_ust_capture_bytecode_from_bytecode(bytecode);
1677 if (!ust_bytecode) {
1678 ret = -LTTNG_ERR_NOMEM;
1679 goto error;
1680 }
1681
1682 /*
1683 * Set the sequence number to ensure the capture of fields is ordered.
1684 */
1685 ust_bytecode->seqnum = capture_seqnum;
1686
1687 pthread_mutex_lock(&app->sock_lock);
1688 ret = lttng_ust_ctl_set_capture(app->sock, ust_bytecode, ust_object);
1689 pthread_mutex_unlock(&app->sock_lock);
1690 if (ret < 0) {
1691 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1692 ret = 0;
1693 DBG3("UST app set capture failed. Application is dead: pid = %d, sock = %d",
1694 app->pid,
1695 app->sock);
1696 } else if (ret == -EAGAIN) {
1697 ret = 0;
1698 DBG3("UST app set capture failed. Communication timeout: pid = %d, sock = %d",
1699 app->pid,
1700 app->sock);
1701 } else {
1702 ERR("UST app event set capture failed with ret %d: pid = %d, sock = %d",
1703 ret,
1704 app->pid,
1705 app->sock);
1706 }
1707
1708 goto error;
1709 }
1710
1711 DBG2("UST capture successfully set: object = %p", ust_object);
1712
1713 error:
1714 health_code_update();
1715 free(ust_bytecode);
1716 return ret;
1717 }
1718
1719 static struct lttng_ust_abi_event_exclusion *
1720 create_ust_exclusion_from_exclusion(const struct lttng_event_exclusion *exclusion)
1721 {
1722 struct lttng_ust_abi_event_exclusion *ust_exclusion = nullptr;
1723 size_t exclusion_alloc_size = sizeof(struct lttng_ust_abi_event_exclusion) +
1724 LTTNG_UST_ABI_SYM_NAME_LEN * exclusion->count;
1725
1726 ust_exclusion = zmalloc<lttng_ust_abi_event_exclusion>(exclusion_alloc_size);
1727 if (!ust_exclusion) {
1728 PERROR("malloc");
1729 goto end;
1730 }
1731
1732 LTTNG_ASSERT(sizeof(struct lttng_event_exclusion) ==
1733 sizeof(struct lttng_ust_abi_event_exclusion));
1734 memcpy(ust_exclusion, exclusion, exclusion_alloc_size);
1735 end:
1736 return ust_exclusion;
1737 }
1738
1739 /*
1740 * Set event exclusions on the tracer.
1741 */
1742 static int set_ust_object_exclusions(struct ust_app *app,
1743 const struct lttng_event_exclusion *exclusions,
1744 struct lttng_ust_abi_object_data *ust_object)
1745 {
1746 int ret;
1747 struct lttng_ust_abi_event_exclusion *ust_exclusions = nullptr;
1748
1749 LTTNG_ASSERT(exclusions && exclusions->count > 0);
1750
1751 health_code_update();
1752
1753 ust_exclusions = create_ust_exclusion_from_exclusion(exclusions);
1754 if (!ust_exclusions) {
1755 ret = -LTTNG_ERR_NOMEM;
1756 goto error;
1757 }
1758 pthread_mutex_lock(&app->sock_lock);
1759 ret = lttng_ust_ctl_set_exclusion(app->sock, ust_exclusions, ust_object);
1760 pthread_mutex_unlock(&app->sock_lock);
1761 if (ret < 0) {
1762 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1763 ret = 0;
1764 DBG3("UST app event exclusion failed. Application is dead: pid = %d, sock = %d",
1765 app->pid,
1766 app->sock);
1767 } else if (ret == -EAGAIN) {
1768 ret = 0;
1769 WARN("UST app event exclusion failed. Communication time out(pid: %d, sock = %d",
1770 app->pid,
1771 app->sock);
1772 } else {
1773 ERR("UST app event exclusions failed with ret %d: pid = %d, sock = %d, object = %p",
1774 ret,
1775 app->pid,
1776 app->sock,
1777 ust_object);
1778 }
1779 goto error;
1780 }
1781
1782 DBG2("UST exclusions set successfully for object %p", ust_object);
1783
1784 error:
1785 health_code_update();
1786 free(ust_exclusions);
1787 return ret;
1788 }
1789
1790 /*
1791 * Disable the specified event on to UST tracer for the UST session.
1792 */
1793 static int disable_ust_object(struct ust_app *app, struct lttng_ust_abi_object_data *object)
1794 {
1795 int ret;
1796
1797 health_code_update();
1798
1799 pthread_mutex_lock(&app->sock_lock);
1800 ret = lttng_ust_ctl_disable(app->sock, object);
1801 pthread_mutex_unlock(&app->sock_lock);
1802 if (ret < 0) {
1803 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1804 ret = 0;
1805 DBG3("UST app disable object failed. Application is dead: pid = %d, sock = %d",
1806 app->pid,
1807 app->sock);
1808 } else if (ret == -EAGAIN) {
1809 ret = 0;
1810 WARN("UST app disable object failed. Communication time out: pid = %d, sock = %d",
1811 app->pid,
1812 app->sock);
1813 } else {
1814 ERR("UST app disable object failed with ret %d: pid = %d, sock = %d, object = %p",
1815 ret,
1816 app->pid,
1817 app->sock,
1818 object);
1819 }
1820 goto error;
1821 }
1822
1823 DBG2("UST app object %p disabled successfully for app: pid = %d", object, app->pid);
1824
1825 error:
1826 health_code_update();
1827 return ret;
1828 }
1829
1830 /*
1831 * Disable the specified channel on to UST tracer for the UST session.
1832 */
1833 static int disable_ust_channel(struct ust_app *app,
1834 struct ust_app_session *ua_sess,
1835 struct ust_app_channel *ua_chan)
1836 {
1837 int ret;
1838
1839 health_code_update();
1840
1841 pthread_mutex_lock(&app->sock_lock);
1842 ret = lttng_ust_ctl_disable(app->sock, ua_chan->obj);
1843 pthread_mutex_unlock(&app->sock_lock);
1844 if (ret < 0) {
1845 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1846 ret = 0;
1847 DBG3("UST app disable channel failed. Application is dead: pid = %d, sock = %d",
1848 app->pid,
1849 app->sock);
1850 } else if (ret == -EAGAIN) {
1851 ret = 0;
1852 WARN("UST app disable channel failed. Communication time out: pid = %d, sock = %d",
1853 app->pid,
1854 app->sock);
1855 } else {
1856 ERR("UST app channel %s disable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1857 ua_chan->name,
1858 ua_sess->handle,
1859 ret,
1860 app->pid,
1861 app->sock);
1862 }
1863 goto error;
1864 }
1865
1866 DBG2("UST app channel %s disabled successfully for app: pid = %d", ua_chan->name, app->pid);
1867
1868 error:
1869 health_code_update();
1870 return ret;
1871 }
1872
1873 /*
1874 * Enable the specified channel on to UST tracer for the UST session.
1875 */
1876 static int enable_ust_channel(struct ust_app *app,
1877 struct ust_app_session *ua_sess,
1878 struct ust_app_channel *ua_chan)
1879 {
1880 int ret;
1881
1882 health_code_update();
1883
1884 pthread_mutex_lock(&app->sock_lock);
1885 ret = lttng_ust_ctl_enable(app->sock, ua_chan->obj);
1886 pthread_mutex_unlock(&app->sock_lock);
1887 if (ret < 0) {
1888 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1889 ret = 0;
1890 DBG3("UST app channel %s enable failed. Application is dead: pid = %d, sock = %d",
1891 ua_chan->name,
1892 app->pid,
1893 app->sock);
1894 } else if (ret == -EAGAIN) {
1895 ret = 0;
1896 WARN("UST app channel %s enable failed. Communication time out: pid = %d, sock = %d",
1897 ua_chan->name,
1898 app->pid,
1899 app->sock);
1900 } else {
1901 ERR("UST app channel %s enable failed, session handle %d, with ret %d: pid = %d, sock = %d",
1902 ua_chan->name,
1903 ua_sess->handle,
1904 ret,
1905 app->pid,
1906 app->sock);
1907 }
1908 goto error;
1909 }
1910
1911 ua_chan->enabled = true;
1912
1913 DBG2("UST app channel %s enabled successfully for app: pid = %d", ua_chan->name, app->pid);
1914
1915 error:
1916 health_code_update();
1917 return ret;
1918 }
1919
1920 /*
1921 * Enable the specified event on to UST tracer for the UST session.
1922 */
1923 static int enable_ust_object(struct ust_app *app, struct lttng_ust_abi_object_data *ust_object)
1924 {
1925 int ret;
1926
1927 health_code_update();
1928
1929 pthread_mutex_lock(&app->sock_lock);
1930 ret = lttng_ust_ctl_enable(app->sock, ust_object);
1931 pthread_mutex_unlock(&app->sock_lock);
1932 if (ret < 0) {
1933 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1934 ret = 0;
1935 DBG3("UST app enable object failed. Application is dead: pid = %d, sock = %d",
1936 app->pid,
1937 app->sock);
1938 } else if (ret == -EAGAIN) {
1939 ret = 0;
1940 WARN("UST app enable object failed. Communication time out: pid = %d, sock = %d",
1941 app->pid,
1942 app->sock);
1943 } else {
1944 ERR("UST app enable object failed with ret %d: pid = %d, sock = %d, object = %p",
1945 ret,
1946 app->pid,
1947 app->sock,
1948 ust_object);
1949 }
1950 goto error;
1951 }
1952
1953 DBG2("UST app object %p enabled successfully for app: pid = %d", ust_object, app->pid);
1954
1955 error:
1956 health_code_update();
1957 return ret;
1958 }
1959
1960 /*
1961 * Send channel and stream buffer to application.
1962 *
1963 * Return 0 on success. On error, a negative value is returned.
1964 */
1965 static int send_channel_pid_to_ust(struct ust_app *app,
1966 struct ust_app_session *ua_sess,
1967 struct ust_app_channel *ua_chan)
1968 {
1969 int ret;
1970 struct ust_app_stream *stream, *stmp;
1971
1972 LTTNG_ASSERT(app);
1973 LTTNG_ASSERT(ua_sess);
1974 LTTNG_ASSERT(ua_chan);
1975
1976 health_code_update();
1977
1978 DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, app->sock);
1979
1980 /* Send channel to the application. */
1981 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
1982 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
1983 ret = -ENOTCONN; /* Caused by app exiting. */
1984 goto error;
1985 } else if (ret == -EAGAIN) {
1986 /* Caused by timeout. */
1987 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
1988 "\".",
1989 app->pid,
1990 ua_chan->name,
1991 ua_sess->tracing_id);
1992 /* Treat this the same way as an application that is exiting. */
1993 ret = -ENOTCONN;
1994 goto error;
1995 } else if (ret < 0) {
1996 goto error;
1997 }
1998
1999 health_code_update();
2000
2001 /* Send all streams to application. */
2002 cds_list_for_each_entry_safe (stream, stmp, &ua_chan->streams.head, list) {
2003 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
2004 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2005 ret = -ENOTCONN; /* Caused by app exiting. */
2006 goto error;
2007 } else if (ret == -EAGAIN) {
2008 /* Caused by timeout. */
2009 WARN("Communication with application %d timed out on send_stream for stream \"%s\" of channel \"%s\" of session \"%" PRIu64
2010 "\".",
2011 app->pid,
2012 stream->name,
2013 ua_chan->name,
2014 ua_sess->tracing_id);
2015 /*
2016 * Treat this the same way as an application that is
2017 * exiting.
2018 */
2019 ret = -ENOTCONN;
2020 } else if (ret < 0) {
2021 goto error;
2022 }
2023 /* We don't need the stream anymore once sent to the tracer. */
2024 cds_list_del(&stream->list);
2025 delete_ust_app_stream(-1, stream, app);
2026 }
2027
2028 error:
2029 health_code_update();
2030 return ret;
2031 }
2032
2033 /*
2034 * Create the specified event onto the UST tracer for a UST session.
2035 *
2036 * Should be called with session mutex held.
2037 */
2038 static int create_ust_event(struct ust_app *app,
2039 struct ust_app_channel *ua_chan,
2040 struct ust_app_event *ua_event)
2041 {
2042 int ret = 0;
2043
2044 health_code_update();
2045
2046 /* Create UST event on tracer */
2047 pthread_mutex_lock(&app->sock_lock);
2048 ret = lttng_ust_ctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, &ua_event->obj);
2049 pthread_mutex_unlock(&app->sock_lock);
2050 if (ret < 0) {
2051 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2052 ret = 0;
2053 DBG3("UST app create event failed. Application is dead: pid = %d, sock = %d",
2054 app->pid,
2055 app->sock);
2056 } else if (ret == -EAGAIN) {
2057 ret = 0;
2058 WARN("UST app create event failed. Communication time out: pid = %d, sock = %d",
2059 app->pid,
2060 app->sock);
2061 } else {
2062 ERR("UST app create event '%s' failed with ret %d: pid = %d, sock = %d",
2063 ua_event->attr.name,
2064 ret,
2065 app->pid,
2066 app->sock);
2067 }
2068 goto error;
2069 }
2070
2071 ua_event->handle = ua_event->obj->handle;
2072
2073 DBG2("UST app event %s created successfully for pid:%d object = %p",
2074 ua_event->attr.name,
2075 app->pid,
2076 ua_event->obj);
2077
2078 health_code_update();
2079
2080 /* Set filter if one is present. */
2081 if (ua_event->filter) {
2082 ret = set_ust_object_filter(app, ua_event->filter, ua_event->obj);
2083 if (ret < 0) {
2084 goto error;
2085 }
2086 }
2087
2088 /* Set exclusions for the event */
2089 if (ua_event->exclusion) {
2090 ret = set_ust_object_exclusions(app, ua_event->exclusion, ua_event->obj);
2091 if (ret < 0) {
2092 goto error;
2093 }
2094 }
2095
2096 /* If event not enabled, disable it on the tracer */
2097 if (ua_event->enabled) {
2098 /*
2099 * We now need to explicitly enable the event, since it
2100 * is now disabled at creation.
2101 */
2102 ret = enable_ust_object(app, ua_event->obj);
2103 if (ret < 0) {
2104 /*
2105 * If we hit an EPERM, something is wrong with our enable call. If
2106 * we get an EEXIST, there is a problem on the tracer side since we
2107 * just created it.
2108 */
2109 switch (ret) {
2110 case -LTTNG_UST_ERR_PERM:
2111 /* Code flow problem */
2112 abort();
2113 case -LTTNG_UST_ERR_EXIST:
2114 /* It's OK for our use case. */
2115 ret = 0;
2116 break;
2117 default:
2118 break;
2119 }
2120 goto error;
2121 }
2122 }
2123
2124 error:
2125 health_code_update();
2126 return ret;
2127 }
2128
2129 static int
2130 init_ust_event_notifier_from_event_rule(const struct lttng_event_rule *rule,
2131 struct lttng_ust_abi_event_notifier *event_notifier)
2132 {
2133 enum lttng_event_rule_status status;
2134 enum lttng_ust_abi_loglevel_type ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2135 int loglevel = -1, ret = 0;
2136 const char *pattern;
2137
2138 memset(event_notifier, 0, sizeof(*event_notifier));
2139
2140 if (lttng_event_rule_targets_agent_domain(rule)) {
2141 /*
2142 * Special event for agents
2143 * The actual meat of the event is in the filter that will be
2144 * attached later on.
2145 * Set the default values for the agent event.
2146 */
2147 pattern = event_get_default_agent_ust_name(lttng_event_rule_get_domain_type(rule));
2148 loglevel = 0;
2149 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2150 } else {
2151 const struct lttng_log_level_rule *log_level_rule;
2152
2153 LTTNG_ASSERT(lttng_event_rule_get_type(rule) ==
2154 LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT);
2155
2156 status = lttng_event_rule_user_tracepoint_get_name_pattern(rule, &pattern);
2157 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
2158 /* At this point, this is a fatal error. */
2159 abort();
2160 }
2161
2162 status = lttng_event_rule_user_tracepoint_get_log_level_rule(rule, &log_level_rule);
2163 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
2164 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_ALL;
2165 } else if (status == LTTNG_EVENT_RULE_STATUS_OK) {
2166 enum lttng_log_level_rule_status llr_status;
2167
2168 switch (lttng_log_level_rule_get_type(log_level_rule)) {
2169 case LTTNG_LOG_LEVEL_RULE_TYPE_EXACTLY:
2170 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_SINGLE;
2171 llr_status = lttng_log_level_rule_exactly_get_level(log_level_rule,
2172 &loglevel);
2173 break;
2174 case LTTNG_LOG_LEVEL_RULE_TYPE_AT_LEAST_AS_SEVERE_AS:
2175 ust_loglevel_type = LTTNG_UST_ABI_LOGLEVEL_RANGE;
2176 llr_status = lttng_log_level_rule_at_least_as_severe_as_get_level(
2177 log_level_rule, &loglevel);
2178 break;
2179 default:
2180 abort();
2181 }
2182
2183 LTTNG_ASSERT(llr_status == LTTNG_LOG_LEVEL_RULE_STATUS_OK);
2184 } else {
2185 /* At this point this is a fatal error. */
2186 abort();
2187 }
2188 }
2189
2190 event_notifier->event.instrumentation = LTTNG_UST_ABI_TRACEPOINT;
2191 ret = lttng_strncpy(
2192 event_notifier->event.name, pattern, sizeof(event_notifier->event.name));
2193 if (ret) {
2194 ERR("Failed to copy event rule pattern to notifier: pattern = '%s' ", pattern);
2195 goto end;
2196 }
2197
2198 event_notifier->event.loglevel_type = ust_loglevel_type;
2199 event_notifier->event.loglevel = loglevel;
2200 end:
2201 return ret;
2202 }
2203
2204 /*
2205 * Create the specified event notifier against the user space tracer of a
2206 * given application.
2207 */
2208 static int create_ust_event_notifier(struct ust_app *app,
2209 struct ust_app_event_notifier_rule *ua_event_notifier_rule)
2210 {
2211 int ret = 0;
2212 enum lttng_condition_status condition_status;
2213 const struct lttng_condition *condition = nullptr;
2214 struct lttng_ust_abi_event_notifier event_notifier;
2215 const struct lttng_event_rule *event_rule = nullptr;
2216 unsigned int capture_bytecode_count = 0, i;
2217 enum lttng_condition_status cond_status;
2218 enum lttng_event_rule_type event_rule_type;
2219
2220 health_code_update();
2221 LTTNG_ASSERT(app->event_notifier_group.object);
2222
2223 condition = lttng_trigger_get_const_condition(ua_event_notifier_rule->trigger);
2224 LTTNG_ASSERT(condition);
2225 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
2226 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
2227
2228 condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
2229 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
2230
2231 LTTNG_ASSERT(event_rule);
2232
2233 event_rule_type = lttng_event_rule_get_type(event_rule);
2234 LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT ||
2235 event_rule_type == LTTNG_EVENT_RULE_TYPE_JUL_LOGGING ||
2236 event_rule_type == LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING ||
2237 event_rule_type == LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING);
2238
2239 init_ust_event_notifier_from_event_rule(event_rule, &event_notifier);
2240 event_notifier.event.token = ua_event_notifier_rule->token;
2241 event_notifier.error_counter_index = ua_event_notifier_rule->error_counter_index;
2242
2243 /* Create UST event notifier against the tracer. */
2244 pthread_mutex_lock(&app->sock_lock);
2245 ret = lttng_ust_ctl_create_event_notifier(app->sock,
2246 &event_notifier,
2247 app->event_notifier_group.object,
2248 &ua_event_notifier_rule->obj);
2249 pthread_mutex_unlock(&app->sock_lock);
2250 if (ret < 0) {
2251 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2252 ret = 0;
2253 DBG3("UST app create event notifier failed. Application is dead: pid = %d, sock = %d",
2254 app->pid,
2255 app->sock);
2256 } else if (ret == -EAGAIN) {
2257 ret = 0;
2258 WARN("UST app create event notifier failed. Communication time out: pid = %d, sock = %d",
2259 app->pid,
2260 app->sock);
2261 } else {
2262 ERR("UST app create event notifier '%s' failed with ret %d: pid = %d, sock = %d",
2263 event_notifier.event.name,
2264 ret,
2265 app->pid,
2266 app->sock);
2267 }
2268 goto error;
2269 }
2270
2271 ua_event_notifier_rule->handle = ua_event_notifier_rule->obj->handle;
2272
2273 DBG2("UST app event notifier %s created successfully: app = '%s': pid = %d, object = %p",
2274 event_notifier.event.name,
2275 app->name,
2276 app->pid,
2277 ua_event_notifier_rule->obj);
2278
2279 health_code_update();
2280
2281 /* Set filter if one is present. */
2282 if (ua_event_notifier_rule->filter) {
2283 ret = set_ust_object_filter(
2284 app, ua_event_notifier_rule->filter, ua_event_notifier_rule->obj);
2285 if (ret < 0) {
2286 goto error;
2287 }
2288 }
2289
2290 /* Set exclusions for the event. */
2291 if (ua_event_notifier_rule->exclusion) {
2292 ret = set_ust_object_exclusions(
2293 app, ua_event_notifier_rule->exclusion, ua_event_notifier_rule->obj);
2294 if (ret < 0) {
2295 goto error;
2296 }
2297 }
2298
2299 /* Set the capture bytecodes. */
2300 cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
2301 condition, &capture_bytecode_count);
2302 LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK);
2303
2304 for (i = 0; i < capture_bytecode_count; i++) {
2305 const struct lttng_bytecode *capture_bytecode =
2306 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition,
2307 i);
2308
2309 ret = set_ust_capture(app, capture_bytecode, i, ua_event_notifier_rule->obj);
2310 if (ret < 0) {
2311 goto error;
2312 }
2313 }
2314
2315 /*
2316 * We now need to explicitly enable the event, since it
2317 * is disabled at creation.
2318 */
2319 ret = enable_ust_object(app, ua_event_notifier_rule->obj);
2320 if (ret < 0) {
2321 /*
2322 * If we hit an EPERM, something is wrong with our enable call.
2323 * If we get an EEXIST, there is a problem on the tracer side
2324 * since we just created it.
2325 */
2326 switch (ret) {
2327 case -LTTNG_UST_ERR_PERM:
2328 /* Code flow problem. */
2329 abort();
2330 case -LTTNG_UST_ERR_EXIST:
2331 /* It's OK for our use case. */
2332 ret = 0;
2333 break;
2334 default:
2335 break;
2336 }
2337
2338 goto error;
2339 }
2340
2341 ua_event_notifier_rule->enabled = true;
2342
2343 error:
2344 health_code_update();
2345 return ret;
2346 }
2347
2348 /*
2349 * Copy data between an UST app event and a LTT event.
2350 */
2351 static void shadow_copy_event(struct ust_app_event *ua_event, struct ltt_ust_event *uevent)
2352 {
2353 size_t exclusion_alloc_size;
2354
2355 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
2356 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
2357
2358 ua_event->enabled = uevent->enabled;
2359
2360 /* Copy event attributes */
2361 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
2362
2363 /* Copy filter bytecode */
2364 if (uevent->filter) {
2365 ua_event->filter = lttng_bytecode_copy(uevent->filter);
2366 /* Filter might be NULL here in case of ENONEM. */
2367 }
2368
2369 /* Copy exclusion data */
2370 if (uevent->exclusion) {
2371 exclusion_alloc_size = sizeof(struct lttng_event_exclusion) +
2372 LTTNG_UST_ABI_SYM_NAME_LEN * uevent->exclusion->count;
2373 ua_event->exclusion = zmalloc<lttng_event_exclusion>(exclusion_alloc_size);
2374 if (ua_event->exclusion == nullptr) {
2375 PERROR("malloc");
2376 } else {
2377 memcpy(ua_event->exclusion, uevent->exclusion, exclusion_alloc_size);
2378 }
2379 }
2380 }
2381
2382 /*
2383 * Copy data between an UST app channel and a LTT channel.
2384 */
2385 static void shadow_copy_channel(struct ust_app_channel *ua_chan, struct ltt_ust_channel *uchan)
2386 {
2387 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
2388
2389 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
2390 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
2391
2392 ua_chan->tracefile_size = uchan->tracefile_size;
2393 ua_chan->tracefile_count = uchan->tracefile_count;
2394
2395 /* Copy event attributes since the layout is different. */
2396 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
2397 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
2398 ua_chan->attr.overwrite = uchan->attr.overwrite;
2399 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
2400 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
2401 ua_chan->monitor_timer_interval = uchan->monitor_timer_interval;
2402 ua_chan->attr.output = (lttng_ust_abi_output) uchan->attr.output;
2403 ua_chan->attr.blocking_timeout = uchan->attr.u.s.blocking_timeout;
2404
2405 /*
2406 * Note that the attribute channel type is not set since the channel on the
2407 * tracing registry side does not have this information.
2408 */
2409
2410 ua_chan->enabled = uchan->enabled;
2411 ua_chan->tracing_channel_id = uchan->id;
2412
2413 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
2414 }
2415
2416 /*
2417 * Copy data between a UST app session and a regular LTT session.
2418 */
2419 static void shadow_copy_session(struct ust_app_session *ua_sess,
2420 struct ltt_ust_session *usess,
2421 struct ust_app *app)
2422 {
2423 struct tm *timeinfo;
2424 char datetime[16];
2425 int ret;
2426 char tmp_shm_path[PATH_MAX];
2427
2428 timeinfo = localtime(&app->registration_time);
2429 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
2430
2431 DBG2("Shadow copy of session handle %d", ua_sess->handle);
2432
2433 ua_sess->tracing_id = usess->id;
2434 ua_sess->id = get_next_session_id();
2435 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.uid, app->uid);
2436 LTTNG_OPTIONAL_SET(&ua_sess->real_credentials.gid, app->gid);
2437 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.uid, usess->uid);
2438 LTTNG_OPTIONAL_SET(&ua_sess->effective_credentials.gid, usess->gid);
2439 ua_sess->buffer_type = usess->buffer_type;
2440 ua_sess->bits_per_long = app->abi.bits_per_long;
2441
2442 /* There is only one consumer object per session possible. */
2443 consumer_output_get(usess->consumer);
2444 ua_sess->consumer = usess->consumer;
2445
2446 ua_sess->output_traces = usess->output_traces;
2447 ua_sess->live_timer_interval = usess->live_timer_interval;
2448 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &usess->metadata_attr);
2449
2450 switch (ua_sess->buffer_type) {
2451 case LTTNG_BUFFER_PER_PID:
2452 ret = snprintf(ua_sess->path,
2453 sizeof(ua_sess->path),
2454 DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
2455 app->name,
2456 app->pid,
2457 datetime);
2458 break;
2459 case LTTNG_BUFFER_PER_UID:
2460 ret = snprintf(ua_sess->path,
2461 sizeof(ua_sess->path),
2462 DEFAULT_UST_TRACE_UID_PATH,
2463 lttng_credentials_get_uid(&ua_sess->real_credentials),
2464 app->abi.bits_per_long);
2465 break;
2466 default:
2467 abort();
2468 goto error;
2469 }
2470 if (ret < 0) {
2471 PERROR("asprintf UST shadow copy session");
2472 abort();
2473 goto error;
2474 }
2475
2476 strncpy(ua_sess->root_shm_path, usess->root_shm_path, sizeof(ua_sess->root_shm_path));
2477 ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0';
2478 strncpy(ua_sess->shm_path, usess->shm_path, sizeof(ua_sess->shm_path));
2479 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2480 if (ua_sess->shm_path[0]) {
2481 switch (ua_sess->buffer_type) {
2482 case LTTNG_BUFFER_PER_PID:
2483 ret = snprintf(tmp_shm_path,
2484 sizeof(tmp_shm_path),
2485 "/" DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s",
2486 app->name,
2487 app->pid,
2488 datetime);
2489 break;
2490 case LTTNG_BUFFER_PER_UID:
2491 ret = snprintf(tmp_shm_path,
2492 sizeof(tmp_shm_path),
2493 "/" DEFAULT_UST_TRACE_UID_PATH,
2494 app->uid,
2495 app->abi.bits_per_long);
2496 break;
2497 default:
2498 abort();
2499 goto error;
2500 }
2501 if (ret < 0) {
2502 PERROR("sprintf UST shadow copy session");
2503 abort();
2504 goto error;
2505 }
2506 strncat(ua_sess->shm_path,
2507 tmp_shm_path,
2508 sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1);
2509 ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0';
2510 }
2511 return;
2512
2513 error:
2514 consumer_output_put(ua_sess->consumer);
2515 }
2516
2517 /*
2518 * Lookup sesison wrapper.
2519 */
2520 static void __lookup_session_by_app(const struct ltt_ust_session *usess,
2521 struct ust_app *app,
2522 struct lttng_ht_iter *iter)
2523 {
2524 /* Get right UST app session from app */
2525 lttng_ht_lookup(app->sessions, &usess->id, iter);
2526 }
2527
2528 /*
2529 * Return ust app session from the app session hashtable using the UST session
2530 * id.
2531 */
2532 static struct ust_app_session *lookup_session_by_app(const struct ltt_ust_session *usess,
2533 struct ust_app *app)
2534 {
2535 struct lttng_ht_iter iter;
2536 struct lttng_ht_node_u64 *node;
2537
2538 __lookup_session_by_app(usess, app, &iter);
2539 node = lttng_ht_iter_get_node_u64(&iter);
2540 if (node == nullptr) {
2541 goto error;
2542 }
2543
2544 return lttng::utils::container_of(node, &ust_app_session::node);
2545
2546 error:
2547 return nullptr;
2548 }
2549
2550 /*
2551 * Setup buffer registry per PID for the given session and application. If none
2552 * is found, a new one is created, added to the global registry and
2553 * initialized. If regp is valid, it's set with the newly created object.
2554 *
2555 * Return 0 on success or else a negative value.
2556 */
2557 static int setup_buffer_reg_pid(struct ust_app_session *ua_sess,
2558 struct ust_app *app,
2559 struct buffer_reg_pid **regp)
2560 {
2561 int ret = 0;
2562 struct buffer_reg_pid *reg_pid;
2563
2564 LTTNG_ASSERT(ua_sess);
2565 LTTNG_ASSERT(app);
2566
2567 lttng::urcu::read_lock_guard read_lock;
2568
2569 reg_pid = buffer_reg_pid_find(ua_sess->id);
2570 if (!reg_pid) {
2571 /*
2572 * This is the create channel path meaning that if there is NO
2573 * registry available, we have to create one for this session.
2574 */
2575 ret = buffer_reg_pid_create(
2576 ua_sess->id, &reg_pid, ua_sess->root_shm_path, ua_sess->shm_path);
2577 if (ret < 0) {
2578 goto error;
2579 }
2580 } else {
2581 goto end;
2582 }
2583
2584 /* Initialize registry. */
2585 reg_pid->registry->reg.ust = ust_registry_session_per_pid_create(
2586 app,
2587 app->abi,
2588 app->version.major,
2589 app->version.minor,
2590 reg_pid->root_shm_path,
2591 reg_pid->shm_path,
2592 lttng_credentials_get_uid(&ua_sess->effective_credentials),
2593 lttng_credentials_get_gid(&ua_sess->effective_credentials),
2594 ua_sess->tracing_id);
2595 if (!reg_pid->registry->reg.ust) {
2596 /*
2597 * reg_pid->registry->reg.ust is NULL upon error, so we need to
2598 * destroy the buffer registry, because it is always expected
2599 * that if the buffer registry can be found, its ust registry is
2600 * non-NULL.
2601 */
2602 buffer_reg_pid_destroy(reg_pid);
2603 goto error;
2604 }
2605
2606 buffer_reg_pid_add(reg_pid);
2607
2608 DBG3("UST app buffer registry per PID created successfully");
2609
2610 end:
2611 if (regp) {
2612 *regp = reg_pid;
2613 }
2614 error:
2615 return ret;
2616 }
2617
2618 /*
2619 * Setup buffer registry per UID for the given session and application. If none
2620 * is found, a new one is created, added to the global registry and
2621 * initialized. If regp is valid, it's set with the newly created object.
2622 *
2623 * Return 0 on success or else a negative value.
2624 */
2625 static int setup_buffer_reg_uid(struct ltt_ust_session *usess,
2626 struct ust_app_session *ua_sess,
2627 struct ust_app *app,
2628 struct buffer_reg_uid **regp)
2629 {
2630 int ret = 0;
2631 struct buffer_reg_uid *reg_uid;
2632
2633 LTTNG_ASSERT(usess);
2634 LTTNG_ASSERT(app);
2635
2636 lttng::urcu::read_lock_guard read_lock;
2637
2638 reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid);
2639 if (!reg_uid) {
2640 /*
2641 * This is the create channel path meaning that if there is NO
2642 * registry available, we have to create one for this session.
2643 */
2644 ret = buffer_reg_uid_create(usess->id,
2645 app->abi.bits_per_long,
2646 app->uid,
2647 LTTNG_DOMAIN_UST,
2648 &reg_uid,
2649 ua_sess->root_shm_path,
2650 ua_sess->shm_path);
2651 if (ret < 0) {
2652 goto error;
2653 }
2654 } else {
2655 goto end;
2656 }
2657
2658 /* Initialize registry. */
2659 reg_uid->registry->reg.ust = ust_registry_session_per_uid_create(app->abi,
2660 app->version.major,
2661 app->version.minor,
2662 reg_uid->root_shm_path,
2663 reg_uid->shm_path,
2664 usess->uid,
2665 usess->gid,
2666 ua_sess->tracing_id,
2667 app->uid);
2668 if (!reg_uid->registry->reg.ust) {
2669 /*
2670 * reg_uid->registry->reg.ust is NULL upon error, so we need to
2671 * destroy the buffer registry, because it is always expected
2672 * that if the buffer registry can be found, its ust registry is
2673 * non-NULL.
2674 */
2675 buffer_reg_uid_destroy(reg_uid, nullptr);
2676 goto error;
2677 }
2678
2679 /* Add node to teardown list of the session. */
2680 cds_list_add(&reg_uid->lnode, &usess->buffer_reg_uid_list);
2681
2682 buffer_reg_uid_add(reg_uid);
2683
2684 DBG3("UST app buffer registry per UID created successfully");
2685 end:
2686 if (regp) {
2687 *regp = reg_uid;
2688 }
2689 error:
2690 return ret;
2691 }
2692
2693 /*
2694 * Create a session on the tracer side for the given app.
2695 *
2696 * On success, ua_sess_ptr is populated with the session pointer or else left
2697 * untouched. If the session was created, is_created is set to 1. On error,
2698 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
2699 * be NULL.
2700 *
2701 * Returns 0 on success or else a negative code which is either -ENOMEM or
2702 * -ENOTCONN which is the default code if the lttng_ust_ctl_create_session fails.
2703 */
2704 static int find_or_create_ust_app_session(struct ltt_ust_session *usess,
2705 struct ust_app *app,
2706 struct ust_app_session **ua_sess_ptr,
2707 int *is_created)
2708 {
2709 int ret, created = 0;
2710 struct ust_app_session *ua_sess;
2711
2712 LTTNG_ASSERT(usess);
2713 LTTNG_ASSERT(app);
2714 LTTNG_ASSERT(ua_sess_ptr);
2715
2716 health_code_update();
2717
2718 ua_sess = lookup_session_by_app(usess, app);
2719 if (ua_sess == nullptr) {
2720 DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it",
2721 app->pid,
2722 usess->id);
2723 ua_sess = alloc_ust_app_session();
2724 if (ua_sess == nullptr) {
2725 /* Only malloc can failed so something is really wrong */
2726 ret = -ENOMEM;
2727 goto error;
2728 }
2729 shadow_copy_session(ua_sess, usess, app);
2730 created = 1;
2731 }
2732
2733 switch (usess->buffer_type) {
2734 case LTTNG_BUFFER_PER_PID:
2735 /* Init local registry. */
2736 ret = setup_buffer_reg_pid(ua_sess, app, nullptr);
2737 if (ret < 0) {
2738 delete_ust_app_session(-1, ua_sess, app);
2739 goto error;
2740 }
2741 break;
2742 case LTTNG_BUFFER_PER_UID:
2743 /* Look for a global registry. If none exists, create one. */
2744 ret = setup_buffer_reg_uid(usess, ua_sess, app, nullptr);
2745 if (ret < 0) {
2746 delete_ust_app_session(-1, ua_sess, app);
2747 goto error;
2748 }
2749 break;
2750 default:
2751 abort();
2752 ret = -EINVAL;
2753 goto error;
2754 }
2755
2756 health_code_update();
2757
2758 if (ua_sess->handle == -1) {
2759 pthread_mutex_lock(&app->sock_lock);
2760 ret = lttng_ust_ctl_create_session(app->sock);
2761 pthread_mutex_unlock(&app->sock_lock);
2762 if (ret < 0) {
2763 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
2764 DBG("UST app creating session failed. Application is dead: pid = %d, sock = %d",
2765 app->pid,
2766 app->sock);
2767 ret = 0;
2768 } else if (ret == -EAGAIN) {
2769 DBG("UST app creating session failed. Communication time out: pid = %d, sock = %d",
2770 app->pid,
2771 app->sock);
2772 ret = 0;
2773 } else {
2774 ERR("UST app creating session failed with ret %d: pid = %d, sock =%d",
2775 ret,
2776 app->pid,
2777 app->sock);
2778 }
2779 delete_ust_app_session(-1, ua_sess, app);
2780 if (ret != -ENOMEM) {
2781 /*
2782 * Tracer is probably gone or got an internal error so let's
2783 * behave like it will soon unregister or not usable.
2784 */
2785 ret = -ENOTCONN;
2786 }
2787 goto error;
2788 }
2789
2790 ua_sess->handle = ret;
2791
2792 /* Add ust app session to app's HT */
2793 lttng_ht_node_init_u64(&ua_sess->node, ua_sess->tracing_id);
2794 lttng_ht_add_unique_u64(app->sessions, &ua_sess->node);
2795 lttng_ht_node_init_ulong(&ua_sess->ust_objd_node, ua_sess->handle);
2796 lttng_ht_add_unique_ulong(app->ust_sessions_objd, &ua_sess->ust_objd_node);
2797
2798 DBG2("UST app session created successfully with handle %d", ret);
2799 }
2800
2801 *ua_sess_ptr = ua_sess;
2802 if (is_created) {
2803 *is_created = created;
2804 }
2805
2806 /* Everything went well. */
2807 ret = 0;
2808
2809 error:
2810 health_code_update();
2811 return ret;
2812 }
2813
2814 /*
2815 * Match function for a hash table lookup of ust_app_ctx.
2816 *
2817 * It matches an ust app context based on the context type and, in the case
2818 * of perf counters, their name.
2819 */
2820 static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key)
2821 {
2822 struct ust_app_ctx *ctx;
2823 const struct lttng_ust_context_attr *key;
2824
2825 LTTNG_ASSERT(node);
2826 LTTNG_ASSERT(_key);
2827
2828 ctx = caa_container_of(node, struct ust_app_ctx, node.node);
2829 key = (lttng_ust_context_attr *) _key;
2830
2831 /* Context type */
2832 if (ctx->ctx.ctx != key->ctx) {
2833 goto no_match;
2834 }
2835
2836 switch (key->ctx) {
2837 case LTTNG_UST_ABI_CONTEXT_PERF_THREAD_COUNTER:
2838 if (strncmp(key->u.perf_counter.name,
2839 ctx->ctx.u.perf_counter.name,
2840 sizeof(key->u.perf_counter.name)) != 0) {
2841 goto no_match;
2842 }
2843 break;
2844 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
2845 if (strcmp(key->u.app_ctx.provider_name, ctx->ctx.u.app_ctx.provider_name) != 0 ||
2846 strcmp(key->u.app_ctx.ctx_name, ctx->ctx.u.app_ctx.ctx_name) != 0) {
2847 goto no_match;
2848 }
2849 break;
2850 default:
2851 break;
2852 }
2853
2854 /* Match. */
2855 return 1;
2856
2857 no_match:
2858 return 0;
2859 }
2860
2861 /*
2862 * Lookup for an ust app context from an lttng_ust_context.
2863 *
2864 * Must be called while holding RCU read side lock.
2865 * Return an ust_app_ctx object or NULL on error.
2866 */
2867 static struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht,
2868 struct lttng_ust_context_attr *uctx)
2869 {
2870 struct lttng_ht_iter iter;
2871 struct lttng_ht_node_ulong *node;
2872 struct ust_app_ctx *app_ctx = nullptr;
2873
2874 LTTNG_ASSERT(uctx);
2875 LTTNG_ASSERT(ht);
2876 ASSERT_RCU_READ_LOCKED();
2877
2878 /* Lookup using the lttng_ust_context_type and a custom match fct. */
2879 cds_lfht_lookup(ht->ht,
2880 ht->hash_fct((void *) uctx->ctx, lttng_ht_seed),
2881 ht_match_ust_app_ctx,
2882 uctx,
2883 &iter.iter);
2884 node = lttng_ht_iter_get_node_ulong(&iter);
2885 if (!node) {
2886 goto end;
2887 }
2888
2889 app_ctx = lttng::utils::container_of(node, &ust_app_ctx::node);
2890
2891 end:
2892 return app_ctx;
2893 }
2894
2895 /*
2896 * Create a context for the channel on the tracer.
2897 *
2898 * Called with UST app session lock held and a RCU read side lock.
2899 */
2900 static int create_ust_app_channel_context(struct ust_app_channel *ua_chan,
2901 struct lttng_ust_context_attr *uctx,
2902 struct ust_app *app)
2903 {
2904 int ret = 0;
2905 struct ust_app_ctx *ua_ctx;
2906
2907 ASSERT_RCU_READ_LOCKED();
2908
2909 DBG2("UST app adding context to channel %s", ua_chan->name);
2910
2911 ua_ctx = find_ust_app_context(ua_chan->ctx, uctx);
2912 if (ua_ctx) {
2913 ret = -EEXIST;
2914 goto error;
2915 }
2916
2917 ua_ctx = alloc_ust_app_ctx(uctx);
2918 if (ua_ctx == nullptr) {
2919 /* malloc failed */
2920 ret = -ENOMEM;
2921 goto error;
2922 }
2923
2924 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
2925 lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node);
2926 cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list);
2927
2928 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2929 if (ret < 0) {
2930 goto error;
2931 }
2932
2933 error:
2934 return ret;
2935 }
2936
2937 /*
2938 * Enable on the tracer side a ust app event for the session and channel.
2939 *
2940 * Called with UST app session lock held.
2941 */
2942 static int enable_ust_app_event(struct ust_app_event *ua_event, struct ust_app *app)
2943 {
2944 int ret;
2945
2946 ret = enable_ust_object(app, ua_event->obj);
2947 if (ret < 0) {
2948 goto error;
2949 }
2950
2951 ua_event->enabled = true;
2952
2953 error:
2954 return ret;
2955 }
2956
2957 /*
2958 * Disable on the tracer side a ust app event for the session and channel.
2959 */
2960 static int disable_ust_app_event(struct ust_app_event *ua_event, struct ust_app *app)
2961 {
2962 int ret;
2963
2964 ret = disable_ust_object(app, ua_event->obj);
2965 if (ret < 0) {
2966 goto error;
2967 }
2968
2969 ua_event->enabled = false;
2970
2971 error:
2972 return ret;
2973 }
2974
2975 /*
2976 * Lookup ust app channel for session and disable it on the tracer side.
2977 */
2978 static int disable_ust_app_channel(struct ust_app_session *ua_sess,
2979 struct ust_app_channel *ua_chan,
2980 struct ust_app *app)
2981 {
2982 int ret;
2983
2984 ret = disable_ust_channel(app, ua_sess, ua_chan);
2985 if (ret < 0) {
2986 goto error;
2987 }
2988
2989 ua_chan->enabled = false;
2990
2991 error:
2992 return ret;
2993 }
2994
2995 /*
2996 * Lookup ust app channel for session and enable it on the tracer side. This
2997 * MUST be called with a RCU read side lock acquired.
2998 */
2999 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
3000 struct ltt_ust_channel *uchan,
3001 struct ust_app *app)
3002 {
3003 int ret = 0;
3004 struct lttng_ht_iter iter;
3005 struct lttng_ht_node_str *ua_chan_node;
3006 struct ust_app_channel *ua_chan;
3007
3008 ASSERT_RCU_READ_LOCKED();
3009
3010 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
3011 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3012 if (ua_chan_node == nullptr) {
3013 DBG2("Unable to find channel %s in ust session id %" PRIu64,
3014 uchan->name,
3015 ua_sess->tracing_id);
3016 goto error;
3017 }
3018
3019 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
3020
3021 ret = enable_ust_channel(app, ua_sess, ua_chan);
3022 if (ret < 0) {
3023 goto error;
3024 }
3025
3026 error:
3027 return ret;
3028 }
3029
3030 /*
3031 * Ask the consumer to create a channel and get it if successful.
3032 *
3033 * Called with UST app session lock held.
3034 *
3035 * Return 0 on success or else a negative value.
3036 */
3037 static int do_consumer_create_channel(struct ltt_ust_session *usess,
3038 struct ust_app_session *ua_sess,
3039 struct ust_app_channel *ua_chan,
3040 int bitness,
3041 lsu::registry_session *registry)
3042 {
3043 int ret;
3044 unsigned int nb_fd = 0;
3045 struct consumer_socket *socket;
3046
3047 LTTNG_ASSERT(usess);
3048 LTTNG_ASSERT(ua_sess);
3049 LTTNG_ASSERT(ua_chan);
3050 LTTNG_ASSERT(registry);
3051
3052 lttng::urcu::read_lock_guard read_lock;
3053 health_code_update();
3054
3055 /* Get the right consumer socket for the application. */
3056 socket = consumer_find_socket_by_bitness(bitness, usess->consumer);
3057 if (!socket) {
3058 ret = -EINVAL;
3059 goto error;
3060 }
3061
3062 health_code_update();
3063
3064 /* Need one fd for the channel. */
3065 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3066 if (ret < 0) {
3067 ERR("Exhausted number of available FD upon create channel");
3068 goto error;
3069 }
3070
3071 /*
3072 * Ask consumer to create channel. The consumer will return the number of
3073 * stream we have to expect.
3074 */
3075 ret = ust_consumer_ask_channel(
3076 ua_sess, ua_chan, usess->consumer, socket, registry, usess->current_trace_chunk);
3077 if (ret < 0) {
3078 goto error_ask;
3079 }
3080
3081 /*
3082 * Compute the number of fd needed before receiving them. It must be 2 per
3083 * stream (2 being the default value here).
3084 */
3085 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
3086
3087 /* Reserve the amount of file descriptor we need. */
3088 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
3089 if (ret < 0) {
3090 ERR("Exhausted number of available FD upon create channel");
3091 goto error_fd_get_stream;
3092 }
3093
3094 health_code_update();
3095
3096 /*
3097 * Now get the channel from the consumer. This call will populate the stream
3098 * list of that channel and set the ust objects.
3099 */
3100 if (usess->consumer->enabled) {
3101 ret = ust_consumer_get_channel(socket, ua_chan);
3102 if (ret < 0) {
3103 goto error_destroy;
3104 }
3105 }
3106
3107 return 0;
3108
3109 error_destroy:
3110 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
3111 error_fd_get_stream:
3112 /*
3113 * Initiate a destroy channel on the consumer since we had an error
3114 * handling it on our side. The return value is of no importance since we
3115 * already have a ret value set by the previous error that we need to
3116 * return.
3117 */
3118 (void) ust_consumer_destroy_channel(socket, ua_chan);
3119 error_ask:
3120 lttng_fd_put(LTTNG_FD_APPS, 1);
3121 error:
3122 health_code_update();
3123 return ret;
3124 }
3125
3126 /*
3127 * Duplicate the ust data object of the ust app stream and save it in the
3128 * buffer registry stream.
3129 *
3130 * Return 0 on success or else a negative value.
3131 */
3132 static int duplicate_stream_object(struct buffer_reg_stream *reg_stream,
3133 struct ust_app_stream *stream)
3134 {
3135 int ret;
3136
3137 LTTNG_ASSERT(reg_stream);
3138 LTTNG_ASSERT(stream);
3139
3140 /* Duplicating a stream requires 2 new fds. Reserve them. */
3141 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
3142 if (ret < 0) {
3143 ERR("Exhausted number of available FD upon duplicate stream");
3144 goto error;
3145 }
3146
3147 /* Duplicate object for stream once the original is in the registry. */
3148 ret = lttng_ust_ctl_duplicate_ust_object_data(&stream->obj, reg_stream->obj.ust);
3149 if (ret < 0) {
3150 ERR("Duplicate stream obj from %p to %p failed with ret %d",
3151 reg_stream->obj.ust,
3152 stream->obj,
3153 ret);
3154 lttng_fd_put(LTTNG_FD_APPS, 2);
3155 goto error;
3156 }
3157 stream->handle = stream->obj->handle;
3158
3159 error:
3160 return ret;
3161 }
3162
3163 /*
3164 * Duplicate the ust data object of the ust app. channel and save it in the
3165 * buffer registry channel.
3166 *
3167 * Return 0 on success or else a negative value.
3168 */
3169 static int duplicate_channel_object(struct buffer_reg_channel *buf_reg_chan,
3170 struct ust_app_channel *ua_chan)
3171 {
3172 int ret;
3173
3174 LTTNG_ASSERT(buf_reg_chan);
3175 LTTNG_ASSERT(ua_chan);
3176
3177 /* Duplicating a channel requires 1 new fd. Reserve it. */
3178 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3179 if (ret < 0) {
3180 ERR("Exhausted number of available FD upon duplicate channel");
3181 goto error_fd_get;
3182 }
3183
3184 /* Duplicate object for stream once the original is in the registry. */
3185 ret = lttng_ust_ctl_duplicate_ust_object_data(&ua_chan->obj, buf_reg_chan->obj.ust);
3186 if (ret < 0) {
3187 ERR("Duplicate channel obj from %p to %p failed with ret: %d",
3188 buf_reg_chan->obj.ust,
3189 ua_chan->obj,
3190 ret);
3191 goto error;
3192 }
3193 ua_chan->handle = ua_chan->obj->handle;
3194
3195 return 0;
3196
3197 error:
3198 lttng_fd_put(LTTNG_FD_APPS, 1);
3199 error_fd_get:
3200 return ret;
3201 }
3202
3203 /*
3204 * For a given channel buffer registry, setup all streams of the given ust
3205 * application channel.
3206 *
3207 * Return 0 on success or else a negative value.
3208 */
3209 static int setup_buffer_reg_streams(struct buffer_reg_channel *buf_reg_chan,
3210 struct ust_app_channel *ua_chan,
3211 struct ust_app *app)
3212 {
3213 int ret = 0;
3214 struct ust_app_stream *stream, *stmp;
3215
3216 LTTNG_ASSERT(buf_reg_chan);
3217 LTTNG_ASSERT(ua_chan);
3218
3219 DBG2("UST app setup buffer registry stream");
3220
3221 /* Send all streams to application. */
3222 cds_list_for_each_entry_safe (stream, stmp, &ua_chan->streams.head, list) {
3223 struct buffer_reg_stream *reg_stream;
3224
3225 ret = buffer_reg_stream_create(&reg_stream);
3226 if (ret < 0) {
3227 goto error;
3228 }
3229
3230 /*
3231 * Keep original pointer and nullify it in the stream so the delete
3232 * stream call does not release the object.
3233 */
3234 reg_stream->obj.ust = stream->obj;
3235 stream->obj = nullptr;
3236 buffer_reg_stream_add(reg_stream, buf_reg_chan);
3237
3238 /* We don't need the streams anymore. */
3239 cds_list_del(&stream->list);
3240 delete_ust_app_stream(-1, stream, app);
3241 }
3242
3243 error:
3244 return ret;
3245 }
3246
3247 /*
3248 * Create a buffer registry channel for the given session registry and
3249 * application channel object. If regp pointer is valid, it's set with the
3250 * created object. Important, the created object is NOT added to the session
3251 * registry hash table.
3252 *
3253 * Return 0 on success else a negative value.
3254 */
3255 static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3256 struct ust_app_channel *ua_chan,
3257 struct buffer_reg_channel **regp)
3258 {
3259 int ret;
3260 struct buffer_reg_channel *buf_reg_chan = nullptr;
3261
3262 LTTNG_ASSERT(reg_sess);
3263 LTTNG_ASSERT(ua_chan);
3264
3265 DBG2("UST app creating buffer registry channel for %s", ua_chan->name);
3266
3267 /* Create buffer registry channel. */
3268 ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, &buf_reg_chan);
3269 if (ret < 0) {
3270 goto error_create;
3271 }
3272 LTTNG_ASSERT(buf_reg_chan);
3273 buf_reg_chan->consumer_key = ua_chan->key;
3274 buf_reg_chan->subbuf_size = ua_chan->attr.subbuf_size;
3275 buf_reg_chan->num_subbuf = ua_chan->attr.num_subbuf;
3276
3277 /* Create and add a channel registry to session. */
3278 try {
3279 reg_sess->reg.ust->add_channel(ua_chan->tracing_channel_id);
3280 } catch (const std::exception& ex) {
3281 ERR("Failed to add a channel registry to userspace registry session: %s",
3282 ex.what());
3283 ret = -1;
3284 goto error;
3285 }
3286
3287 buffer_reg_channel_add(reg_sess, buf_reg_chan);
3288
3289 if (regp) {
3290 *regp = buf_reg_chan;
3291 }
3292
3293 return 0;
3294
3295 error:
3296 /* Safe because the registry channel object was not added to any HT. */
3297 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3298 error_create:
3299 return ret;
3300 }
3301
3302 /*
3303 * Setup buffer registry channel for the given session registry and application
3304 * channel object. If regp pointer is valid, it's set with the created object.
3305 *
3306 * Return 0 on success else a negative value.
3307 */
3308 static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess,
3309 struct ust_app_channel *ua_chan,
3310 struct buffer_reg_channel *buf_reg_chan,
3311 struct ust_app *app)
3312 {
3313 int ret;
3314
3315 LTTNG_ASSERT(reg_sess);
3316 LTTNG_ASSERT(buf_reg_chan);
3317 LTTNG_ASSERT(ua_chan);
3318 LTTNG_ASSERT(ua_chan->obj);
3319
3320 DBG2("UST app setup buffer registry channel for %s", ua_chan->name);
3321
3322 /* Setup all streams for the registry. */
3323 ret = setup_buffer_reg_streams(buf_reg_chan, ua_chan, app);
3324 if (ret < 0) {
3325 goto error;
3326 }
3327
3328 buf_reg_chan->obj.ust = ua_chan->obj;
3329 ua_chan->obj = nullptr;
3330
3331 return 0;
3332
3333 error:
3334 buffer_reg_channel_remove(reg_sess, buf_reg_chan);
3335 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3336 return ret;
3337 }
3338
3339 /*
3340 * Send buffer registry channel to the application.
3341 *
3342 * Return 0 on success else a negative value.
3343 */
3344 static int send_channel_uid_to_ust(struct buffer_reg_channel *buf_reg_chan,
3345 struct ust_app *app,
3346 struct ust_app_session *ua_sess,
3347 struct ust_app_channel *ua_chan)
3348 {
3349 int ret;
3350 struct buffer_reg_stream *reg_stream;
3351
3352 LTTNG_ASSERT(buf_reg_chan);
3353 LTTNG_ASSERT(app);
3354 LTTNG_ASSERT(ua_sess);
3355 LTTNG_ASSERT(ua_chan);
3356
3357 DBG("UST app sending buffer registry channel to ust sock %d", app->sock);
3358
3359 ret = duplicate_channel_object(buf_reg_chan, ua_chan);
3360 if (ret < 0) {
3361 goto error;
3362 }
3363
3364 /* Send channel to the application. */
3365 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
3366 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3367 ret = -ENOTCONN; /* Caused by app exiting. */
3368 goto error;
3369 } else if (ret == -EAGAIN) {
3370 /* Caused by timeout. */
3371 WARN("Communication with application %d timed out on send_channel for channel \"%s\" of session \"%" PRIu64
3372 "\".",
3373 app->pid,
3374 ua_chan->name,
3375 ua_sess->tracing_id);
3376 /* Treat this the same way as an application that is exiting. */
3377 ret = -ENOTCONN;
3378 goto error;
3379 } else if (ret < 0) {
3380 goto error;
3381 }
3382
3383 health_code_update();
3384
3385 /* Send all streams to application. */
3386 pthread_mutex_lock(&buf_reg_chan->stream_list_lock);
3387 cds_list_for_each_entry (reg_stream, &buf_reg_chan->streams, lnode) {
3388 struct ust_app_stream stream = {};
3389
3390 ret = duplicate_stream_object(reg_stream, &stream);
3391 if (ret < 0) {
3392 goto error_stream_unlock;
3393 }
3394
3395 ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream);
3396 if (ret < 0) {
3397 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
3398 ret = -ENOTCONN; /* Caused by app exiting. */
3399 } else if (ret == -EAGAIN) {
3400 /*
3401 * Caused by timeout.
3402 * Treat this the same way as an application
3403 * that is exiting.
3404 */
3405 WARN("Communication with application %d timed out on send_stream for stream of channel \"%s\" of session \"%" PRIu64
3406 "\".",
3407 app->pid,
3408 ua_chan->name,
3409 ua_sess->tracing_id);
3410 ret = -ENOTCONN;
3411 }
3412 (void) release_ust_app_stream(-1, &stream, app);
3413 goto error_stream_unlock;
3414 }
3415
3416 /*
3417 * The return value is not important here. This function will output an
3418 * error if needed.
3419 */
3420 (void) release_ust_app_stream(-1, &stream, app);
3421 }
3422
3423 error_stream_unlock:
3424 pthread_mutex_unlock(&buf_reg_chan->stream_list_lock);
3425 error:
3426 return ret;
3427 }
3428
3429 /*
3430 * Create and send to the application the created buffers with per UID buffers.
3431 *
3432 * This MUST be called with a RCU read side lock acquired.
3433 * The session list lock and the session's lock must be acquired.
3434 *
3435 * Return 0 on success else a negative value.
3436 */
3437 static int create_channel_per_uid(struct ust_app *app,
3438 struct ltt_ust_session *usess,
3439 struct ust_app_session *ua_sess,
3440 struct ust_app_channel *ua_chan)
3441 {
3442 int ret;
3443 struct buffer_reg_uid *reg_uid;
3444 struct buffer_reg_channel *buf_reg_chan;
3445 struct ltt_session *session = nullptr;
3446 enum lttng_error_code notification_ret;
3447
3448 LTTNG_ASSERT(app);
3449 LTTNG_ASSERT(usess);
3450 LTTNG_ASSERT(ua_sess);
3451 LTTNG_ASSERT(ua_chan);
3452 ASSERT_RCU_READ_LOCKED();
3453
3454 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
3455
3456 reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid);
3457 /*
3458 * The session creation handles the creation of this global registry
3459 * object. If none can be find, there is a code flow problem or a
3460 * teardown race.
3461 */
3462 LTTNG_ASSERT(reg_uid);
3463
3464 buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, reg_uid);
3465 if (buf_reg_chan) {
3466 goto send_channel;
3467 }
3468
3469 /* Create the buffer registry channel object. */
3470 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan);
3471 if (ret < 0) {
3472 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan->name);
3473 goto error;
3474 }
3475
3476 session = session_find_by_id(ua_sess->tracing_id);
3477 LTTNG_ASSERT(session);
3478 ASSERT_LOCKED(session->lock);
3479 ASSERT_SESSION_LIST_LOCKED();
3480
3481 /*
3482 * Create the buffers on the consumer side. This call populates the
3483 * ust app channel object with all streams and data object.
3484 */
3485 ret = do_consumer_create_channel(
3486 usess, ua_sess, ua_chan, app->abi.bits_per_long, reg_uid->registry->reg.ust);
3487 if (ret < 0) {
3488 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name);
3489
3490 /*
3491 * Let's remove the previously created buffer registry channel so
3492 * it's not visible anymore in the session registry.
3493 */
3494 auto locked_registry = reg_uid->registry->reg.ust->lock();
3495 try {
3496 locked_registry->remove_channel(ua_chan->tracing_channel_id, false);
3497 } catch (const std::exception& ex) {
3498 DBG("Could not find channel for removal: %s", ex.what());
3499 }
3500 buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan);
3501 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3502 goto error;
3503 }
3504
3505 /*
3506 * Setup the streams and add it to the session registry.
3507 */
3508 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, buf_reg_chan, app);
3509 if (ret < 0) {
3510 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
3511 goto error;
3512 }
3513
3514 {
3515 auto locked_registry = reg_uid->registry->reg.ust->lock();
3516 auto& ust_reg_chan = locked_registry->channel(ua_chan->tracing_channel_id);
3517
3518 ust_reg_chan._consumer_key = ua_chan->key;
3519 }
3520
3521 /* Notify the notification subsystem of the channel's creation. */
3522 notification_ret = notification_thread_command_add_channel(
3523 the_notification_thread_handle,
3524 session->id,
3525 ua_chan->name,
3526 ua_chan->key,
3527 LTTNG_DOMAIN_UST,
3528 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3529 if (notification_ret != LTTNG_OK) {
3530 ret = -(int) notification_ret;
3531 ERR("Failed to add channel to notification thread");
3532 goto error;
3533 }
3534
3535 send_channel:
3536 /* Send buffers to the application. */
3537 ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan);
3538 if (ret < 0) {
3539 if (ret != -ENOTCONN) {
3540 ERR("Error sending channel to application");
3541 }
3542 goto error;
3543 }
3544
3545 error:
3546 if (session) {
3547 session_put(session);
3548 }
3549 return ret;
3550 }
3551
3552 /*
3553 * Create and send to the application the created buffers with per PID buffers.
3554 *
3555 * Called with UST app session lock held.
3556 * The session list lock and the session's lock must be acquired.
3557 *
3558 * Return 0 on success else a negative value.
3559 */
3560 static int create_channel_per_pid(struct ust_app *app,
3561 struct ltt_ust_session *usess,
3562 struct ust_app_session *ua_sess,
3563 struct ust_app_channel *ua_chan)
3564 {
3565 int ret;
3566 lsu::registry_session *registry;
3567 enum lttng_error_code cmd_ret;
3568 struct ltt_session *session = nullptr;
3569 uint64_t chan_reg_key;
3570
3571 LTTNG_ASSERT(app);
3572 LTTNG_ASSERT(usess);
3573 LTTNG_ASSERT(ua_sess);
3574 LTTNG_ASSERT(ua_chan);
3575
3576 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3577
3578 lttng::urcu::read_lock_guard read_lock;
3579
3580 registry = get_session_registry(ua_sess);
3581 /* The UST app session lock is held, registry shall not be null. */
3582 LTTNG_ASSERT(registry);
3583
3584 /* Create and add a new channel registry to session. */
3585 try {
3586 registry->add_channel(ua_chan->key);
3587 } catch (const std::exception& ex) {
3588 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3589 ua_chan->name,
3590 ex.what());
3591 ret = -1;
3592 goto error;
3593 }
3594
3595 session = session_find_by_id(ua_sess->tracing_id);
3596 LTTNG_ASSERT(session);
3597 ASSERT_LOCKED(session->lock);
3598 ASSERT_SESSION_LIST_LOCKED();
3599
3600 /* Create and get channel on the consumer side. */
3601 ret = do_consumer_create_channel(usess, ua_sess, ua_chan, app->abi.bits_per_long, registry);
3602 if (ret < 0) {
3603 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name);
3604 goto error_remove_from_registry;
3605 }
3606
3607 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3608 if (ret < 0) {
3609 if (ret != -ENOTCONN) {
3610 ERR("Error sending channel to application");
3611 }
3612 goto error_remove_from_registry;
3613 }
3614
3615 chan_reg_key = ua_chan->key;
3616 {
3617 auto locked_registry = registry->lock();
3618
3619 auto& ust_reg_chan = locked_registry->channel(chan_reg_key);
3620 ust_reg_chan._consumer_key = ua_chan->key;
3621 }
3622
3623 cmd_ret = notification_thread_command_add_channel(the_notification_thread_handle,
3624 session->id,
3625 ua_chan->name,
3626 ua_chan->key,
3627 LTTNG_DOMAIN_UST,
3628 ua_chan->attr.subbuf_size *
3629 ua_chan->attr.num_subbuf);
3630 if (cmd_ret != LTTNG_OK) {
3631 ret = -(int) cmd_ret;
3632 ERR("Failed to add channel to notification thread");
3633 goto error_remove_from_registry;
3634 }
3635
3636 error_remove_from_registry:
3637 if (ret) {
3638 try {
3639 auto locked_registry = registry->lock();
3640 locked_registry->remove_channel(ua_chan->key, false);
3641 } catch (const std::exception& ex) {
3642 DBG("Could not find channel for removal: %s", ex.what());
3643 }
3644 }
3645 error:
3646 if (session) {
3647 session_put(session);
3648 }
3649 return ret;
3650 }
3651
3652 /*
3653 * From an already allocated ust app channel, create the channel buffers if
3654 * needed and send them to the application. This MUST be called with a RCU read
3655 * side lock acquired.
3656 *
3657 * Called with UST app session lock held.
3658 *
3659 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3660 * the application exited concurrently.
3661 */
3662 static int ust_app_channel_send(struct ust_app *app,
3663 struct ltt_ust_session *usess,
3664 struct ust_app_session *ua_sess,
3665 struct ust_app_channel *ua_chan)
3666 {
3667 int ret;
3668
3669 LTTNG_ASSERT(app);
3670 LTTNG_ASSERT(usess);
3671 LTTNG_ASSERT(usess->active);
3672 LTTNG_ASSERT(ua_sess);
3673 LTTNG_ASSERT(ua_chan);
3674 ASSERT_RCU_READ_LOCKED();
3675
3676 /* Handle buffer type before sending the channel to the application. */
3677 switch (usess->buffer_type) {
3678 case LTTNG_BUFFER_PER_UID:
3679 {
3680 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3681 if (ret < 0) {
3682 goto error;
3683 }
3684 break;
3685 }
3686 case LTTNG_BUFFER_PER_PID:
3687 {
3688 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3689 if (ret < 0) {
3690 goto error;
3691 }
3692 break;
3693 }
3694 default:
3695 abort();
3696 ret = -EINVAL;
3697 goto error;
3698 }
3699
3700 /* Initialize ust objd object using the received handle and add it. */
3701 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3702 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
3703
3704 /* If channel is not enabled, disable it on the tracer */
3705 if (!ua_chan->enabled) {
3706 ret = disable_ust_channel(app, ua_sess, ua_chan);
3707 if (ret < 0) {
3708 goto error;
3709 }
3710 }
3711
3712 error:
3713 return ret;
3714 }
3715
3716 /*
3717 * Create UST app channel and return it through ua_chanp if not NULL.
3718 *
3719 * Called with UST app session lock and RCU read-side lock held.
3720 *
3721 * Return 0 on success or else a negative value.
3722 */
3723 static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3724 struct ltt_ust_channel *uchan,
3725 enum lttng_ust_abi_chan_type type,
3726 struct ltt_ust_session *usess __attribute__((unused)),
3727 struct ust_app_channel **ua_chanp)
3728 {
3729 int ret = 0;
3730 struct lttng_ht_iter iter;
3731 struct lttng_ht_node_str *ua_chan_node;
3732 struct ust_app_channel *ua_chan;
3733
3734 ASSERT_RCU_READ_LOCKED();
3735
3736 /* Lookup channel in the ust app session */
3737 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
3738 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3739 if (ua_chan_node != nullptr) {
3740 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
3741 goto end;
3742 }
3743
3744 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
3745 if (ua_chan == nullptr) {
3746 /* Only malloc can fail here */
3747 ret = -ENOMEM;
3748 goto error;
3749 }
3750 shadow_copy_channel(ua_chan, uchan);
3751
3752 /* Set channel type. */
3753 ua_chan->attr.type = type;
3754
3755 /* Only add the channel if successful on the tracer side. */
3756 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3757 end:
3758 if (ua_chanp) {
3759 *ua_chanp = ua_chan;
3760 }
3761
3762 /* Everything went well. */
3763 return 0;
3764
3765 error:
3766 return ret;
3767 }
3768
3769 /*
3770 * Create UST app event and create it on the tracer side.
3771 *
3772 * Must be called with the RCU read side lock held.
3773 * Called with ust app session mutex held.
3774 */
3775 static int create_ust_app_event(struct ust_app_channel *ua_chan,
3776 struct ltt_ust_event *uevent,
3777 struct ust_app *app)
3778 {
3779 int ret = 0;
3780 struct ust_app_event *ua_event;
3781
3782 ASSERT_RCU_READ_LOCKED();
3783
3784 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3785 if (ua_event == nullptr) {
3786 /* Only failure mode of alloc_ust_app_event(). */
3787 ret = -ENOMEM;
3788 goto end;
3789 }
3790 shadow_copy_event(ua_event, uevent);
3791
3792 /* Create it on the tracer side */
3793 ret = create_ust_event(app, ua_chan, ua_event);
3794 if (ret < 0) {
3795 /*
3796 * Not found previously means that it does not exist on the
3797 * tracer. If the application reports that the event existed,
3798 * it means there is a bug in the sessiond or lttng-ust
3799 * (or corruption, etc.)
3800 */
3801 if (ret == -LTTNG_UST_ERR_EXIST) {
3802 ERR("Tracer for application reported that an event being created already existed: "
3803 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3804 uevent->attr.name,
3805 app->pid,
3806 app->ppid,
3807 app->uid,
3808 app->gid);
3809 }
3810 goto error;
3811 }
3812
3813 add_unique_ust_app_event(ua_chan, ua_event);
3814
3815 DBG2("UST app create event completed: app = '%s' pid = %d", app->name, app->pid);
3816
3817 end:
3818 return ret;
3819
3820 error:
3821 /* Valid. Calling here is already in a read side lock */
3822 delete_ust_app_event(-1, ua_event, app);
3823 return ret;
3824 }
3825
3826 /*
3827 * Create UST app event notifier rule and create it on the tracer side.
3828 *
3829 * Must be called with the RCU read side lock held.
3830 * Called with ust app session mutex held.
3831 */
3832 static int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, struct ust_app *app)
3833 {
3834 int ret = 0;
3835 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
3836
3837 ASSERT_RCU_READ_LOCKED();
3838
3839 ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger);
3840 if (ua_event_notifier_rule == nullptr) {
3841 ret = -ENOMEM;
3842 goto end;
3843 }
3844
3845 /* Create it on the tracer side. */
3846 ret = create_ust_event_notifier(app, ua_event_notifier_rule);
3847 if (ret < 0) {
3848 /*
3849 * Not found previously means that it does not exist on the
3850 * tracer. If the application reports that the event existed,
3851 * it means there is a bug in the sessiond or lttng-ust
3852 * (or corruption, etc.)
3853 */
3854 if (ret == -LTTNG_UST_ERR_EXIST) {
3855 ERR("Tracer for application reported that an event notifier being created already exists: "
3856 "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d",
3857 lttng_trigger_get_tracer_token(trigger),
3858 app->pid,
3859 app->ppid,
3860 app->uid,
3861 app->gid);
3862 }
3863 goto error;
3864 }
3865
3866 lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht,
3867 &ua_event_notifier_rule->node);
3868
3869 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64,
3870 app->name,
3871 app->pid,
3872 lttng_trigger_get_tracer_token(trigger));
3873
3874 goto end;
3875
3876 error:
3877 /* The RCU read side lock is already being held by the caller. */
3878 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app);
3879 end:
3880 return ret;
3881 }
3882
3883 /*
3884 * Create UST metadata and open it on the tracer side.
3885 *
3886 * Called with UST app session lock held and RCU read side lock.
3887 */
3888 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
3889 struct ust_app *app,
3890 struct consumer_output *consumer)
3891 {
3892 int ret = 0;
3893 struct ust_app_channel *metadata;
3894 struct consumer_socket *socket;
3895 struct ltt_session *session = nullptr;
3896
3897 LTTNG_ASSERT(ua_sess);
3898 LTTNG_ASSERT(app);
3899 LTTNG_ASSERT(consumer);
3900 ASSERT_RCU_READ_LOCKED();
3901
3902 auto locked_registry = get_locked_session_registry(ua_sess);
3903 /* The UST app session is held registry shall not be null. */
3904 LTTNG_ASSERT(locked_registry);
3905
3906 /* Metadata already exists for this registry or it was closed previously */
3907 if (locked_registry->_metadata_key || locked_registry->_metadata_closed) {
3908 ret = 0;
3909 goto error;
3910 }
3911
3912 /* Allocate UST metadata */
3913 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, nullptr);
3914 if (!metadata) {
3915 /* malloc() failed */
3916 ret = -ENOMEM;
3917 goto error;
3918 }
3919
3920 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3921
3922 /* Need one fd for the channel. */
3923 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3924 if (ret < 0) {
3925 ERR("Exhausted number of available FD upon create metadata");
3926 goto error;
3927 }
3928
3929 /* Get the right consumer socket for the application. */
3930 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, consumer);
3931 if (!socket) {
3932 ret = -EINVAL;
3933 goto error_consumer;
3934 }
3935
3936 /*
3937 * Keep metadata key so we can identify it on the consumer side. Assign it
3938 * to the registry *before* we ask the consumer so we avoid the race of the
3939 * consumer requesting the metadata and the ask_channel call on our side
3940 * did not returned yet.
3941 */
3942 locked_registry->_metadata_key = metadata->key;
3943
3944 session = session_find_by_id(ua_sess->tracing_id);
3945 LTTNG_ASSERT(session);
3946 ASSERT_LOCKED(session->lock);
3947 ASSERT_SESSION_LIST_LOCKED();
3948
3949 /*
3950 * Ask the metadata channel creation to the consumer. The metadata object
3951 * will be created by the consumer and kept their. However, the stream is
3952 * never added or monitored until we do a first push metadata to the
3953 * consumer.
3954 */
3955 ret = ust_consumer_ask_channel(ua_sess,
3956 metadata,
3957 consumer,
3958 socket,
3959 locked_registry.get(),
3960 session->current_trace_chunk);
3961 if (ret < 0) {
3962 /* Nullify the metadata key so we don't try to close it later on. */
3963 locked_registry->_metadata_key = 0;
3964 goto error_consumer;
3965 }
3966
3967 /*
3968 * The setup command will make the metadata stream be sent to the relayd,
3969 * if applicable, and the thread managing the metadatas. This is important
3970 * because after this point, if an error occurs, the only way the stream
3971 * can be deleted is to be monitored in the consumer.
3972 */
3973 ret = consumer_setup_metadata(socket, metadata->key);
3974 if (ret < 0) {
3975 /* Nullify the metadata key so we don't try to close it later on. */
3976 locked_registry->_metadata_key = 0;
3977 goto error_consumer;
3978 }
3979
3980 DBG2("UST metadata with key %" PRIu64 " created for app pid %d", metadata->key, app->pid);
3981
3982 error_consumer:
3983 lttng_fd_put(LTTNG_FD_APPS, 1);
3984 delete_ust_app_channel(-1, metadata, app, locked_registry);
3985 error:
3986 if (session) {
3987 session_put(session);
3988 }
3989 return ret;
3990 }
3991
3992 /*
3993 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3994 * acquired before calling this function.
3995 */
3996 struct ust_app *ust_app_find_by_pid(pid_t pid)
3997 {
3998 struct ust_app *app = nullptr;
3999 struct lttng_ht_node_ulong *node;
4000 struct lttng_ht_iter iter;
4001
4002 lttng_ht_lookup(ust_app_ht, (void *) ((unsigned long) pid), &iter);
4003 node = lttng_ht_iter_get_node_ulong(&iter);
4004 if (node == nullptr) {
4005 DBG2("UST app no found with pid %d", pid);
4006 goto error;
4007 }
4008
4009 DBG2("Found UST app by pid %d", pid);
4010
4011 app = lttng::utils::container_of(node, &ust_app::pid_n);
4012
4013 error:
4014 return app;
4015 }
4016
4017 /*
4018 * Allocate and init an UST app object using the registration information and
4019 * the command socket. This is called when the command socket connects to the
4020 * session daemon.
4021 *
4022 * The object is returned on success or else NULL.
4023 */
4024 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
4025 {
4026 int ret;
4027 struct ust_app *lta = nullptr;
4028 struct lttng_pipe *event_notifier_event_source_pipe = nullptr;
4029
4030 LTTNG_ASSERT(msg);
4031 LTTNG_ASSERT(sock >= 0);
4032
4033 DBG3("UST app creating application for socket %d", sock);
4034
4035 if ((msg->bits_per_long == 64 && (uatomic_read(&the_ust_consumerd64_fd) == -EINVAL)) ||
4036 (msg->bits_per_long == 32 && (uatomic_read(&the_ust_consumerd32_fd) == -EINVAL))) {
4037 ERR("Registration failed: application \"%s\" (pid: %d) has "
4038 "%d-bit long, but no consumerd for this size is available.\n",
4039 msg->name,
4040 msg->pid,
4041 msg->bits_per_long);
4042 goto error;
4043 }
4044
4045 /*
4046 * Reserve the two file descriptors of the event source pipe. The write
4047 * end will be closed once it is passed to the application, at which
4048 * point a single 'put' will be performed.
4049 */
4050 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
4051 if (ret) {
4052 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4053 msg->name,
4054 (int) msg->pid);
4055 goto error;
4056 }
4057
4058 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
4059 if (!event_notifier_event_source_pipe) {
4060 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4061 msg->name,
4062 msg->pid);
4063 goto error;
4064 }
4065
4066 lta = zmalloc<ust_app>();
4067 if (lta == nullptr) {
4068 PERROR("malloc");
4069 goto error_free_pipe;
4070 }
4071
4072 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
4073
4074 lta->ppid = msg->ppid;
4075 lta->uid = msg->uid;
4076 lta->gid = msg->gid;
4077
4078 lta->abi = {
4079 .bits_per_long = msg->bits_per_long,
4080 .long_alignment = msg->long_alignment,
4081 .uint8_t_alignment = msg->uint8_t_alignment,
4082 .uint16_t_alignment = msg->uint16_t_alignment,
4083 .uint32_t_alignment = msg->uint32_t_alignment,
4084 .uint64_t_alignment = msg->uint64_t_alignment,
4085 .byte_order = msg->byte_order == LITTLE_ENDIAN ?
4086 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_ :
4087 lttng::sessiond::trace::byte_order::BIG_ENDIAN_,
4088 };
4089
4090 lta->v_major = msg->major;
4091 lta->v_minor = msg->minor;
4092 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4093 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4094 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4095 lta->notify_sock = -1;
4096 lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4097
4098 /* Copy name and make sure it's NULL terminated. */
4099 strncpy(lta->name, msg->name, sizeof(lta->name));
4100 lta->name[UST_APP_PROCNAME_LEN] = '\0';
4101
4102 /*
4103 * Before this can be called, when receiving the registration information,
4104 * the application compatibility is checked. So, at this point, the
4105 * application can work with this session daemon.
4106 */
4107 lta->compatible = 1;
4108
4109 lta->pid = msg->pid;
4110 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
4111 lta->sock = sock;
4112 pthread_mutex_init(&lta->sock_lock, nullptr);
4113 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
4114
4115 CDS_INIT_LIST_HEAD(&lta->teardown_head);
4116 return lta;
4117
4118 error_free_pipe:
4119 lttng_pipe_destroy(event_notifier_event_source_pipe);
4120 lttng_fd_put(LTTNG_FD_APPS, 2);
4121 error:
4122 return nullptr;
4123 }
4124
4125 /*
4126 * For a given application object, add it to every hash table.
4127 */
4128 void ust_app_add(struct ust_app *app)
4129 {
4130 LTTNG_ASSERT(app);
4131 LTTNG_ASSERT(app->notify_sock >= 0);
4132
4133 app->registration_time = time(nullptr);
4134
4135 lttng::urcu::read_lock_guard read_lock;
4136
4137 /*
4138 * On a re-registration, we want to kick out the previous registration of
4139 * that pid
4140 */
4141 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
4142
4143 /*
4144 * The socket _should_ be unique until _we_ call close. So, a add_unique
4145 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4146 * already in the table.
4147 */
4148 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
4149
4150 /* Add application to the notify socket hash table. */
4151 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
4152 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
4153
4154 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4155 "notify_sock =%d (version %d.%d)",
4156 app->pid,
4157 app->ppid,
4158 app->uid,
4159 app->gid,
4160 app->sock,
4161 app->name,
4162 app->notify_sock,
4163 app->v_major,
4164 app->v_minor);
4165 }
4166
4167 /*
4168 * Set the application version into the object.
4169 *
4170 * Return 0 on success else a negative value either an errno code or a
4171 * LTTng-UST error code.
4172 */
4173 int ust_app_version(struct ust_app *app)
4174 {
4175 int ret;
4176
4177 LTTNG_ASSERT(app);
4178
4179 pthread_mutex_lock(&app->sock_lock);
4180 ret = lttng_ust_ctl_tracer_version(app->sock, &app->version);
4181 pthread_mutex_unlock(&app->sock_lock);
4182 if (ret < 0) {
4183 if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) {
4184 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4185 app->pid,
4186 app->sock);
4187 } else if (ret == -EAGAIN) {
4188 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4189 app->pid,
4190 app->sock);
4191 } else {
4192 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4193 ret,
4194 app->pid,
4195 app->sock);
4196 }
4197 }
4198
4199 return ret;
4200 }
4201
4202 bool ust_app_supports_notifiers(const struct ust_app *app)
4203 {
4204 return app->v_major >= 9;
4205 }
4206
4207 bool ust_app_supports_counters(const struct ust_app *app)
4208 {
4209 return app->v_major >= 9;
4210 }
4211
4212 /*
4213 * Setup the base event notifier group.
4214 *
4215 * Return 0 on success else a negative value either an errno code or a
4216 * LTTng-UST error code.
4217 */
4218 int ust_app_setup_event_notifier_group(struct ust_app *app)
4219 {
4220 int ret;
4221 int event_pipe_write_fd;
4222 struct lttng_ust_abi_object_data *event_notifier_group = nullptr;
4223 enum lttng_error_code lttng_ret;
4224 enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
4225
4226 LTTNG_ASSERT(app);
4227
4228 if (!ust_app_supports_notifiers(app)) {
4229 ret = -ENOSYS;
4230 goto error;
4231 }
4232
4233 /* Get the write side of the pipe. */
4234 event_pipe_write_fd = lttng_pipe_get_writefd(app->event_notifier_group.event_pipe);
4235
4236 pthread_mutex_lock(&app->sock_lock);
4237 ret = lttng_ust_ctl_create_event_notifier_group(
4238 app->sock, event_pipe_write_fd, &event_notifier_group);
4239 pthread_mutex_unlock(&app->sock_lock);
4240 if (ret < 0) {
4241 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4242 ret = 0;
4243 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4244 app->pid,
4245 app->sock);
4246 } else if (ret == -EAGAIN) {
4247 ret = 0;
4248 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4249 app->pid,
4250 app->sock);
4251 } else {
4252 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4253 ret,
4254 app->pid,
4255 app->sock,
4256 event_pipe_write_fd);
4257 }
4258 goto error;
4259 }
4260
4261 ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe);
4262 if (ret) {
4263 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4264 app->name,
4265 app->pid);
4266 goto error;
4267 }
4268
4269 /*
4270 * Release the file descriptor that was reserved for the write-end of
4271 * the pipe.
4272 */
4273 lttng_fd_put(LTTNG_FD_APPS, 1);
4274
4275 lttng_ret = notification_thread_command_add_tracer_event_source(
4276 the_notification_thread_handle,
4277 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe),
4278 LTTNG_DOMAIN_UST);
4279 if (lttng_ret != LTTNG_OK) {
4280 ERR("Failed to add tracer event source to notification thread");
4281 ret = -1;
4282 goto error;
4283 }
4284
4285 /* Assign handle only when the complete setup is valid. */
4286 app->event_notifier_group.object = event_notifier_group;
4287
4288 event_notifier_error_accounting_status = event_notifier_error_accounting_register_app(app);
4289 switch (event_notifier_error_accounting_status) {
4290 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK:
4291 break;
4292 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED:
4293 DBG3("Failed to setup event notifier error accounting (application does not support notifier error accounting): app socket fd = %d, app name = '%s', app pid = %d",
4294 app->sock,
4295 app->name,
4296 (int) app->pid);
4297 ret = 0;
4298 goto error_accounting;
4299 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD:
4300 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4301 app->sock,
4302 app->name,
4303 (int) app->pid);
4304 ret = 0;
4305 goto error_accounting;
4306 default:
4307 ERR("Failed to setup event notifier error accounting for app");
4308 ret = -1;
4309 goto error_accounting;
4310 }
4311
4312 return ret;
4313
4314 error_accounting:
4315 lttng_ret = notification_thread_command_remove_tracer_event_source(
4316 the_notification_thread_handle,
4317 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe));
4318 if (lttng_ret != LTTNG_OK) {
4319 ERR("Failed to remove application tracer event source from notification thread");
4320 }
4321
4322 error:
4323 lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object);
4324 free(app->event_notifier_group.object);
4325 app->event_notifier_group.object = nullptr;
4326 return ret;
4327 }
4328
4329 /*
4330 * Unregister app by removing it from the global traceable app list and freeing
4331 * the data struct.
4332 *
4333 * The socket is already closed at this point so no close to sock.
4334 */
4335 void ust_app_unregister(int sock)
4336 {
4337 struct ust_app *lta;
4338 struct lttng_ht_node_ulong *node;
4339 struct lttng_ht_iter ust_app_sock_iter;
4340 struct lttng_ht_iter iter;
4341 struct ust_app_session *ua_sess;
4342 int ret;
4343
4344 lttng::urcu::read_lock_guard read_lock;
4345
4346 /* Get the node reference for a call_rcu */
4347 lttng_ht_lookup(ust_app_ht_by_sock, (void *) ((unsigned long) sock), &ust_app_sock_iter);
4348 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
4349 LTTNG_ASSERT(node);
4350
4351 lta = lttng::utils::container_of(node, &ust_app::sock_n);
4352 DBG("PID %d unregistering with sock %d", lta->pid, sock);
4353
4354 /*
4355 * For per-PID buffers, perform "push metadata" and flush all
4356 * application streams before removing app from hash tables,
4357 * ensuring proper behavior of data_pending check.
4358 * Remove sessions so they are not visible during deletion.
4359 */
4360 cds_lfht_for_each_entry (lta->sessions->ht, &iter.iter, ua_sess, node.node) {
4361 ret = lttng_ht_del(lta->sessions, &iter);
4362 if (ret) {
4363 /* The session was already removed so scheduled for teardown. */
4364 continue;
4365 }
4366
4367 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
4368 (void) ust_app_flush_app_session(lta, ua_sess);
4369 }
4370
4371 /*
4372 * Add session to list for teardown. This is safe since at this point we
4373 * are the only one using this list.
4374 */
4375 pthread_mutex_lock(&ua_sess->lock);
4376
4377 if (ua_sess->deleted) {
4378 pthread_mutex_unlock(&ua_sess->lock);
4379 continue;
4380 }
4381
4382 /*
4383 * Normally, this is done in the delete session process which is
4384 * executed in the call rcu below. However, upon registration we can't
4385 * afford to wait for the grace period before pushing data or else the
4386 * data pending feature can race between the unregistration and stop
4387 * command where the data pending command is sent *before* the grace
4388 * period ended.
4389 *
4390 * The close metadata below nullifies the metadata pointer in the
4391 * session so the delete session will NOT push/close a second time.
4392 */
4393 auto locked_registry = get_locked_session_registry(ua_sess);
4394 if (locked_registry) {
4395 /* Push metadata for application before freeing the application. */
4396 (void) push_metadata(locked_registry, ua_sess->consumer);
4397
4398 /*
4399 * Don't ask to close metadata for global per UID buffers. Close
4400 * metadata only on destroy trace session in this case. Also, the
4401 * previous push metadata could have flag the metadata registry to
4402 * close so don't send a close command if closed.
4403 */
4404 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
4405 const auto metadata_key = locked_registry->_metadata_key;
4406 const auto consumer_bitness = locked_registry->abi.bits_per_long;
4407
4408 if (!locked_registry->_metadata_closed && metadata_key != 0) {
4409 locked_registry->_metadata_closed = true;
4410 }
4411
4412 /* Release lock before communication, see comments in
4413 * close_metadata(). */
4414 locked_registry.reset();
4415 (void) close_metadata(
4416 metadata_key, consumer_bitness, ua_sess->consumer);
4417 } else {
4418 locked_registry.reset();
4419 }
4420 }
4421 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
4422
4423 pthread_mutex_unlock(&ua_sess->lock);
4424 }
4425
4426 /* Remove application from PID hash table */
4427 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
4428 LTTNG_ASSERT(!ret);
4429
4430 /*
4431 * Remove application from notify hash table. The thread handling the
4432 * notify socket could have deleted the node so ignore on error because
4433 * either way it's valid. The close of that socket is handled by the
4434 * apps_notify_thread.
4435 */
4436 iter.iter.node = &lta->notify_sock_n.node;
4437 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
4438
4439 /*
4440 * Ignore return value since the node might have been removed before by an
4441 * add replace during app registration because the PID can be reassigned by
4442 * the OS.
4443 */
4444 iter.iter.node = &lta->pid_n.node;
4445 ret = lttng_ht_del(ust_app_ht, &iter);
4446 if (ret) {
4447 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta->pid);
4448 }
4449
4450 /* Free memory */
4451 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
4452
4453 return;
4454 }
4455
4456 /*
4457 * Fill events array with all events name of all registered apps.
4458 */
4459 int ust_app_list_events(struct lttng_event **events)
4460 {
4461 int ret, handle;
4462 size_t nbmem, count = 0;
4463 struct lttng_ht_iter iter;
4464 struct ust_app *app;
4465 struct lttng_event *tmp_event;
4466
4467 nbmem = UST_APP_EVENT_LIST_SIZE;
4468 tmp_event = calloc<lttng_event>(nbmem);
4469 if (tmp_event == nullptr) {
4470 PERROR("zmalloc ust app events");
4471 ret = -ENOMEM;
4472 goto error;
4473 }
4474
4475 {
4476 lttng::urcu::read_lock_guard read_lock;
4477
4478 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4479 struct lttng_ust_abi_tracepoint_iter uiter;
4480
4481 health_code_update();
4482
4483 if (!app->compatible) {
4484 /*
4485 * TODO: In time, we should notice the caller of this error by
4486 * telling him that this is a version error.
4487 */
4488 continue;
4489 }
4490
4491 pthread_mutex_lock(&app->sock_lock);
4492 handle = lttng_ust_ctl_tracepoint_list(app->sock);
4493 if (handle < 0) {
4494 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4495 ERR("UST app list events getting handle failed for app pid %d",
4496 app->pid);
4497 }
4498 pthread_mutex_unlock(&app->sock_lock);
4499 continue;
4500 }
4501
4502 while ((ret = lttng_ust_ctl_tracepoint_list_get(
4503 app->sock, handle, &uiter)) != -LTTNG_UST_ERR_NOENT) {
4504 /* Handle ustctl error. */
4505 if (ret < 0) {
4506 int release_ret;
4507
4508 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4509 ERR("UST app tp list get failed for app %d with ret %d",
4510 app->sock,
4511 ret);
4512 } else {
4513 DBG3("UST app tp list get failed. Application is dead");
4514 break;
4515 }
4516
4517 free(tmp_event);
4518 release_ret =
4519 lttng_ust_ctl_release_handle(app->sock, handle);
4520 if (release_ret < 0 &&
4521 release_ret != -LTTNG_UST_ERR_EXITING &&
4522 release_ret != -EPIPE) {
4523 ERR("Error releasing app handle for app %d with ret %d",
4524 app->sock,
4525 release_ret);
4526 }
4527
4528 pthread_mutex_unlock(&app->sock_lock);
4529 goto rcu_error;
4530 }
4531
4532 health_code_update();
4533 if (count >= nbmem) {
4534 /* In case the realloc fails, we free the memory */
4535 struct lttng_event *new_tmp_event;
4536 size_t new_nbmem;
4537
4538 new_nbmem = nbmem << 1;
4539 DBG2("Reallocating event list from %zu to %zu entries",
4540 nbmem,
4541 new_nbmem);
4542 new_tmp_event = (lttng_event *) realloc(
4543 tmp_event, new_nbmem * sizeof(struct lttng_event));
4544 if (new_tmp_event == nullptr) {
4545 int release_ret;
4546
4547 PERROR("realloc ust app events");
4548 free(tmp_event);
4549 ret = -ENOMEM;
4550 release_ret = lttng_ust_ctl_release_handle(
4551 app->sock, handle);
4552 if (release_ret < 0 &&
4553 release_ret != -LTTNG_UST_ERR_EXITING &&
4554 release_ret != -EPIPE) {
4555 ERR("Error releasing app handle for app %d with ret %d",
4556 app->sock,
4557 release_ret);
4558 }
4559
4560 pthread_mutex_unlock(&app->sock_lock);
4561 goto rcu_error;
4562 }
4563 /* Zero the new memory */
4564 memset(new_tmp_event + nbmem,
4565 0,
4566 (new_nbmem - nbmem) * sizeof(struct lttng_event));
4567 nbmem = new_nbmem;
4568 tmp_event = new_tmp_event;
4569 }
4570
4571 memcpy(tmp_event[count].name,
4572 uiter.name,
4573 LTTNG_UST_ABI_SYM_NAME_LEN);
4574 tmp_event[count].loglevel = uiter.loglevel;
4575 tmp_event[count].type =
4576 (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT;
4577 tmp_event[count].pid = app->pid;
4578 tmp_event[count].enabled = -1;
4579 count++;
4580 }
4581
4582 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4583 pthread_mutex_unlock(&app->sock_lock);
4584 if (ret < 0) {
4585 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4586 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4587 app->pid,
4588 app->sock);
4589 } else if (ret == -EAGAIN) {
4590 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4591 app->pid,
4592 app->sock);
4593 } else {
4594 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4595 ret,
4596 app->pid,
4597 app->sock);
4598 }
4599 }
4600 }
4601 }
4602
4603 ret = count;
4604 *events = tmp_event;
4605
4606 DBG2("UST app list events done (%zu events)", count);
4607
4608 rcu_error:
4609 error:
4610 health_code_update();
4611 return ret;
4612 }
4613
4614 /*
4615 * Fill events array with all events name of all registered apps.
4616 */
4617 int ust_app_list_event_fields(struct lttng_event_field **fields)
4618 {
4619 int ret, handle;
4620 size_t nbmem, count = 0;
4621 struct lttng_ht_iter iter;
4622 struct ust_app *app;
4623 struct lttng_event_field *tmp_event;
4624
4625 nbmem = UST_APP_EVENT_LIST_SIZE;
4626 tmp_event = calloc<lttng_event_field>(nbmem);
4627 if (tmp_event == nullptr) {
4628 PERROR("zmalloc ust app event fields");
4629 ret = -ENOMEM;
4630 goto error;
4631 }
4632
4633 {
4634 lttng::urcu::read_lock_guard read_lock;
4635
4636 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4637 struct lttng_ust_abi_field_iter uiter;
4638
4639 health_code_update();
4640
4641 if (!app->compatible) {
4642 /*
4643 * TODO: In time, we should notice the caller of this error by
4644 * telling him that this is a version error.
4645 */
4646 continue;
4647 }
4648
4649 pthread_mutex_lock(&app->sock_lock);
4650 handle = lttng_ust_ctl_tracepoint_field_list(app->sock);
4651 if (handle < 0) {
4652 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4653 ERR("UST app list field getting handle failed for app pid %d",
4654 app->pid);
4655 }
4656 pthread_mutex_unlock(&app->sock_lock);
4657 continue;
4658 }
4659
4660 while ((ret = lttng_ust_ctl_tracepoint_field_list_get(
4661 app->sock, handle, &uiter)) != -LTTNG_UST_ERR_NOENT) {
4662 /* Handle ustctl error. */
4663 if (ret < 0) {
4664 int release_ret;
4665
4666 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4667 ERR("UST app tp list field failed for app %d with ret %d",
4668 app->sock,
4669 ret);
4670 } else {
4671 DBG3("UST app tp list field failed. Application is dead");
4672 break;
4673 }
4674
4675 free(tmp_event);
4676 release_ret =
4677 lttng_ust_ctl_release_handle(app->sock, handle);
4678 pthread_mutex_unlock(&app->sock_lock);
4679 if (release_ret < 0 &&
4680 release_ret != -LTTNG_UST_ERR_EXITING &&
4681 release_ret != -EPIPE) {
4682 ERR("Error releasing app handle for app %d with ret %d",
4683 app->sock,
4684 release_ret);
4685 }
4686
4687 goto rcu_error;
4688 }
4689
4690 health_code_update();
4691 if (count >= nbmem) {
4692 /* In case the realloc fails, we free the memory */
4693 struct lttng_event_field *new_tmp_event;
4694 size_t new_nbmem;
4695
4696 new_nbmem = nbmem << 1;
4697 DBG2("Reallocating event field list from %zu to %zu entries",
4698 nbmem,
4699 new_nbmem);
4700 new_tmp_event = (lttng_event_field *) realloc(
4701 tmp_event,
4702 new_nbmem * sizeof(struct lttng_event_field));
4703 if (new_tmp_event == nullptr) {
4704 int release_ret;
4705
4706 PERROR("realloc ust app event fields");
4707 free(tmp_event);
4708 ret = -ENOMEM;
4709 release_ret = lttng_ust_ctl_release_handle(
4710 app->sock, handle);
4711 pthread_mutex_unlock(&app->sock_lock);
4712 if (release_ret &&
4713 release_ret != -LTTNG_UST_ERR_EXITING &&
4714 release_ret != -EPIPE) {
4715 ERR("Error releasing app handle for app %d with ret %d",
4716 app->sock,
4717 release_ret);
4718 }
4719
4720 goto rcu_error;
4721 }
4722
4723 /* Zero the new memory */
4724 memset(new_tmp_event + nbmem,
4725 0,
4726 (new_nbmem - nbmem) *
4727 sizeof(struct lttng_event_field));
4728 nbmem = new_nbmem;
4729 tmp_event = new_tmp_event;
4730 }
4731
4732 memcpy(tmp_event[count].field_name,
4733 uiter.field_name,
4734 LTTNG_UST_ABI_SYM_NAME_LEN);
4735 /* Mapping between these enums matches 1 to 1. */
4736 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
4737 tmp_event[count].nowrite = uiter.nowrite;
4738
4739 memcpy(tmp_event[count].event.name,
4740 uiter.event_name,
4741 LTTNG_UST_ABI_SYM_NAME_LEN);
4742 tmp_event[count].event.loglevel = uiter.loglevel;
4743 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
4744 tmp_event[count].event.pid = app->pid;
4745 tmp_event[count].event.enabled = -1;
4746 count++;
4747 }
4748
4749 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4750 pthread_mutex_unlock(&app->sock_lock);
4751 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4752 ERR("Error releasing app handle for app %d with ret %d",
4753 app->sock,
4754 ret);
4755 }
4756 }
4757 }
4758
4759 ret = count;
4760 *fields = tmp_event;
4761
4762 DBG2("UST app list event fields done (%zu events)", count);
4763
4764 rcu_error:
4765 error:
4766 health_code_update();
4767 return ret;
4768 }
4769
4770 /*
4771 * Free and clean all traceable apps of the global list.
4772 */
4773 void ust_app_clean_list()
4774 {
4775 int ret;
4776 struct ust_app *app;
4777 struct lttng_ht_iter iter;
4778
4779 DBG2("UST app cleaning registered apps hash table");
4780
4781 /* Cleanup notify socket hash table */
4782 if (ust_app_ht_by_notify_sock) {
4783 lttng::urcu::read_lock_guard read_lock;
4784
4785 cds_lfht_for_each_entry (
4786 ust_app_ht_by_notify_sock->ht, &iter.iter, app, notify_sock_n.node) {
4787 /*
4788 * Assert that all notifiers are gone as all triggers
4789 * are unregistered prior to this clean-up.
4790 */
4791 LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0);
4792
4793 ust_app_notify_sock_unregister(app->notify_sock);
4794 }
4795 }
4796
4797 if (ust_app_ht) {
4798 lttng::urcu::read_lock_guard read_lock;
4799
4800 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4801 ret = lttng_ht_del(ust_app_ht, &iter);
4802 LTTNG_ASSERT(!ret);
4803 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
4804 }
4805 }
4806
4807 /* Cleanup socket hash table */
4808 if (ust_app_ht_by_sock) {
4809 lttng::urcu::read_lock_guard read_lock;
4810
4811 cds_lfht_for_each_entry (ust_app_ht_by_sock->ht, &iter.iter, app, sock_n.node) {
4812 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
4813 LTTNG_ASSERT(!ret);
4814 }
4815 }
4816
4817 /* Destroy is done only when the ht is empty */
4818 if (ust_app_ht) {
4819 lttng_ht_destroy(ust_app_ht);
4820 }
4821 if (ust_app_ht_by_sock) {
4822 lttng_ht_destroy(ust_app_ht_by_sock);
4823 }
4824 if (ust_app_ht_by_notify_sock) {
4825 lttng_ht_destroy(ust_app_ht_by_notify_sock);
4826 }
4827 }
4828
4829 /*
4830 * Init UST app hash table.
4831 */
4832 int ust_app_ht_alloc()
4833 {
4834 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4835 if (!ust_app_ht) {
4836 return -1;
4837 }
4838 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4839 if (!ust_app_ht_by_sock) {
4840 return -1;
4841 }
4842 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4843 if (!ust_app_ht_by_notify_sock) {
4844 return -1;
4845 }
4846 return 0;
4847 }
4848
4849 /*
4850 * For a specific UST session, disable the channel for all registered apps.
4851 */
4852 int ust_app_disable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan)
4853 {
4854 int ret = 0;
4855 struct lttng_ht_iter iter;
4856 struct lttng_ht_node_str *ua_chan_node;
4857 struct ust_app *app;
4858 struct ust_app_session *ua_sess;
4859 struct ust_app_channel *ua_chan;
4860
4861 LTTNG_ASSERT(usess->active);
4862 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
4863 uchan->name,
4864 usess->id);
4865
4866 {
4867 lttng::urcu::read_lock_guard read_lock;
4868
4869 /* For every registered applications */
4870 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4871 struct lttng_ht_iter uiter;
4872 if (!app->compatible) {
4873 /*
4874 * TODO: In time, we should notice the caller of this error by
4875 * telling him that this is a version error.
4876 */
4877 continue;
4878 }
4879 ua_sess = lookup_session_by_app(usess, app);
4880 if (ua_sess == nullptr) {
4881 continue;
4882 }
4883
4884 /* Get channel */
4885 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
4886 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4887 /* If the session if found for the app, the channel must be there */
4888 LTTNG_ASSERT(ua_chan_node);
4889
4890 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
4891 /* The channel must not be already disabled */
4892 LTTNG_ASSERT(ua_chan->enabled);
4893
4894 /* Disable channel onto application */
4895 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
4896 if (ret < 0) {
4897 /* XXX: We might want to report this error at some point... */
4898 continue;
4899 }
4900 }
4901 }
4902
4903 return ret;
4904 }
4905
4906 /*
4907 * For a specific UST session, enable the channel for all registered apps.
4908 */
4909 int ust_app_enable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan)
4910 {
4911 int ret = 0;
4912 struct lttng_ht_iter iter;
4913 struct ust_app *app;
4914 struct ust_app_session *ua_sess;
4915
4916 LTTNG_ASSERT(usess->active);
4917 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
4918 uchan->name,
4919 usess->id);
4920
4921 {
4922 lttng::urcu::read_lock_guard read_lock;
4923
4924 /* For every registered applications */
4925 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4926 if (!app->compatible) {
4927 /*
4928 * TODO: In time, we should notice the caller of this error by
4929 * telling him that this is a version error.
4930 */
4931 continue;
4932 }
4933 ua_sess = lookup_session_by_app(usess, app);
4934 if (ua_sess == nullptr) {
4935 continue;
4936 }
4937
4938 /* Enable channel onto application */
4939 ret = enable_ust_app_channel(ua_sess, uchan, app);
4940 if (ret < 0) {
4941 /* XXX: We might want to report this error at some point... */
4942 continue;
4943 }
4944 }
4945 }
4946
4947 return ret;
4948 }
4949
4950 /*
4951 * Disable an event in a channel and for a specific session.
4952 */
4953 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4954 struct ltt_ust_channel *uchan,
4955 struct ltt_ust_event *uevent)
4956 {
4957 int ret = 0;
4958 struct lttng_ht_iter iter, uiter;
4959 struct lttng_ht_node_str *ua_chan_node;
4960 struct ust_app *app;
4961 struct ust_app_session *ua_sess;
4962 struct ust_app_channel *ua_chan;
4963 struct ust_app_event *ua_event;
4964
4965 LTTNG_ASSERT(usess->active);
4966 DBG("UST app disabling event %s for all apps in channel "
4967 "%s for session id %" PRIu64,
4968 uevent->attr.name,
4969 uchan->name,
4970 usess->id);
4971
4972 {
4973 lttng::urcu::read_lock_guard read_lock;
4974
4975 /* For all registered applications */
4976 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4977 if (!app->compatible) {
4978 /*
4979 * TODO: In time, we should notice the caller of this error by
4980 * telling him that this is a version error.
4981 */
4982 continue;
4983 }
4984 ua_sess = lookup_session_by_app(usess, app);
4985 if (ua_sess == nullptr) {
4986 /* Next app */
4987 continue;
4988 }
4989
4990 /* Lookup channel in the ust app session */
4991 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
4992 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4993 if (ua_chan_node == nullptr) {
4994 DBG2("Channel %s not found in session id %" PRIu64
4995 " for app pid %d."
4996 "Skipping",
4997 uchan->name,
4998 usess->id,
4999 app->pid);
5000 continue;
5001 }
5002 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5003
5004 ua_event = find_ust_app_event(ua_chan->events,
5005 uevent->attr.name,
5006 uevent->filter,
5007 uevent->attr.loglevel,
5008 uevent->exclusion);
5009 if (ua_event == nullptr) {
5010 DBG2("Event %s not found in channel %s for app pid %d."
5011 "Skipping",
5012 uevent->attr.name,
5013 uchan->name,
5014 app->pid);
5015 continue;
5016 }
5017
5018 ret = disable_ust_app_event(ua_event, app);
5019 if (ret < 0) {
5020 /* XXX: Report error someday... */
5021 continue;
5022 }
5023 }
5024 }
5025
5026 return ret;
5027 }
5028
5029 /* The ua_sess lock must be held by the caller. */
5030 static int ust_app_channel_create(struct ltt_ust_session *usess,
5031 struct ust_app_session *ua_sess,
5032 struct ltt_ust_channel *uchan,
5033 struct ust_app *app,
5034 struct ust_app_channel **_ua_chan)
5035 {
5036 int ret = 0;
5037 struct ust_app_channel *ua_chan = nullptr;
5038
5039 LTTNG_ASSERT(ua_sess);
5040 ASSERT_LOCKED(ua_sess->lock);
5041
5042 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, sizeof(uchan->name))) {
5043 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr);
5044 ret = 0;
5045 } else {
5046 struct ltt_ust_context *uctx = nullptr;
5047
5048 /*
5049 * Create channel onto application and synchronize its
5050 * configuration.
5051 */
5052 ret = ust_app_channel_allocate(
5053 ua_sess, uchan, LTTNG_UST_ABI_CHAN_PER_CPU, usess, &ua_chan);
5054 if (ret < 0) {
5055 goto error;
5056 }
5057
5058 ret = ust_app_channel_send(app, usess, ua_sess, ua_chan);
5059 if (ret) {
5060 goto error;
5061 }
5062
5063 /* Add contexts. */
5064 cds_list_for_each_entry (uctx, &uchan->ctx_list, list) {
5065 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
5066 if (ret) {
5067 goto error;
5068 }
5069 }
5070 }
5071
5072 error:
5073 if (ret < 0) {
5074 switch (ret) {
5075 case -ENOTCONN:
5076 /*
5077 * The application's socket is not valid. Either a bad socket
5078 * or a timeout on it. We can't inform the caller that for a
5079 * specific app, the session failed so lets continue here.
5080 */
5081 ret = 0; /* Not an error. */
5082 break;
5083 case -ENOMEM:
5084 default:
5085 break;
5086 }
5087 }
5088
5089 if (ret == 0 && _ua_chan) {
5090 /*
5091 * Only return the application's channel on success. Note
5092 * that the channel can still be part of the application's
5093 * channel hashtable on error.
5094 */
5095 *_ua_chan = ua_chan;
5096 }
5097 return ret;
5098 }
5099
5100 /*
5101 * Enable event for a specific session and channel on the tracer.
5102 */
5103 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
5104 struct ltt_ust_channel *uchan,
5105 struct ltt_ust_event *uevent)
5106 {
5107 int ret = 0;
5108 struct lttng_ht_iter iter, uiter;
5109 struct lttng_ht_node_str *ua_chan_node;
5110 struct ust_app *app;
5111 struct ust_app_session *ua_sess;
5112 struct ust_app_channel *ua_chan;
5113 struct ust_app_event *ua_event;
5114
5115 LTTNG_ASSERT(usess->active);
5116 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
5117 uevent->attr.name,
5118 usess->id);
5119
5120 /*
5121 * NOTE: At this point, this function is called only if the session and
5122 * channel passed are already created for all apps. and enabled on the
5123 * tracer also.
5124 */
5125
5126 {
5127 lttng::urcu::read_lock_guard read_lock;
5128
5129 /* For all registered applications */
5130 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5131 if (!app->compatible) {
5132 /*
5133 * TODO: In time, we should notice the caller of this error by
5134 * telling him that this is a version error.
5135 */
5136 continue;
5137 }
5138 ua_sess = lookup_session_by_app(usess, app);
5139 if (!ua_sess) {
5140 /* The application has problem or is probably dead. */
5141 continue;
5142 }
5143
5144 pthread_mutex_lock(&ua_sess->lock);
5145
5146 if (ua_sess->deleted) {
5147 pthread_mutex_unlock(&ua_sess->lock);
5148 continue;
5149 }
5150
5151 /* Lookup channel in the ust app session */
5152 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
5153 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5154 /*
5155 * It is possible that the channel cannot be found is
5156 * the channel/event creation occurs concurrently with
5157 * an application exit.
5158 */
5159 if (!ua_chan_node) {
5160 pthread_mutex_unlock(&ua_sess->lock);
5161 continue;
5162 }
5163
5164 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5165
5166 /* Get event node */
5167 ua_event = find_ust_app_event(ua_chan->events,
5168 uevent->attr.name,
5169 uevent->filter,
5170 uevent->attr.loglevel,
5171 uevent->exclusion);
5172 if (ua_event == nullptr) {
5173 DBG3("UST app enable event %s not found for app PID %d."
5174 "Skipping app",
5175 uevent->attr.name,
5176 app->pid);
5177 goto next_app;
5178 }
5179
5180 ret = enable_ust_app_event(ua_event, app);
5181 if (ret < 0) {
5182 pthread_mutex_unlock(&ua_sess->lock);
5183 goto error;
5184 }
5185 next_app:
5186 pthread_mutex_unlock(&ua_sess->lock);
5187 }
5188 }
5189 error:
5190 return ret;
5191 }
5192
5193 /*
5194 * For a specific existing UST session and UST channel, creates the event for
5195 * all registered apps.
5196 */
5197 int ust_app_create_event_glb(struct ltt_ust_session *usess,
5198 struct ltt_ust_channel *uchan,
5199 struct ltt_ust_event *uevent)
5200 {
5201 int ret = 0;
5202 struct lttng_ht_iter iter, uiter;
5203 struct lttng_ht_node_str *ua_chan_node;
5204 struct ust_app *app;
5205 struct ust_app_session *ua_sess;
5206 struct ust_app_channel *ua_chan;
5207
5208 LTTNG_ASSERT(usess->active);
5209 DBG("UST app creating event %s for all apps for session id %" PRIu64,
5210 uevent->attr.name,
5211 usess->id);
5212
5213 {
5214 lttng::urcu::read_lock_guard read_lock;
5215
5216 /* For all registered applications */
5217 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5218 if (!app->compatible) {
5219 /*
5220 * TODO: In time, we should notice the caller of this error by
5221 * telling him that this is a version error.
5222 */
5223 continue;
5224 }
5225
5226 ua_sess = lookup_session_by_app(usess, app);
5227 if (!ua_sess) {
5228 /* The application has problem or is probably dead. */
5229 continue;
5230 }
5231
5232 pthread_mutex_lock(&ua_sess->lock);
5233
5234 if (ua_sess->deleted) {
5235 pthread_mutex_unlock(&ua_sess->lock);
5236 continue;
5237 }
5238
5239 /* Lookup channel in the ust app session */
5240 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
5241 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5242 /* If the channel is not found, there is a code flow error */
5243 LTTNG_ASSERT(ua_chan_node);
5244
5245 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5246
5247 ret = create_ust_app_event(ua_chan, uevent, app);
5248 pthread_mutex_unlock(&ua_sess->lock);
5249 if (ret < 0) {
5250 if (ret != -LTTNG_UST_ERR_EXIST) {
5251 /* Possible value at this point: -ENOMEM. If so, we stop! */
5252 break;
5253 }
5254
5255 DBG2("UST app event %s already exist on app PID %d",
5256 uevent->attr.name,
5257 app->pid);
5258 continue;
5259 }
5260 }
5261 }
5262
5263 return ret;
5264 }
5265
5266 /*
5267 * Start tracing for a specific UST session and app.
5268 *
5269 * Called with UST app session lock held.
5270 *
5271 */
5272 static int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
5273 {
5274 int ret = 0;
5275 struct ust_app_session *ua_sess;
5276
5277 DBG("Starting tracing for ust app pid %d", app->pid);
5278
5279 lttng::urcu::read_lock_guard read_lock;
5280
5281 if (!app->compatible) {
5282 goto end;
5283 }
5284
5285 ua_sess = lookup_session_by_app(usess, app);
5286 if (ua_sess == nullptr) {
5287 /* The session is in teardown process. Ignore and continue. */
5288 goto end;
5289 }
5290
5291 pthread_mutex_lock(&ua_sess->lock);
5292
5293 if (ua_sess->deleted) {
5294 pthread_mutex_unlock(&ua_sess->lock);
5295 goto end;
5296 }
5297
5298 if (ua_sess->enabled) {
5299 pthread_mutex_unlock(&ua_sess->lock);
5300 goto end;
5301 }
5302
5303 /* Upon restart, we skip the setup, already done */
5304 if (ua_sess->started) {
5305 goto skip_setup;
5306 }
5307
5308 health_code_update();
5309
5310 skip_setup:
5311 /* This starts the UST tracing */
5312 pthread_mutex_lock(&app->sock_lock);
5313 ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle);
5314 pthread_mutex_unlock(&app->sock_lock);
5315 if (ret < 0) {
5316 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5317 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5318 app->pid,
5319 app->sock);
5320 pthread_mutex_unlock(&ua_sess->lock);
5321 goto end;
5322 } else if (ret == -EAGAIN) {
5323 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5324 app->pid,
5325 app->sock);
5326 pthread_mutex_unlock(&ua_sess->lock);
5327 goto end;
5328
5329 } else {
5330 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5331 ret,
5332 app->pid,
5333 app->sock);
5334 }
5335 goto error_unlock;
5336 }
5337
5338 /* Indicate that the session has been started once */
5339 ua_sess->started = true;
5340 ua_sess->enabled = true;
5341
5342 pthread_mutex_unlock(&ua_sess->lock);
5343
5344 health_code_update();
5345
5346 /* Quiescent wait after starting trace */
5347 pthread_mutex_lock(&app->sock_lock);
5348 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5349 pthread_mutex_unlock(&app->sock_lock);
5350 if (ret < 0) {
5351 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5352 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5353 app->pid,
5354 app->sock);
5355 } else if (ret == -EAGAIN) {
5356 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5357 app->pid,
5358 app->sock);
5359 } else {
5360 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5361 ret,
5362 app->pid,
5363 app->sock);
5364 }
5365 }
5366
5367 end:
5368 health_code_update();
5369 return 0;
5370
5371 error_unlock:
5372 pthread_mutex_unlock(&ua_sess->lock);
5373 health_code_update();
5374 return -1;
5375 }
5376
5377 /*
5378 * Stop tracing for a specific UST session and app.
5379 */
5380 static int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
5381 {
5382 int ret = 0;
5383 struct ust_app_session *ua_sess;
5384
5385 DBG("Stopping tracing for ust app pid %d", app->pid);
5386
5387 lttng::urcu::read_lock_guard read_lock;
5388
5389 if (!app->compatible) {
5390 goto end_no_session;
5391 }
5392
5393 ua_sess = lookup_session_by_app(usess, app);
5394 if (ua_sess == nullptr) {
5395 goto end_no_session;
5396 }
5397
5398 pthread_mutex_lock(&ua_sess->lock);
5399
5400 if (ua_sess->deleted) {
5401 pthread_mutex_unlock(&ua_sess->lock);
5402 goto end_no_session;
5403 }
5404
5405 /*
5406 * If started = 0, it means that stop trace has been called for a session
5407 * that was never started. It's possible since we can have a fail start
5408 * from either the application manager thread or the command thread. Simply
5409 * indicate that this is a stop error.
5410 */
5411 if (!ua_sess->started) {
5412 goto error_rcu_unlock;
5413 }
5414
5415 health_code_update();
5416
5417 /* This inhibits UST tracing */
5418 pthread_mutex_lock(&app->sock_lock);
5419 ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle);
5420 pthread_mutex_unlock(&app->sock_lock);
5421 if (ret < 0) {
5422 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5423 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5424 app->pid,
5425 app->sock);
5426 goto end_unlock;
5427 } else if (ret == -EAGAIN) {
5428 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5429 app->pid,
5430 app->sock);
5431 goto end_unlock;
5432
5433 } else {
5434 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5435 ret,
5436 app->pid,
5437 app->sock);
5438 }
5439 goto error_rcu_unlock;
5440 }
5441
5442 health_code_update();
5443 ua_sess->enabled = false;
5444
5445 /* Quiescent wait after stopping trace */
5446 pthread_mutex_lock(&app->sock_lock);
5447 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5448 pthread_mutex_unlock(&app->sock_lock);
5449 if (ret < 0) {
5450 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5451 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5452 app->pid,
5453 app->sock);
5454 } else if (ret == -EAGAIN) {
5455 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5456 app->pid,
5457 app->sock);
5458 } else {
5459 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5460 ret,
5461 app->pid,
5462 app->sock);
5463 }
5464 }
5465
5466 health_code_update();
5467
5468 {
5469 auto locked_registry = get_locked_session_registry(ua_sess);
5470
5471 /* The UST app session is held registry shall not be null. */
5472 LTTNG_ASSERT(locked_registry);
5473
5474 /* Push metadata for application before freeing the application. */
5475 (void) push_metadata(locked_registry, ua_sess->consumer);
5476 }
5477
5478 end_unlock:
5479 pthread_mutex_unlock(&ua_sess->lock);
5480 end_no_session:
5481 health_code_update();
5482 return 0;
5483
5484 error_rcu_unlock:
5485 pthread_mutex_unlock(&ua_sess->lock);
5486 health_code_update();
5487 return -1;
5488 }
5489
5490 static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
5491 {
5492 int ret, retval = 0;
5493 struct lttng_ht_iter iter;
5494 struct ust_app_channel *ua_chan;
5495 struct consumer_socket *socket;
5496
5497 DBG("Flushing app session buffers for ust app pid %d", app->pid);
5498
5499 if (!app->compatible) {
5500 goto end_not_compatible;
5501 }
5502
5503 pthread_mutex_lock(&ua_sess->lock);
5504
5505 if (ua_sess->deleted) {
5506 goto end_deleted;
5507 }
5508
5509 health_code_update();
5510
5511 /* Flushing buffers */
5512 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, ua_sess->consumer);
5513
5514 /* Flush buffers and push metadata. */
5515 switch (ua_sess->buffer_type) {
5516 case LTTNG_BUFFER_PER_PID:
5517 {
5518 lttng::urcu::read_lock_guard read_lock;
5519
5520 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
5521 health_code_update();
5522 ret = consumer_flush_channel(socket, ua_chan->key);
5523 if (ret) {
5524 ERR("Error flushing consumer channel");
5525 retval = -1;
5526 continue;
5527 }
5528 }
5529
5530 break;
5531 }
5532 case LTTNG_BUFFER_PER_UID:
5533 default:
5534 abort();
5535 break;
5536 }
5537
5538 health_code_update();
5539
5540 end_deleted:
5541 pthread_mutex_unlock(&ua_sess->lock);
5542
5543 end_not_compatible:
5544 health_code_update();
5545 return retval;
5546 }
5547
5548 /*
5549 * Flush buffers for all applications for a specific UST session.
5550 * Called with UST session lock held.
5551 */
5552 static int ust_app_flush_session(struct ltt_ust_session *usess)
5553
5554 {
5555 int ret = 0;
5556
5557 DBG("Flushing session buffers for all ust apps");
5558
5559 /* Flush buffers and push metadata. */
5560 switch (usess->buffer_type) {
5561 case LTTNG_BUFFER_PER_UID:
5562 {
5563 struct buffer_reg_uid *reg;
5564 struct lttng_ht_iter iter;
5565
5566 /* Flush all per UID buffers associated to that session. */
5567 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
5568 lttng::urcu::read_lock_guard read_lock;
5569 lsu::registry_session *ust_session_reg;
5570 struct buffer_reg_channel *buf_reg_chan;
5571 struct consumer_socket *socket;
5572
5573 /* Get consumer socket to use to push the metadata.*/
5574 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5575 usess->consumer);
5576 if (!socket) {
5577 /* Ignore request if no consumer is found for the session. */
5578 continue;
5579 }
5580
5581 cds_lfht_for_each_entry (
5582 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
5583 /*
5584 * The following call will print error values so the return
5585 * code is of little importance because whatever happens, we
5586 * have to try them all.
5587 */
5588 (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key);
5589 }
5590
5591 ust_session_reg = reg->registry->reg.ust;
5592 /* Push metadata. */
5593 auto locked_registry = ust_session_reg->lock();
5594 (void) push_metadata(locked_registry, usess->consumer);
5595 }
5596
5597 break;
5598 }
5599 case LTTNG_BUFFER_PER_PID:
5600 {
5601 struct ust_app_session *ua_sess;
5602 struct lttng_ht_iter iter;
5603 struct ust_app *app;
5604 lttng::urcu::read_lock_guard read_lock;
5605
5606 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5607 ua_sess = lookup_session_by_app(usess, app);
5608 if (ua_sess == nullptr) {
5609 continue;
5610 }
5611
5612 (void) ust_app_flush_app_session(app, ua_sess);
5613 }
5614
5615 break;
5616 }
5617 default:
5618 ret = -1;
5619 abort();
5620 break;
5621 }
5622
5623 health_code_update();
5624 return ret;
5625 }
5626
5627 static int ust_app_clear_quiescent_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
5628 {
5629 int ret = 0;
5630 struct lttng_ht_iter iter;
5631 struct ust_app_channel *ua_chan;
5632 struct consumer_socket *socket;
5633
5634 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
5635
5636 lttng::urcu::read_lock_guard read_lock;
5637
5638 if (!app->compatible) {
5639 goto end_not_compatible;
5640 }
5641
5642 pthread_mutex_lock(&ua_sess->lock);
5643
5644 if (ua_sess->deleted) {
5645 goto end_unlock;
5646 }
5647
5648 health_code_update();
5649
5650 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, ua_sess->consumer);
5651 if (!socket) {
5652 ERR("Failed to find consumer (%" PRIu32 ") socket", app->abi.bits_per_long);
5653 ret = -1;
5654 goto end_unlock;
5655 }
5656
5657 /* Clear quiescent state. */
5658 switch (ua_sess->buffer_type) {
5659 case LTTNG_BUFFER_PER_PID:
5660 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
5661 health_code_update();
5662 ret = consumer_clear_quiescent_channel(socket, ua_chan->key);
5663 if (ret) {
5664 ERR("Error clearing quiescent state for consumer channel");
5665 ret = -1;
5666 continue;
5667 }
5668 }
5669 break;
5670 case LTTNG_BUFFER_PER_UID:
5671 default:
5672 abort();
5673 ret = -1;
5674 break;
5675 }
5676
5677 health_code_update();
5678
5679 end_unlock:
5680 pthread_mutex_unlock(&ua_sess->lock);
5681
5682 end_not_compatible:
5683 health_code_update();
5684 return ret;
5685 }
5686
5687 /*
5688 * Clear quiescent state in each stream for all applications for a
5689 * specific UST session.
5690 * Called with UST session lock held.
5691 */
5692 static int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
5693
5694 {
5695 int ret = 0;
5696
5697 DBG("Clearing stream quiescent state for all ust apps");
5698
5699 switch (usess->buffer_type) {
5700 case LTTNG_BUFFER_PER_UID:
5701 {
5702 struct lttng_ht_iter iter;
5703 struct buffer_reg_uid *reg;
5704
5705 /*
5706 * Clear quiescent for all per UID buffers associated to
5707 * that session.
5708 */
5709 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
5710 struct consumer_socket *socket;
5711 struct buffer_reg_channel *buf_reg_chan;
5712 lttng::urcu::read_lock_guard read_lock;
5713
5714 /* Get associated consumer socket.*/
5715 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5716 usess->consumer);
5717 if (!socket) {
5718 /*
5719 * Ignore request if no consumer is found for
5720 * the session.
5721 */
5722 continue;
5723 }
5724
5725 cds_lfht_for_each_entry (
5726 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
5727 /*
5728 * The following call will print error values so
5729 * the return code is of little importance
5730 * because whatever happens, we have to try them
5731 * all.
5732 */
5733 (void) consumer_clear_quiescent_channel(socket,
5734 buf_reg_chan->consumer_key);
5735 }
5736 }
5737
5738 break;
5739 }
5740 case LTTNG_BUFFER_PER_PID:
5741 {
5742 struct ust_app_session *ua_sess;
5743 struct lttng_ht_iter iter;
5744 struct ust_app *app;
5745 lttng::urcu::read_lock_guard read_lock;
5746
5747 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5748 ua_sess = lookup_session_by_app(usess, app);
5749 if (ua_sess == nullptr) {
5750 continue;
5751 }
5752 (void) ust_app_clear_quiescent_app_session(app, ua_sess);
5753 }
5754
5755 break;
5756 }
5757 default:
5758 ret = -1;
5759 abort();
5760 break;
5761 }
5762
5763 health_code_update();
5764 return ret;
5765 }
5766
5767 /*
5768 * Destroy a specific UST session in apps.
5769 */
5770 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
5771 {
5772 int ret;
5773 struct ust_app_session *ua_sess;
5774 struct lttng_ht_iter iter;
5775 struct lttng_ht_node_u64 *node;
5776
5777 DBG("Destroy tracing for ust app pid %d", app->pid);
5778
5779 lttng::urcu::read_lock_guard read_lock;
5780
5781 if (!app->compatible) {
5782 goto end;
5783 }
5784
5785 __lookup_session_by_app(usess, app, &iter);
5786 node = lttng_ht_iter_get_node_u64(&iter);
5787 if (node == nullptr) {
5788 /* Session is being or is deleted. */
5789 goto end;
5790 }
5791 ua_sess = lttng::utils::container_of(node, &ust_app_session::node);
5792
5793 health_code_update();
5794 destroy_app_session(app, ua_sess);
5795
5796 health_code_update();
5797
5798 /* Quiescent wait after stopping trace */
5799 pthread_mutex_lock(&app->sock_lock);
5800 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5801 pthread_mutex_unlock(&app->sock_lock);
5802 if (ret < 0) {
5803 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5804 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5805 app->pid,
5806 app->sock);
5807 } else if (ret == -EAGAIN) {
5808 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5809 app->pid,
5810 app->sock);
5811 } else {
5812 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5813 ret,
5814 app->pid,
5815 app->sock);
5816 }
5817 }
5818 end:
5819 health_code_update();
5820 return 0;
5821 }
5822
5823 /*
5824 * Start tracing for the UST session.
5825 */
5826 int ust_app_start_trace_all(struct ltt_ust_session *usess)
5827 {
5828 struct lttng_ht_iter iter;
5829 struct ust_app *app;
5830
5831 DBG("Starting all UST traces");
5832
5833 /*
5834 * Even though the start trace might fail, flag this session active so
5835 * other application coming in are started by default.
5836 */
5837 usess->active = true;
5838
5839 /*
5840 * In a start-stop-start use-case, we need to clear the quiescent state
5841 * of each channel set by the prior stop command, thus ensuring that a
5842 * following stop or destroy is sure to grab a timestamp_end near those
5843 * operations, even if the packet is empty.
5844 */
5845 (void) ust_app_clear_quiescent_session(usess);
5846
5847 {
5848 lttng::urcu::read_lock_guard read_lock;
5849
5850 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5851 ust_app_global_update(usess, app);
5852 }
5853 }
5854
5855 return 0;
5856 }
5857
5858 /*
5859 * Start tracing for the UST session.
5860 * Called with UST session lock held.
5861 */
5862 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5863 {
5864 int ret = 0;
5865 struct lttng_ht_iter iter;
5866 struct ust_app *app;
5867
5868 DBG("Stopping all UST traces");
5869
5870 /*
5871 * Even though the stop trace might fail, flag this session inactive so
5872 * other application coming in are not started by default.
5873 */
5874 usess->active = false;
5875
5876 {
5877 lttng::urcu::read_lock_guard read_lock;
5878
5879 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5880 ret = ust_app_stop_trace(usess, app);
5881 if (ret < 0) {
5882 /* Continue to next apps even on error */
5883 continue;
5884 }
5885 }
5886 }
5887
5888 (void) ust_app_flush_session(usess);
5889
5890 return 0;
5891 }
5892
5893 /*
5894 * Destroy app UST session.
5895 */
5896 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5897 {
5898 int ret = 0;
5899 struct lttng_ht_iter iter;
5900 struct ust_app *app;
5901
5902 DBG("Destroy all UST traces");
5903
5904 {
5905 lttng::urcu::read_lock_guard read_lock;
5906
5907 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5908 ret = destroy_trace(usess, app);
5909 if (ret < 0) {
5910 /* Continue to next apps even on error */
5911 continue;
5912 }
5913 }
5914 }
5915
5916 return 0;
5917 }
5918
5919 /* The ua_sess lock must be held by the caller. */
5920 static int find_or_create_ust_app_channel(struct ltt_ust_session *usess,
5921 struct ust_app_session *ua_sess,
5922 struct ust_app *app,
5923 struct ltt_ust_channel *uchan,
5924 struct ust_app_channel **ua_chan)
5925 {
5926 int ret = 0;
5927 struct lttng_ht_iter iter;
5928 struct lttng_ht_node_str *ua_chan_node;
5929
5930 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5931 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5932 if (ua_chan_node) {
5933 *ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5934 goto end;
5935 }
5936
5937 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5938 if (ret) {
5939 goto end;
5940 }
5941 end:
5942 return ret;
5943 }
5944
5945 static int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5946 struct ltt_ust_event *uevent,
5947 struct ust_app *app)
5948 {
5949 int ret = 0;
5950 struct ust_app_event *ua_event = nullptr;
5951
5952 ua_event = find_ust_app_event(ua_chan->events,
5953 uevent->attr.name,
5954 uevent->filter,
5955 uevent->attr.loglevel,
5956 uevent->exclusion);
5957 if (!ua_event) {
5958 ret = create_ust_app_event(ua_chan, uevent, app);
5959 if (ret < 0) {
5960 goto end;
5961 }
5962 } else {
5963 if (ua_event->enabled != uevent->enabled) {
5964 ret = uevent->enabled ? enable_ust_app_event(ua_event, app) :
5965 disable_ust_app_event(ua_event, app);
5966 }
5967 }
5968
5969 end:
5970 return ret;
5971 }
5972
5973 /* Called with RCU read-side lock held. */
5974 static void ust_app_synchronize_event_notifier_rules(struct ust_app *app)
5975 {
5976 int ret = 0;
5977 enum lttng_error_code ret_code;
5978 enum lttng_trigger_status t_status;
5979 struct lttng_ht_iter app_trigger_iter;
5980 struct lttng_triggers *triggers = nullptr;
5981 struct ust_app_event_notifier_rule *event_notifier_rule;
5982 unsigned int count, i;
5983
5984 ASSERT_RCU_READ_LOCKED();
5985
5986 if (!ust_app_supports_notifiers(app)) {
5987 goto end;
5988 }
5989
5990 /*
5991 * Currrently, registering or unregistering a trigger with an
5992 * event rule condition causes a full synchronization of the event
5993 * notifiers.
5994 *
5995 * The first step attempts to add an event notifier for all registered
5996 * triggers that apply to the user space tracers. Then, the
5997 * application's event notifiers rules are all checked against the list
5998 * of registered triggers. Any event notifier that doesn't have a
5999 * matching trigger can be assumed to have been disabled.
6000 *
6001 * All of this is inefficient, but is put in place to get the feature
6002 * rolling as it is simpler at this moment. It will be optimized Soon™
6003 * to allow the state of enabled
6004 * event notifiers to be synchronized in a piece-wise way.
6005 */
6006
6007 /* Get all triggers using uid 0 (root) */
6008 ret_code = notification_thread_command_list_triggers(
6009 the_notification_thread_handle, 0, &triggers);
6010 if (ret_code != LTTNG_OK) {
6011 goto end;
6012 }
6013
6014 LTTNG_ASSERT(triggers);
6015
6016 t_status = lttng_triggers_get_count(triggers, &count);
6017 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
6018 goto end;
6019 }
6020
6021 for (i = 0; i < count; i++) {
6022 struct lttng_condition *condition;
6023 struct lttng_event_rule *event_rule;
6024 struct lttng_trigger *trigger;
6025 const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule;
6026 enum lttng_condition_status condition_status;
6027 uint64_t token;
6028
6029 trigger = lttng_triggers_borrow_mutable_at_index(triggers, i);
6030 LTTNG_ASSERT(trigger);
6031
6032 token = lttng_trigger_get_tracer_token(trigger);
6033 condition = lttng_trigger_get_condition(trigger);
6034
6035 if (lttng_condition_get_type(condition) !=
6036 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) {
6037 /* Does not apply */
6038 continue;
6039 }
6040
6041 condition_status = lttng_condition_event_rule_matches_borrow_rule_mutable(
6042 condition, &event_rule);
6043 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
6044
6045 if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) {
6046 /* Skip kernel related triggers. */
6047 continue;
6048 }
6049
6050 /*
6051 * Find or create the associated token event rule. The caller
6052 * holds the RCU read lock, so this is safe to call without
6053 * explicitly acquiring it here.
6054 */
6055 looked_up_event_notifier_rule = find_ust_app_event_notifier_rule(
6056 app->token_to_event_notifier_rule_ht, token);
6057 if (!looked_up_event_notifier_rule) {
6058 ret = create_ust_app_event_notifier_rule(trigger, app);
6059 if (ret < 0) {
6060 goto end;
6061 }
6062 }
6063 }
6064
6065 {
6066 lttng::urcu::read_lock_guard read_lock;
6067
6068 /* Remove all unknown event sources from the app. */
6069 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
6070 &app_trigger_iter.iter,
6071 event_notifier_rule,
6072 node.node) {
6073 const uint64_t app_token = event_notifier_rule->token;
6074 bool found = false;
6075
6076 /*
6077 * Check if the app event trigger still exists on the
6078 * notification side.
6079 */
6080 for (i = 0; i < count; i++) {
6081 uint64_t notification_thread_token;
6082 const struct lttng_trigger *trigger =
6083 lttng_triggers_get_at_index(triggers, i);
6084
6085 LTTNG_ASSERT(trigger);
6086
6087 notification_thread_token = lttng_trigger_get_tracer_token(trigger);
6088
6089 if (notification_thread_token == app_token) {
6090 found = true;
6091 break;
6092 }
6093 }
6094
6095 if (found) {
6096 /* Still valid. */
6097 continue;
6098 }
6099
6100 /*
6101 * This trigger was unregistered, disable it on the tracer's
6102 * side.
6103 */
6104 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &app_trigger_iter);
6105 LTTNG_ASSERT(ret == 0);
6106
6107 /* Callee logs errors. */
6108 (void) disable_ust_object(app, event_notifier_rule->obj);
6109
6110 delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app);
6111 }
6112 }
6113
6114 end:
6115 lttng_triggers_destroy(triggers);
6116 return;
6117 }
6118
6119 /*
6120 * RCU read lock must be held by the caller.
6121 */
6122 static void ust_app_synchronize_all_channels(struct ltt_ust_session *usess,
6123 struct ust_app_session *ua_sess,
6124 struct ust_app *app)
6125 {
6126 int ret = 0;
6127 struct cds_lfht_iter uchan_iter;
6128 struct ltt_ust_channel *uchan;
6129
6130 LTTNG_ASSERT(usess);
6131 LTTNG_ASSERT(ua_sess);
6132 LTTNG_ASSERT(app);
6133 ASSERT_RCU_READ_LOCKED();
6134
6135 cds_lfht_for_each_entry (usess->domain_global.channels->ht, &uchan_iter, uchan, node.node) {
6136 struct ust_app_channel *ua_chan;
6137 struct cds_lfht_iter uevent_iter;
6138 struct ltt_ust_event *uevent;
6139
6140 /*
6141 * Search for a matching ust_app_channel. If none is found,
6142 * create it. Creating the channel will cause the ua_chan
6143 * structure to be allocated, the channel buffers to be
6144 * allocated (if necessary) and sent to the application, and
6145 * all enabled contexts will be added to the channel.
6146 */
6147 ret = find_or_create_ust_app_channel(usess, ua_sess, app, uchan, &ua_chan);
6148 if (ret) {
6149 /* Tracer is probably gone or ENOMEM. */
6150 goto end;
6151 }
6152
6153 if (!ua_chan) {
6154 /* ua_chan will be NULL for the metadata channel */
6155 continue;
6156 }
6157
6158 cds_lfht_for_each_entry (uchan->events->ht, &uevent_iter, uevent, node.node) {
6159 ret = ust_app_channel_synchronize_event(ua_chan, uevent, app);
6160 if (ret) {
6161 goto end;
6162 }
6163 }
6164
6165 if (ua_chan->enabled != uchan->enabled) {
6166 ret = uchan->enabled ? enable_ust_app_channel(ua_sess, uchan, app) :
6167 disable_ust_app_channel(ua_sess, ua_chan, app);
6168 if (ret) {
6169 goto end;
6170 }
6171 }
6172 }
6173 end:
6174 return;
6175 }
6176
6177 /*
6178 * The caller must ensure that the application is compatible and is tracked
6179 * by the process attribute trackers.
6180 */
6181 static void ust_app_synchronize(struct ltt_ust_session *usess, struct ust_app *app)
6182 {
6183 int ret = 0;
6184 struct ust_app_session *ua_sess = nullptr;
6185
6186 /*
6187 * The application's configuration should only be synchronized for
6188 * active sessions.
6189 */
6190 LTTNG_ASSERT(usess->active);
6191
6192 ret = find_or_create_ust_app_session(usess, app, &ua_sess, nullptr);
6193 if (ret < 0) {
6194 /* Tracer is probably gone or ENOMEM. */
6195 if (ua_sess) {
6196 destroy_app_session(app, ua_sess);
6197 }
6198 goto end;
6199 }
6200 LTTNG_ASSERT(ua_sess);
6201
6202 pthread_mutex_lock(&ua_sess->lock);
6203 if (ua_sess->deleted) {
6204 goto deleted_session;
6205 }
6206
6207 {
6208 lttng::urcu::read_lock_guard read_lock;
6209
6210 ust_app_synchronize_all_channels(usess, ua_sess, app);
6211
6212 /*
6213 * Create the metadata for the application. This returns gracefully if a
6214 * metadata was already set for the session.
6215 *
6216 * The metadata channel must be created after the data channels as the
6217 * consumer daemon assumes this ordering. When interacting with a relay
6218 * daemon, the consumer will use this assumption to send the
6219 * "STREAMS_SENT" message to the relay daemon.
6220 */
6221 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
6222 if (ret < 0) {
6223 ERR("Metadata creation failed for app sock %d for session id %" PRIu64,
6224 app->sock,
6225 usess->id);
6226 }
6227 }
6228
6229 deleted_session:
6230 pthread_mutex_unlock(&ua_sess->lock);
6231 end:
6232 return;
6233 }
6234
6235 static void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
6236 {
6237 struct ust_app_session *ua_sess;
6238
6239 ua_sess = lookup_session_by_app(usess, app);
6240 if (ua_sess == nullptr) {
6241 return;
6242 }
6243 destroy_app_session(app, ua_sess);
6244 }
6245
6246 /*
6247 * Add channels/events from UST global domain to registered apps at sock.
6248 *
6249 * Called with session lock held.
6250 * Called with RCU read-side lock held.
6251 */
6252 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
6253 {
6254 LTTNG_ASSERT(usess);
6255 LTTNG_ASSERT(usess->active);
6256 ASSERT_RCU_READ_LOCKED();
6257
6258 DBG2("UST app global update for app sock %d for session id %" PRIu64, app->sock, usess->id);
6259
6260 if (!app->compatible) {
6261 return;
6262 }
6263 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, usess, app->pid) &&
6264 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, usess, app->uid) &&
6265 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, usess, app->gid)) {
6266 /*
6267 * Synchronize the application's internal tracing configuration
6268 * and start tracing.
6269 */
6270 ust_app_synchronize(usess, app);
6271 ust_app_start_trace(usess, app);
6272 } else {
6273 ust_app_global_destroy(usess, app);
6274 }
6275 }
6276
6277 /*
6278 * Add all event notifiers to an application.
6279 *
6280 * Called with session lock held.
6281 * Called with RCU read-side lock held.
6282 */
6283 void ust_app_global_update_event_notifier_rules(struct ust_app *app)
6284 {
6285 ASSERT_RCU_READ_LOCKED();
6286
6287 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6288 app->name,
6289 app->pid);
6290
6291 if (!app->compatible || !ust_app_supports_notifiers(app)) {
6292 return;
6293 }
6294
6295 if (app->event_notifier_group.object == nullptr) {
6296 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6297 app->name,
6298 app->pid);
6299 return;
6300 }
6301
6302 ust_app_synchronize_event_notifier_rules(app);
6303 }
6304
6305 /*
6306 * Called with session lock held.
6307 */
6308 void ust_app_global_update_all(struct ltt_ust_session *usess)
6309 {
6310 struct lttng_ht_iter iter;
6311 struct ust_app *app;
6312
6313 {
6314 lttng::urcu::read_lock_guard read_lock;
6315
6316 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6317 ust_app_global_update(usess, app);
6318 }
6319 }
6320 }
6321
6322 void ust_app_global_update_all_event_notifier_rules()
6323 {
6324 struct lttng_ht_iter iter;
6325 struct ust_app *app;
6326
6327 lttng::urcu::read_lock_guard read_lock;
6328 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6329 ust_app_global_update_event_notifier_rules(app);
6330 }
6331 }
6332
6333 /*
6334 * Add context to a specific channel for global UST domain.
6335 */
6336 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
6337 struct ltt_ust_channel *uchan,
6338 struct ltt_ust_context *uctx)
6339 {
6340 int ret = 0;
6341 struct lttng_ht_node_str *ua_chan_node;
6342 struct lttng_ht_iter iter, uiter;
6343 struct ust_app_channel *ua_chan = nullptr;
6344 struct ust_app_session *ua_sess;
6345 struct ust_app *app;
6346
6347 LTTNG_ASSERT(usess->active);
6348
6349 {
6350 lttng::urcu::read_lock_guard read_lock;
6351 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6352 if (!app->compatible) {
6353 /*
6354 * TODO: In time, we should notice the caller of this error by
6355 * telling him that this is a version error.
6356 */
6357 continue;
6358 }
6359 ua_sess = lookup_session_by_app(usess, app);
6360 if (ua_sess == nullptr) {
6361 continue;
6362 }
6363
6364 pthread_mutex_lock(&ua_sess->lock);
6365
6366 if (ua_sess->deleted) {
6367 pthread_mutex_unlock(&ua_sess->lock);
6368 continue;
6369 }
6370
6371 /* Lookup channel in the ust app session */
6372 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
6373 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6374 if (ua_chan_node == nullptr) {
6375 goto next_app;
6376 }
6377 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6378 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
6379 if (ret < 0) {
6380 goto next_app;
6381 }
6382 next_app:
6383 pthread_mutex_unlock(&ua_sess->lock);
6384 }
6385 }
6386
6387 return ret;
6388 }
6389
6390 /*
6391 * Receive registration and populate the given msg structure.
6392 *
6393 * On success return 0 else a negative value returned by the ustctl call.
6394 */
6395 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
6396 {
6397 int ret;
6398 uint32_t pid, ppid, uid, gid;
6399
6400 LTTNG_ASSERT(msg);
6401
6402 ret = lttng_ust_ctl_recv_reg_msg(sock,
6403 &msg->type,
6404 &msg->major,
6405 &msg->minor,
6406 &pid,
6407 &ppid,
6408 &uid,
6409 &gid,
6410 &msg->bits_per_long,
6411 &msg->uint8_t_alignment,
6412 &msg->uint16_t_alignment,
6413 &msg->uint32_t_alignment,
6414 &msg->uint64_t_alignment,
6415 &msg->long_alignment,
6416 &msg->byte_order,
6417 msg->name);
6418 if (ret < 0) {
6419 switch (-ret) {
6420 case EPIPE:
6421 case ECONNRESET:
6422 case LTTNG_UST_ERR_EXITING:
6423 DBG3("UST app recv reg message failed. Application died");
6424 break;
6425 case LTTNG_UST_ERR_UNSUP_MAJOR:
6426 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6427 msg->major,
6428 msg->minor,
6429 LTTNG_UST_ABI_MAJOR_VERSION,
6430 LTTNG_UST_ABI_MINOR_VERSION);
6431 break;
6432 default:
6433 ERR("UST app recv reg message failed with ret %d", ret);
6434 break;
6435 }
6436 goto error;
6437 }
6438 msg->pid = (pid_t) pid;
6439 msg->ppid = (pid_t) ppid;
6440 msg->uid = (uid_t) uid;
6441 msg->gid = (gid_t) gid;
6442
6443 error:
6444 return ret;
6445 }
6446
6447 /*
6448 * Return a ust app session object using the application object and the
6449 * session object descriptor has a key. If not found, NULL is returned.
6450 * A RCU read side lock MUST be acquired when calling this function.
6451 */
6452 static struct ust_app_session *find_session_by_objd(struct ust_app *app, int objd)
6453 {
6454 struct lttng_ht_node_ulong *node;
6455 struct lttng_ht_iter iter;
6456 struct ust_app_session *ua_sess = nullptr;
6457
6458 LTTNG_ASSERT(app);
6459 ASSERT_RCU_READ_LOCKED();
6460
6461 lttng_ht_lookup(app->ust_sessions_objd, (void *) ((unsigned long) objd), &iter);
6462 node = lttng_ht_iter_get_node_ulong(&iter);
6463 if (node == nullptr) {
6464 DBG2("UST app session find by objd %d not found", objd);
6465 goto error;
6466 }
6467
6468 ua_sess = lttng::utils::container_of(node, &ust_app_session::ust_objd_node);
6469
6470 error:
6471 return ua_sess;
6472 }
6473
6474 /*
6475 * Return a ust app channel object using the application object and the channel
6476 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6477 * lock MUST be acquired before calling this function.
6478 */
6479 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, int objd)
6480 {
6481 struct lttng_ht_node_ulong *node;
6482 struct lttng_ht_iter iter;
6483 struct ust_app_channel *ua_chan = nullptr;
6484
6485 LTTNG_ASSERT(app);
6486 ASSERT_RCU_READ_LOCKED();
6487
6488 lttng_ht_lookup(app->ust_objd, (void *) ((unsigned long) objd), &iter);
6489 node = lttng_ht_iter_get_node_ulong(&iter);
6490 if (node == nullptr) {
6491 DBG2("UST app channel find by objd %d not found", objd);
6492 goto error;
6493 }
6494
6495 ua_chan = lttng::utils::container_of(node, &ust_app_channel::ust_objd_node);
6496
6497 error:
6498 return ua_chan;
6499 }
6500
6501 /*
6502 * Reply to a register channel notification from an application on the notify
6503 * socket. The channel metadata is also created.
6504 *
6505 * The session UST registry lock is acquired in this function.
6506 *
6507 * On success 0 is returned else a negative value.
6508 */
6509 static int handle_app_register_channel_notification(int sock,
6510 int cobjd,
6511 struct lttng_ust_ctl_field *raw_context_fields,
6512 size_t context_field_count)
6513 {
6514 int ret, ret_code = 0;
6515 uint32_t chan_id;
6516 uint64_t chan_reg_key;
6517 struct ust_app *app;
6518 struct ust_app_channel *ua_chan;
6519 struct ust_app_session *ua_sess;
6520 auto ust_ctl_context_fields =
6521 lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_context_fields);
6522
6523 lttng::urcu::read_lock_guard read_lock_guard;
6524
6525 /* Lookup application. If not found, there is a code flow error. */
6526 app = find_app_by_notify_sock(sock);
6527 if (!app) {
6528 DBG("Application socket %d is being torn down. Abort event notify", sock);
6529 return -1;
6530 }
6531
6532 /* Lookup channel by UST object descriptor. */
6533 ua_chan = find_channel_by_objd(app, cobjd);
6534 if (!ua_chan) {
6535 DBG("Application channel is being torn down. Abort event notify");
6536 return 0;
6537 }
6538
6539 LTTNG_ASSERT(ua_chan->session);
6540 ua_sess = ua_chan->session;
6541
6542 /* Get right session registry depending on the session buffer type. */
6543 auto locked_registry_session = get_locked_session_registry(ua_sess);
6544 if (!locked_registry_session) {
6545 DBG("Application session is being torn down. Abort event notify");
6546 return 0;
6547 };
6548
6549 /* Depending on the buffer type, a different channel key is used. */
6550 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6551 chan_reg_key = ua_chan->tracing_channel_id;
6552 } else {
6553 chan_reg_key = ua_chan->key;
6554 }
6555
6556 auto& ust_reg_chan = locked_registry_session->channel(chan_reg_key);
6557
6558 /* Channel id is set during the object creation. */
6559 chan_id = ust_reg_chan.id;
6560
6561 /*
6562 * The application returns the typing information of the channel's
6563 * context fields. In per-PID buffering mode, this is the first and only
6564 * time we get this information. It is our chance to finalize the
6565 * initialiation of the channel and serialize it's layout's description
6566 * to the trace's metadata.
6567 *
6568 * However, in per-UID buffering mode, every application will provide
6569 * this information (redundantly). The first time will allow us to
6570 * complete the initialization. The following times, we simply validate
6571 * that all apps provide the same typing for the context fields as a
6572 * sanity check.
6573 */
6574 try {
6575 auto app_context_fields = lsu::create_trace_fields_from_ust_ctl_fields(
6576 *locked_registry_session,
6577 ust_ctl_context_fields.get(),
6578 context_field_count,
6579 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT,
6580 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS);
6581
6582 if (!ust_reg_chan.is_registered()) {
6583 lst::type::cuptr event_context = app_context_fields.size() ?
6584 lttng::make_unique<lst::structure_type>(
6585 0, std::move(app_context_fields)) :
6586 nullptr;
6587
6588 ust_reg_chan.event_context(std::move(event_context));
6589 } else {
6590 /*
6591 * Validate that the context fields match between
6592 * registry and newcoming application.
6593 */
6594 bool context_fields_match;
6595 const auto *previous_event_context = ust_reg_chan.event_context();
6596
6597 if (!previous_event_context) {
6598 context_fields_match = app_context_fields.size() == 0;
6599 } else {
6600 const lst::structure_type app_event_context_struct(
6601 0, std::move(app_context_fields));
6602
6603 context_fields_match = *previous_event_context ==
6604 app_event_context_struct;
6605 }
6606
6607 if (!context_fields_match) {
6608 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6609 app->pid,
6610 app->sock);
6611 ret_code = -EINVAL;
6612 goto reply;
6613 }
6614 }
6615 } catch (std::exception& ex) {
6616 ERR("Failed to handle application context: %s", ex.what());
6617 ret_code = -EINVAL;
6618 goto reply;
6619 }
6620
6621 reply:
6622 DBG3("UST app replying to register channel key %" PRIu64 " with id %u, ret = %d",
6623 chan_reg_key,
6624 chan_id,
6625 ret_code);
6626
6627 ret = lttng_ust_ctl_reply_register_channel(
6628 sock,
6629 chan_id,
6630 ust_reg_chan.header_type_ == lst::stream_class::header_type::COMPACT ?
6631 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT :
6632 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE,
6633 ret_code);
6634 if (ret < 0) {
6635 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6636 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6637 app->pid,
6638 app->sock);
6639 } else if (ret == -EAGAIN) {
6640 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6641 app->pid,
6642 app->sock);
6643 } else {
6644 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6645 ret,
6646 app->pid,
6647 app->sock);
6648 }
6649
6650 return ret;
6651 }
6652
6653 /* This channel registry's registration is completed. */
6654 ust_reg_chan.set_as_registered();
6655
6656 return ret;
6657 }
6658
6659 /*
6660 * Add event to the UST channel registry. When the event is added to the
6661 * registry, the metadata is also created. Once done, this replies to the
6662 * application with the appropriate error code.
6663 *
6664 * The session UST registry lock is acquired in the function.
6665 *
6666 * On success 0 is returned else a negative value.
6667 */
6668 static int add_event_ust_registry(int sock,
6669 int sobjd,
6670 int cobjd,
6671 const char *name,
6672 char *raw_signature,
6673 size_t nr_fields,
6674 struct lttng_ust_ctl_field *raw_fields,
6675 int loglevel_value,
6676 char *raw_model_emf_uri)
6677 {
6678 int ret, ret_code;
6679 uint32_t event_id = 0;
6680 uint64_t chan_reg_key;
6681 struct ust_app *app;
6682 struct ust_app_channel *ua_chan;
6683 struct ust_app_session *ua_sess;
6684 lttng::urcu::read_lock_guard rcu_lock;
6685 auto signature = lttng::make_unique_wrapper<char, lttng::free>(raw_signature);
6686 auto fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_fields);
6687 auto model_emf_uri = lttng::make_unique_wrapper<char, lttng::free>(raw_model_emf_uri);
6688
6689 /* Lookup application. If not found, there is a code flow error. */
6690 app = find_app_by_notify_sock(sock);
6691 if (!app) {
6692 DBG("Application socket %d is being torn down. Abort event notify", sock);
6693 return -1;
6694 }
6695
6696 /* Lookup channel by UST object descriptor. */
6697 ua_chan = find_channel_by_objd(app, cobjd);
6698 if (!ua_chan) {
6699 DBG("Application channel is being torn down. Abort event notify");
6700 return 0;
6701 }
6702
6703 LTTNG_ASSERT(ua_chan->session);
6704 ua_sess = ua_chan->session;
6705
6706 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6707 chan_reg_key = ua_chan->tracing_channel_id;
6708 } else {
6709 chan_reg_key = ua_chan->key;
6710 }
6711
6712 {
6713 auto locked_registry = get_locked_session_registry(ua_sess);
6714 if (locked_registry) {
6715 /*
6716 * From this point on, this call acquires the ownership of the signature,
6717 * fields and model_emf_uri meaning any free are done inside it if needed.
6718 * These three variables MUST NOT be read/write after this.
6719 */
6720 try {
6721 auto& channel = locked_registry->channel(chan_reg_key);
6722
6723 /* event_id is set on success. */
6724 channel.add_event(
6725 sobjd,
6726 cobjd,
6727 name,
6728 signature.get(),
6729 lsu::create_trace_fields_from_ust_ctl_fields(
6730 *locked_registry,
6731 fields.get(),
6732 nr_fields,
6733 lst::field_location::root::EVENT_RECORD_PAYLOAD,
6734 lsu::ctl_field_quirks::
6735 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS),
6736 loglevel_value,
6737 model_emf_uri.get() ?
6738 nonstd::optional<std::string>(model_emf_uri.get()) :
6739 nonstd::nullopt,
6740 ua_sess->buffer_type,
6741 *app,
6742 event_id);
6743 ret_code = 0;
6744 } catch (const std::exception& ex) {
6745 ERR("Failed to add event `%s` to registry session: %s",
6746 name,
6747 ex.what());
6748 /* Inform the application of the error; don't return directly. */
6749 ret_code = -EINVAL;
6750 }
6751 } else {
6752 DBG("Application session is being torn down. Abort event notify");
6753 return 0;
6754 }
6755 }
6756
6757 /*
6758 * The return value is returned to ustctl so in case of an error, the
6759 * application can be notified. In case of an error, it's important not to
6760 * return a negative error or else the application will get closed.
6761 */
6762 ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code);
6763 if (ret < 0) {
6764 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6765 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6766 app->pid,
6767 app->sock);
6768 } else if (ret == -EAGAIN) {
6769 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6770 app->pid,
6771 app->sock);
6772 } else {
6773 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6774 ret,
6775 app->pid,
6776 app->sock);
6777 }
6778 /*
6779 * No need to wipe the create event since the application socket will
6780 * get close on error hence cleaning up everything by itself.
6781 */
6782 return ret;
6783 }
6784
6785 DBG3("UST registry event %s with id %" PRId32 " added successfully", name, event_id);
6786 return ret;
6787 }
6788
6789 /*
6790 * Add enum to the UST session registry. Once done, this replies to the
6791 * application with the appropriate error code.
6792 *
6793 * The session UST registry lock is acquired within this function.
6794 *
6795 * On success 0 is returned else a negative value.
6796 */
6797 static int add_enum_ust_registry(int sock,
6798 int sobjd,
6799 const char *name,
6800 struct lttng_ust_ctl_enum_entry *raw_entries,
6801 size_t nr_entries)
6802 {
6803 int ret = 0;
6804 struct ust_app *app;
6805 struct ust_app_session *ua_sess;
6806 uint64_t enum_id = -1ULL;
6807 lttng::urcu::read_lock_guard read_lock_guard;
6808 auto entries = lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::free>(
6809 raw_entries);
6810
6811 /* Lookup application. If not found, there is a code flow error. */
6812 app = find_app_by_notify_sock(sock);
6813 if (!app) {
6814 /* Return an error since this is not an error */
6815 DBG("Application socket %d is being torn down. Aborting enum registration", sock);
6816 return -1;
6817 }
6818
6819 /* Lookup session by UST object descriptor. */
6820 ua_sess = find_session_by_objd(app, sobjd);
6821 if (!ua_sess) {
6822 /* Return an error since this is not an error */
6823 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6824 return 0;
6825 }
6826
6827 auto locked_registry = get_locked_session_registry(ua_sess);
6828 if (!locked_registry) {
6829 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6830 return 0;
6831 }
6832
6833 /*
6834 * From this point on, the callee acquires the ownership of
6835 * entries. The variable entries MUST NOT be read/written after
6836 * call.
6837 */
6838 int application_reply_code;
6839 try {
6840 locked_registry->create_or_find_enum(
6841 sobjd, name, entries.release(), nr_entries, &enum_id);
6842 application_reply_code = 0;
6843 } catch (const std::exception& ex) {
6844 ERR("%s: %s",
6845 fmt::format(
6846 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6847 *app,
6848 name)
6849 .c_str(),
6850 ex.what());
6851 application_reply_code = -1;
6852 }
6853
6854 /*
6855 * The return value is returned to ustctl so in case of an error, the
6856 * application can be notified. In case of an error, it's important not to
6857 * return a negative error or else the application will get closed.
6858 */
6859 ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code);
6860 if (ret < 0) {
6861 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6862 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6863 app->pid,
6864 app->sock);
6865 } else if (ret == -EAGAIN) {
6866 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6867 app->pid,
6868 app->sock);
6869 } else {
6870 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6871 ret,
6872 app->pid,
6873 app->sock);
6874 }
6875 /*
6876 * No need to wipe the create enum since the application socket will
6877 * get close on error hence cleaning up everything by itself.
6878 */
6879 return ret;
6880 }
6881
6882 DBG3("UST registry enum %s added successfully or already found", name);
6883 return 0;
6884 }
6885
6886 /*
6887 * Handle application notification through the given notify socket.
6888 *
6889 * Return 0 on success or else a negative value.
6890 */
6891 int ust_app_recv_notify(int sock)
6892 {
6893 int ret;
6894 enum lttng_ust_ctl_notify_cmd cmd;
6895
6896 DBG3("UST app receiving notify from sock %d", sock);
6897
6898 ret = lttng_ust_ctl_recv_notify(sock, &cmd);
6899 if (ret < 0) {
6900 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6901 DBG3("UST app recv notify failed. Application died: sock = %d", sock);
6902 } else if (ret == -EAGAIN) {
6903 WARN("UST app recv notify failed. Communication time out: sock = %d", sock);
6904 } else {
6905 ERR("UST app recv notify failed with ret %d: sock = %d", ret, sock);
6906 }
6907 goto error;
6908 }
6909
6910 switch (cmd) {
6911 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT:
6912 {
6913 int sobjd, cobjd, loglevel_value;
6914 char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri;
6915 size_t nr_fields;
6916 struct lttng_ust_ctl_field *fields;
6917
6918 DBG2("UST app ustctl register event received");
6919
6920 ret = lttng_ust_ctl_recv_register_event(sock,
6921 &sobjd,
6922 &cobjd,
6923 name,
6924 &loglevel_value,
6925 &sig,
6926 &nr_fields,
6927 &fields,
6928 &model_emf_uri);
6929 if (ret < 0) {
6930 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6931 DBG3("UST app recv event failed. Application died: sock = %d",
6932 sock);
6933 } else if (ret == -EAGAIN) {
6934 WARN("UST app recv event failed. Communication time out: sock = %d",
6935 sock);
6936 } else {
6937 ERR("UST app recv event failed with ret %d: sock = %d", ret, sock);
6938 }
6939 goto error;
6940 }
6941
6942 {
6943 lttng::urcu::read_lock_guard rcu_lock;
6944 const struct ust_app *app = find_app_by_notify_sock(sock);
6945 if (!app) {
6946 DBG("Application socket %d is being torn down. Abort event notify",
6947 sock);
6948 ret = -1;
6949 goto error;
6950 }
6951 }
6952
6953 if ((!fields && nr_fields > 0) || (fields && nr_fields == 0)) {
6954 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6955 fields,
6956 nr_fields);
6957 ret = -1;
6958 free(fields);
6959 goto error;
6960 }
6961
6962 /*
6963 * Add event to the UST registry coming from the notify socket. This
6964 * call will free if needed the sig, fields and model_emf_uri. This
6965 * code path loses the ownsership of these variables and transfer them
6966 * to the this function.
6967 */
6968 ret = add_event_ust_registry(sock,
6969 sobjd,
6970 cobjd,
6971 name,
6972 sig,
6973 nr_fields,
6974 fields,
6975 loglevel_value,
6976 model_emf_uri);
6977 if (ret < 0) {
6978 goto error;
6979 }
6980
6981 break;
6982 }
6983 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL:
6984 {
6985 int sobjd, cobjd;
6986 size_t field_count;
6987 struct lttng_ust_ctl_field *context_fields;
6988
6989 DBG2("UST app ustctl register channel received");
6990
6991 ret = lttng_ust_ctl_recv_register_channel(
6992 sock, &sobjd, &cobjd, &field_count, &context_fields);
6993 if (ret < 0) {
6994 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6995 DBG3("UST app recv channel failed. Application died: sock = %d",
6996 sock);
6997 } else if (ret == -EAGAIN) {
6998 WARN("UST app recv channel failed. Communication time out: sock = %d",
6999 sock);
7000 } else {
7001 ERR("UST app recv channel failed with ret %d: sock = %d",
7002 ret,
7003 sock);
7004 }
7005 goto error;
7006 }
7007
7008 /*
7009 * The fields ownership are transfered to this function call meaning
7010 * that if needed it will be freed. After this, it's invalid to access
7011 * fields or clean them up.
7012 */
7013 ret = handle_app_register_channel_notification(
7014 sock, cobjd, context_fields, field_count);
7015 if (ret < 0) {
7016 goto error;
7017 }
7018
7019 break;
7020 }
7021 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM:
7022 {
7023 int sobjd;
7024 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
7025 size_t nr_entries;
7026 struct lttng_ust_ctl_enum_entry *entries;
7027
7028 DBG2("UST app ustctl register enum received");
7029
7030 ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name, &entries, &nr_entries);
7031 if (ret < 0) {
7032 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
7033 DBG3("UST app recv enum failed. Application died: sock = %d", sock);
7034 } else if (ret == -EAGAIN) {
7035 WARN("UST app recv enum failed. Communication time out: sock = %d",
7036 sock);
7037 } else {
7038 ERR("UST app recv enum failed with ret %d: sock = %d", ret, sock);
7039 }
7040 goto error;
7041 }
7042
7043 /* Callee assumes ownership of entries. */
7044 ret = add_enum_ust_registry(sock, sobjd, name, entries, nr_entries);
7045 if (ret < 0) {
7046 goto error;
7047 }
7048
7049 break;
7050 }
7051 default:
7052 /* Should NEVER happen. */
7053 abort();
7054 }
7055
7056 error:
7057 return ret;
7058 }
7059
7060 /*
7061 * Once the notify socket hangs up, this is called. First, it tries to find the
7062 * corresponding application. On failure, the call_rcu to close the socket is
7063 * executed. If an application is found, it tries to delete it from the notify
7064 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7065 *
7066 * Note that an object needs to be allocated here so on ENOMEM failure, the
7067 * call RCU is not done but the rest of the cleanup is.
7068 */
7069 void ust_app_notify_sock_unregister(int sock)
7070 {
7071 int err_enomem = 0;
7072 struct lttng_ht_iter iter;
7073 struct ust_app *app;
7074 struct ust_app_notify_sock_obj *obj;
7075
7076 LTTNG_ASSERT(sock >= 0);
7077
7078 lttng::urcu::read_lock_guard read_lock;
7079
7080 obj = zmalloc<ust_app_notify_sock_obj>();
7081 if (!obj) {
7082 /*
7083 * An ENOMEM is kind of uncool. If this strikes we continue the
7084 * procedure but the call_rcu will not be called. In this case, we
7085 * accept the fd leak rather than possibly creating an unsynchronized
7086 * state between threads.
7087 *
7088 * TODO: The notify object should be created once the notify socket is
7089 * registered and stored independantely from the ust app object. The
7090 * tricky part is to synchronize the teardown of the application and
7091 * this notify object. Let's keep that in mind so we can avoid this
7092 * kind of shenanigans with ENOMEM in the teardown path.
7093 */
7094 err_enomem = 1;
7095 } else {
7096 obj->fd = sock;
7097 }
7098
7099 DBG("UST app notify socket unregister %d", sock);
7100
7101 /*
7102 * Lookup application by notify socket. If this fails, this means that the
7103 * hash table delete has already been done by the application
7104 * unregistration process so we can safely close the notify socket in a
7105 * call RCU.
7106 */
7107 app = find_app_by_notify_sock(sock);
7108 if (!app) {
7109 goto close_socket;
7110 }
7111
7112 iter.iter.node = &app->notify_sock_n.node;
7113
7114 /*
7115 * Whatever happens here either we fail or succeed, in both cases we have
7116 * to close the socket after a grace period to continue to the call RCU
7117 * here. If the deletion is successful, the application is not visible
7118 * anymore by other threads and is it fails it means that it was already
7119 * deleted from the hash table so either way we just have to close the
7120 * socket.
7121 */
7122 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
7123
7124 close_socket:
7125
7126 /*
7127 * Close socket after a grace period to avoid for the socket to be reused
7128 * before the application object is freed creating potential race between
7129 * threads trying to add unique in the global hash table.
7130 */
7131 if (!err_enomem) {
7132 call_rcu(&obj->head, close_notify_sock_rcu);
7133 }
7134 }
7135
7136 /*
7137 * Destroy a ust app data structure and free its memory.
7138 */
7139 void ust_app_destroy(struct ust_app *app)
7140 {
7141 if (!app) {
7142 return;
7143 }
7144
7145 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
7146 }
7147
7148 /*
7149 * Take a snapshot for a given UST session. The snapshot is sent to the given
7150 * output.
7151 *
7152 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7153 */
7154 enum lttng_error_code ust_app_snapshot_record(const struct ltt_ust_session *usess,
7155 const struct consumer_output *output,
7156 uint64_t nb_packets_per_stream)
7157 {
7158 int ret = 0;
7159 enum lttng_error_code status = LTTNG_OK;
7160 struct lttng_ht_iter iter;
7161 struct ust_app *app;
7162 char *trace_path = nullptr;
7163
7164 LTTNG_ASSERT(usess);
7165 LTTNG_ASSERT(output);
7166
7167 switch (usess->buffer_type) {
7168 case LTTNG_BUFFER_PER_UID:
7169 {
7170 struct buffer_reg_uid *reg;
7171
7172 lttng::urcu::read_lock_guard read_lock;
7173
7174 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7175 struct buffer_reg_channel *buf_reg_chan;
7176 struct consumer_socket *socket;
7177 char pathname[PATH_MAX];
7178 size_t consumer_path_offset = 0;
7179
7180 if (!reg->registry->reg.ust->_metadata_key) {
7181 /* Skip since no metadata is present */
7182 continue;
7183 }
7184
7185 /* Get consumer socket to use to push the metadata.*/
7186 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7187 usess->consumer);
7188 if (!socket) {
7189 status = LTTNG_ERR_INVALID;
7190 goto error;
7191 }
7192
7193 memset(pathname, 0, sizeof(pathname));
7194 ret = snprintf(pathname,
7195 sizeof(pathname),
7196 DEFAULT_UST_TRACE_UID_PATH,
7197 reg->uid,
7198 reg->bits_per_long);
7199 if (ret < 0) {
7200 PERROR("snprintf snapshot path");
7201 status = LTTNG_ERR_INVALID;
7202 goto error;
7203 }
7204 /* Free path allowed on previous iteration. */
7205 free(trace_path);
7206 trace_path = setup_channel_trace_path(
7207 usess->consumer, pathname, &consumer_path_offset);
7208 if (!trace_path) {
7209 status = LTTNG_ERR_INVALID;
7210 goto error;
7211 }
7212 /* Add the UST default trace dir to path. */
7213 cds_lfht_for_each_entry (
7214 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7215 status =
7216 consumer_snapshot_channel(socket,
7217 buf_reg_chan->consumer_key,
7218 output,
7219 0,
7220 &trace_path[consumer_path_offset],
7221 nb_packets_per_stream);
7222 if (status != LTTNG_OK) {
7223 goto error;
7224 }
7225 }
7226 status = consumer_snapshot_channel(socket,
7227 reg->registry->reg.ust->_metadata_key,
7228 output,
7229 1,
7230 &trace_path[consumer_path_offset],
7231 0);
7232 if (status != LTTNG_OK) {
7233 goto error;
7234 }
7235 }
7236
7237 break;
7238 }
7239 case LTTNG_BUFFER_PER_PID:
7240 {
7241 lttng::urcu::read_lock_guard read_lock;
7242
7243 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7244 struct consumer_socket *socket;
7245 struct lttng_ht_iter chan_iter;
7246 struct ust_app_channel *ua_chan;
7247 struct ust_app_session *ua_sess;
7248 lsu::registry_session *registry;
7249 char pathname[PATH_MAX];
7250 size_t consumer_path_offset = 0;
7251
7252 ua_sess = lookup_session_by_app(usess, app);
7253 if (!ua_sess) {
7254 /* Session not associated with this app. */
7255 continue;
7256 }
7257
7258 /* Get the right consumer socket for the application. */
7259 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, output);
7260 if (!socket) {
7261 status = LTTNG_ERR_INVALID;
7262 goto error;
7263 }
7264
7265 /* Add the UST default trace dir to path. */
7266 memset(pathname, 0, sizeof(pathname));
7267 ret = snprintf(pathname, sizeof(pathname), "%s", ua_sess->path);
7268 if (ret < 0) {
7269 status = LTTNG_ERR_INVALID;
7270 PERROR("snprintf snapshot path");
7271 goto error;
7272 }
7273 /* Free path allowed on previous iteration. */
7274 free(trace_path);
7275 trace_path = setup_channel_trace_path(
7276 usess->consumer, pathname, &consumer_path_offset);
7277 if (!trace_path) {
7278 status = LTTNG_ERR_INVALID;
7279 goto error;
7280 }
7281 cds_lfht_for_each_entry (
7282 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7283 status =
7284 consumer_snapshot_channel(socket,
7285 ua_chan->key,
7286 output,
7287 0,
7288 &trace_path[consumer_path_offset],
7289 nb_packets_per_stream);
7290 switch (status) {
7291 case LTTNG_OK:
7292 break;
7293 case LTTNG_ERR_CHAN_NOT_FOUND:
7294 continue;
7295 default:
7296 goto error;
7297 }
7298 }
7299
7300 registry = get_session_registry(ua_sess);
7301 if (!registry) {
7302 DBG("Application session is being torn down. Skip application.");
7303 continue;
7304 }
7305 status = consumer_snapshot_channel(socket,
7306 registry->_metadata_key,
7307 output,
7308 1,
7309 &trace_path[consumer_path_offset],
7310 0);
7311 switch (status) {
7312 case LTTNG_OK:
7313 break;
7314 case LTTNG_ERR_CHAN_NOT_FOUND:
7315 continue;
7316 default:
7317 goto error;
7318 }
7319 }
7320 break;
7321 }
7322 default:
7323 abort();
7324 break;
7325 }
7326
7327 error:
7328 free(trace_path);
7329 return status;
7330 }
7331
7332 /*
7333 * Return the size taken by one more packet per stream.
7334 */
7335 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess,
7336 uint64_t cur_nr_packets)
7337 {
7338 uint64_t tot_size = 0;
7339 struct ust_app *app;
7340 struct lttng_ht_iter iter;
7341
7342 LTTNG_ASSERT(usess);
7343
7344 switch (usess->buffer_type) {
7345 case LTTNG_BUFFER_PER_UID:
7346 {
7347 struct buffer_reg_uid *reg;
7348
7349 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7350 struct buffer_reg_channel *buf_reg_chan;
7351
7352 lttng::urcu::read_lock_guard read_lock;
7353
7354 cds_lfht_for_each_entry (
7355 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7356 if (cur_nr_packets >= buf_reg_chan->num_subbuf) {
7357 /*
7358 * Don't take channel into account if we
7359 * already grab all its packets.
7360 */
7361 continue;
7362 }
7363 tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count;
7364 }
7365 }
7366 break;
7367 }
7368 case LTTNG_BUFFER_PER_PID:
7369 {
7370 lttng::urcu::read_lock_guard read_lock;
7371
7372 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7373 struct ust_app_channel *ua_chan;
7374 struct ust_app_session *ua_sess;
7375 struct lttng_ht_iter chan_iter;
7376
7377 ua_sess = lookup_session_by_app(usess, app);
7378 if (!ua_sess) {
7379 /* Session not associated with this app. */
7380 continue;
7381 }
7382
7383 cds_lfht_for_each_entry (
7384 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7385 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
7386 /*
7387 * Don't take channel into account if we
7388 * already grab all its packets.
7389 */
7390 continue;
7391 }
7392 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
7393 }
7394 }
7395 break;
7396 }
7397 default:
7398 abort();
7399 break;
7400 }
7401
7402 return tot_size;
7403 }
7404
7405 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
7406 struct cds_list_head *buffer_reg_uid_list,
7407 struct consumer_output *consumer,
7408 uint64_t uchan_id,
7409 int overwrite,
7410 uint64_t *discarded,
7411 uint64_t *lost)
7412 {
7413 int ret;
7414 uint64_t consumer_chan_key;
7415
7416 *discarded = 0;
7417 *lost = 0;
7418
7419 ret = buffer_reg_uid_consumer_channel_key(
7420 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
7421 if (ret < 0) {
7422 /* Not found */
7423 ret = 0;
7424 goto end;
7425 }
7426
7427 if (overwrite) {
7428 ret = consumer_get_lost_packets(ust_session_id, consumer_chan_key, consumer, lost);
7429 } else {
7430 ret = consumer_get_discarded_events(
7431 ust_session_id, consumer_chan_key, consumer, discarded);
7432 }
7433
7434 end:
7435 return ret;
7436 }
7437
7438 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
7439 struct ltt_ust_channel *uchan,
7440 struct consumer_output *consumer,
7441 int overwrite,
7442 uint64_t *discarded,
7443 uint64_t *lost)
7444 {
7445 int ret = 0;
7446 struct lttng_ht_iter iter;
7447 struct lttng_ht_node_str *ua_chan_node;
7448 struct ust_app *app;
7449 struct ust_app_session *ua_sess;
7450 struct ust_app_channel *ua_chan;
7451
7452 *discarded = 0;
7453 *lost = 0;
7454
7455 /*
7456 * Iterate over every registered applications. Sum counters for
7457 * all applications containing requested session and channel.
7458 */
7459 lttng::urcu::read_lock_guard read_lock;
7460
7461 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7462 struct lttng_ht_iter uiter;
7463
7464 ua_sess = lookup_session_by_app(usess, app);
7465 if (ua_sess == nullptr) {
7466 continue;
7467 }
7468
7469 /* Get channel */
7470 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
7471 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
7472 /* If the session is found for the app, the channel must be there */
7473 LTTNG_ASSERT(ua_chan_node);
7474
7475 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
7476
7477 if (overwrite) {
7478 uint64_t _lost;
7479
7480 ret = consumer_get_lost_packets(usess->id, ua_chan->key, consumer, &_lost);
7481 if (ret < 0) {
7482 break;
7483 }
7484 (*lost) += _lost;
7485 } else {
7486 uint64_t _discarded;
7487
7488 ret = consumer_get_discarded_events(
7489 usess->id, ua_chan->key, consumer, &_discarded);
7490 if (ret < 0) {
7491 break;
7492 }
7493 (*discarded) += _discarded;
7494 }
7495 }
7496
7497 return ret;
7498 }
7499
7500 static int ust_app_regenerate_statedump(struct ltt_ust_session *usess, struct ust_app *app)
7501 {
7502 int ret = 0;
7503 struct ust_app_session *ua_sess;
7504
7505 DBG("Regenerating the metadata for ust app pid %d", app->pid);
7506
7507 lttng::urcu::read_lock_guard read_lock;
7508
7509 ua_sess = lookup_session_by_app(usess, app);
7510 if (ua_sess == nullptr) {
7511 /* The session is in teardown process. Ignore and continue. */
7512 goto end;
7513 }
7514
7515 pthread_mutex_lock(&ua_sess->lock);
7516
7517 if (ua_sess->deleted) {
7518 goto end_unlock;
7519 }
7520
7521 pthread_mutex_lock(&app->sock_lock);
7522 ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle);
7523 pthread_mutex_unlock(&app->sock_lock);
7524
7525 end_unlock:
7526 pthread_mutex_unlock(&ua_sess->lock);
7527
7528 end:
7529 health_code_update();
7530 return ret;
7531 }
7532
7533 /*
7534 * Regenerate the statedump for each app in the session.
7535 */
7536 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
7537 {
7538 int ret = 0;
7539 struct lttng_ht_iter iter;
7540 struct ust_app *app;
7541
7542 DBG("Regenerating the metadata for all UST apps");
7543
7544 lttng::urcu::read_lock_guard read_lock;
7545
7546 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7547 if (!app->compatible) {
7548 continue;
7549 }
7550
7551 ret = ust_app_regenerate_statedump(usess, app);
7552 if (ret < 0) {
7553 /* Continue to the next app even on error */
7554 continue;
7555 }
7556 }
7557
7558 return 0;
7559 }
7560
7561 /*
7562 * Rotate all the channels of a session.
7563 *
7564 * Return LTTNG_OK on success or else an LTTng error code.
7565 */
7566 enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
7567 {
7568 int ret;
7569 enum lttng_error_code cmd_ret = LTTNG_OK;
7570 struct lttng_ht_iter iter;
7571 struct ust_app *app;
7572 struct ltt_ust_session *usess = session->ust_session;
7573
7574 LTTNG_ASSERT(usess);
7575
7576 switch (usess->buffer_type) {
7577 case LTTNG_BUFFER_PER_UID:
7578 {
7579 struct buffer_reg_uid *reg;
7580
7581 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7582 struct buffer_reg_channel *buf_reg_chan;
7583 struct consumer_socket *socket;
7584 lttng::urcu::read_lock_guard read_lock;
7585
7586 /* Get consumer socket to use to push the metadata.*/
7587 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7588 usess->consumer);
7589 if (!socket) {
7590 cmd_ret = LTTNG_ERR_INVALID;
7591 goto error;
7592 }
7593
7594 /* Rotate the data channels. */
7595 cds_lfht_for_each_entry (
7596 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7597 ret = consumer_rotate_channel(socket,
7598 buf_reg_chan->consumer_key,
7599 usess->consumer,
7600 /* is_metadata_channel */ false);
7601 if (ret < 0) {
7602 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7603 goto error;
7604 }
7605 }
7606
7607 /*
7608 * The metadata channel might not be present.
7609 *
7610 * Consumer stream allocation can be done
7611 * asynchronously and can fail on intermediary
7612 * operations (i.e add context) and lead to data
7613 * channels created with no metadata channel.
7614 */
7615 if (!reg->registry->reg.ust->_metadata_key) {
7616 /* Skip since no metadata is present. */
7617 continue;
7618 }
7619
7620 {
7621 auto locked_registry = reg->registry->reg.ust->lock();
7622 (void) push_metadata(locked_registry, usess->consumer);
7623 }
7624
7625 ret = consumer_rotate_channel(socket,
7626 reg->registry->reg.ust->_metadata_key,
7627 usess->consumer,
7628 /* is_metadata_channel */ true);
7629 if (ret < 0) {
7630 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7631 goto error;
7632 }
7633 }
7634 break;
7635 }
7636 case LTTNG_BUFFER_PER_PID:
7637 {
7638 lttng::urcu::read_lock_guard read_lock;
7639
7640 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7641 struct consumer_socket *socket;
7642 struct lttng_ht_iter chan_iter;
7643 struct ust_app_channel *ua_chan;
7644 struct ust_app_session *ua_sess;
7645 lsu::registry_session *registry;
7646
7647 ua_sess = lookup_session_by_app(usess, app);
7648 if (!ua_sess) {
7649 /* Session not associated with this app. */
7650 continue;
7651 }
7652
7653 /* Get the right consumer socket for the application. */
7654 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7655 usess->consumer);
7656 if (!socket) {
7657 cmd_ret = LTTNG_ERR_INVALID;
7658 goto error;
7659 }
7660
7661 registry = get_session_registry(ua_sess);
7662 if (!registry) {
7663 DBG("Application session is being torn down. Skip application.");
7664 continue;
7665 }
7666
7667 /* Rotate the data channels. */
7668 cds_lfht_for_each_entry (
7669 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7670 ret = consumer_rotate_channel(socket,
7671 ua_chan->key,
7672 ua_sess->consumer,
7673 /* is_metadata_channel */ false);
7674 if (ret < 0) {
7675 /* Per-PID buffer and application going away. */
7676 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7677 continue;
7678 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7679 goto error;
7680 }
7681 }
7682
7683 /* Rotate the metadata channel. */
7684 {
7685 auto locked_registry = registry->lock();
7686
7687 (void) push_metadata(locked_registry, usess->consumer);
7688 }
7689 ret = consumer_rotate_channel(socket,
7690 registry->_metadata_key,
7691 ua_sess->consumer,
7692 /* is_metadata_channel */ true);
7693 if (ret < 0) {
7694 /* Per-PID buffer and application going away. */
7695 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7696 continue;
7697 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7698 goto error;
7699 }
7700 }
7701 break;
7702 }
7703 default:
7704 abort();
7705 break;
7706 }
7707
7708 cmd_ret = LTTNG_OK;
7709
7710 error:
7711 return cmd_ret;
7712 }
7713
7714 enum lttng_error_code ust_app_create_channel_subdirectories(const struct ltt_ust_session *usess)
7715 {
7716 enum lttng_error_code ret = LTTNG_OK;
7717 struct lttng_ht_iter iter;
7718 enum lttng_trace_chunk_status chunk_status;
7719 char *pathname_index;
7720 int fmt_ret;
7721
7722 LTTNG_ASSERT(usess->current_trace_chunk);
7723
7724 switch (usess->buffer_type) {
7725 case LTTNG_BUFFER_PER_UID:
7726 {
7727 struct buffer_reg_uid *reg;
7728 lttng::urcu::read_lock_guard read_lock;
7729
7730 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7731 fmt_ret = asprintf(&pathname_index,
7732 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH
7733 "/" DEFAULT_INDEX_DIR,
7734 reg->uid,
7735 reg->bits_per_long);
7736 if (fmt_ret < 0) {
7737 ERR("Failed to format channel index directory");
7738 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7739 goto error;
7740 }
7741
7742 /*
7743 * Create the index subdirectory which will take care
7744 * of implicitly creating the channel's path.
7745 */
7746 chunk_status = lttng_trace_chunk_create_subdirectory(
7747 usess->current_trace_chunk, pathname_index);
7748 free(pathname_index);
7749 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7750 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7751 goto error;
7752 }
7753 }
7754 break;
7755 }
7756 case LTTNG_BUFFER_PER_PID:
7757 {
7758 struct ust_app *app;
7759 lttng::urcu::read_lock_guard read_lock;
7760
7761 /*
7762 * Create the toplevel ust/ directory in case no apps are running.
7763 */
7764 chunk_status = lttng_trace_chunk_create_subdirectory(usess->current_trace_chunk,
7765 DEFAULT_UST_TRACE_DIR);
7766 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7767 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7768 goto error;
7769 }
7770
7771 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7772 struct ust_app_session *ua_sess;
7773 lsu::registry_session *registry;
7774
7775 ua_sess = lookup_session_by_app(usess, app);
7776 if (!ua_sess) {
7777 /* Session not associated with this app. */
7778 continue;
7779 }
7780
7781 registry = get_session_registry(ua_sess);
7782 if (!registry) {
7783 DBG("Application session is being torn down. Skip application.");
7784 continue;
7785 }
7786
7787 fmt_ret = asprintf(&pathname_index,
7788 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
7789 ua_sess->path);
7790 if (fmt_ret < 0) {
7791 ERR("Failed to format channel index directory");
7792 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7793 goto error;
7794 }
7795 /*
7796 * Create the index subdirectory which will take care
7797 * of implicitly creating the channel's path.
7798 */
7799 chunk_status = lttng_trace_chunk_create_subdirectory(
7800 usess->current_trace_chunk, pathname_index);
7801 free(pathname_index);
7802 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7803 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7804 goto error;
7805 }
7806 }
7807 break;
7808 }
7809 default:
7810 abort();
7811 }
7812
7813 ret = LTTNG_OK;
7814 error:
7815 return ret;
7816 }
7817
7818 /*
7819 * Clear all the channels of a session.
7820 *
7821 * Return LTTNG_OK on success or else an LTTng error code.
7822 */
7823 enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
7824 {
7825 int ret;
7826 enum lttng_error_code cmd_ret = LTTNG_OK;
7827 struct lttng_ht_iter iter;
7828 struct ust_app *app;
7829 struct ltt_ust_session *usess = session->ust_session;
7830
7831 LTTNG_ASSERT(usess);
7832
7833 if (usess->active) {
7834 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
7835 cmd_ret = LTTNG_ERR_FATAL;
7836 goto end;
7837 }
7838
7839 switch (usess->buffer_type) {
7840 case LTTNG_BUFFER_PER_UID:
7841 {
7842 struct buffer_reg_uid *reg;
7843 lttng::urcu::read_lock_guard read_lock;
7844
7845 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7846 struct buffer_reg_channel *buf_reg_chan;
7847 struct consumer_socket *socket;
7848
7849 /* Get consumer socket to use to push the metadata.*/
7850 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7851 usess->consumer);
7852 if (!socket) {
7853 cmd_ret = LTTNG_ERR_INVALID;
7854 goto error_socket;
7855 }
7856
7857 /* Clear the data channels. */
7858 cds_lfht_for_each_entry (
7859 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7860 ret = consumer_clear_channel(socket, buf_reg_chan->consumer_key);
7861 if (ret < 0) {
7862 goto error;
7863 }
7864 }
7865
7866 {
7867 auto locked_registry = reg->registry->reg.ust->lock();
7868 (void) push_metadata(locked_registry, usess->consumer);
7869 }
7870
7871 /*
7872 * Clear the metadata channel.
7873 * Metadata channel is not cleared per se but we still need to
7874 * perform a rotation operation on it behind the scene.
7875 */
7876 ret = consumer_clear_channel(socket, reg->registry->reg.ust->_metadata_key);
7877 if (ret < 0) {
7878 goto error;
7879 }
7880 }
7881 break;
7882 }
7883 case LTTNG_BUFFER_PER_PID:
7884 {
7885 lttng::urcu::read_lock_guard read_lock;
7886
7887 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7888 struct consumer_socket *socket;
7889 struct lttng_ht_iter chan_iter;
7890 struct ust_app_channel *ua_chan;
7891 struct ust_app_session *ua_sess;
7892 lsu::registry_session *registry;
7893
7894 ua_sess = lookup_session_by_app(usess, app);
7895 if (!ua_sess) {
7896 /* Session not associated with this app. */
7897 continue;
7898 }
7899
7900 /* Get the right consumer socket for the application. */
7901 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7902 usess->consumer);
7903 if (!socket) {
7904 cmd_ret = LTTNG_ERR_INVALID;
7905 goto error_socket;
7906 }
7907
7908 registry = get_session_registry(ua_sess);
7909 if (!registry) {
7910 DBG("Application session is being torn down. Skip application.");
7911 continue;
7912 }
7913
7914 /* Clear the data channels. */
7915 cds_lfht_for_each_entry (
7916 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7917 ret = consumer_clear_channel(socket, ua_chan->key);
7918 if (ret < 0) {
7919 /* Per-PID buffer and application going away. */
7920 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7921 continue;
7922 }
7923 goto error;
7924 }
7925 }
7926
7927 {
7928 auto locked_registry = registry->lock();
7929 (void) push_metadata(locked_registry, usess->consumer);
7930 }
7931
7932 /*
7933 * Clear the metadata channel.
7934 * Metadata channel is not cleared per se but we still need to
7935 * perform rotation operation on it behind the scene.
7936 */
7937 ret = consumer_clear_channel(socket, registry->_metadata_key);
7938 if (ret < 0) {
7939 /* Per-PID buffer and application going away. */
7940 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7941 continue;
7942 }
7943 goto error;
7944 }
7945 }
7946 break;
7947 }
7948 default:
7949 abort();
7950 break;
7951 }
7952
7953 cmd_ret = LTTNG_OK;
7954 goto end;
7955
7956 error:
7957 switch (-ret) {
7958 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
7959 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
7960 break;
7961 default:
7962 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
7963 }
7964
7965 error_socket:
7966 end:
7967 return cmd_ret;
7968 }
7969
7970 /*
7971 * This function skips the metadata channel as the begin/end timestamps of a
7972 * metadata packet are useless.
7973 *
7974 * Moreover, opening a packet after a "clear" will cause problems for live
7975 * sessions as it will introduce padding that was not part of the first trace
7976 * chunk. The relay daemon expects the content of the metadata stream of
7977 * successive metadata trace chunks to be strict supersets of one another.
7978 *
7979 * For example, flushing a packet at the beginning of the metadata stream of
7980 * a trace chunk resulting from a "clear" session command will cause the
7981 * size of the metadata stream of the new trace chunk to not match the size of
7982 * the metadata stream of the original chunk. This will confuse the relay
7983 * daemon as the same "offset" in a metadata stream will no longer point
7984 * to the same content.
7985 */
7986 enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
7987 {
7988 enum lttng_error_code ret = LTTNG_OK;
7989 struct lttng_ht_iter iter;
7990 struct ltt_ust_session *usess = session->ust_session;
7991
7992 LTTNG_ASSERT(usess);
7993
7994 switch (usess->buffer_type) {
7995 case LTTNG_BUFFER_PER_UID:
7996 {
7997 struct buffer_reg_uid *reg;
7998
7999 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
8000 struct buffer_reg_channel *buf_reg_chan;
8001 struct consumer_socket *socket;
8002 lttng::urcu::read_lock_guard read_lock;
8003
8004 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
8005 usess->consumer);
8006 if (!socket) {
8007 ret = LTTNG_ERR_FATAL;
8008 goto error;
8009 }
8010
8011 cds_lfht_for_each_entry (
8012 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
8013 const int open_ret = consumer_open_channel_packets(
8014 socket, buf_reg_chan->consumer_key);
8015
8016 if (open_ret < 0) {
8017 ret = LTTNG_ERR_UNK;
8018 goto error;
8019 }
8020 }
8021 }
8022 break;
8023 }
8024 case LTTNG_BUFFER_PER_PID:
8025 {
8026 struct ust_app *app;
8027 lttng::urcu::read_lock_guard read_lock;
8028
8029 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
8030 struct consumer_socket *socket;
8031 struct lttng_ht_iter chan_iter;
8032 struct ust_app_channel *ua_chan;
8033 struct ust_app_session *ua_sess;
8034 lsu::registry_session *registry;
8035
8036 ua_sess = lookup_session_by_app(usess, app);
8037 if (!ua_sess) {
8038 /* Session not associated with this app. */
8039 continue;
8040 }
8041
8042 /* Get the right consumer socket for the application. */
8043 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
8044 usess->consumer);
8045 if (!socket) {
8046 ret = LTTNG_ERR_FATAL;
8047 goto error;
8048 }
8049
8050 registry = get_session_registry(ua_sess);
8051 if (!registry) {
8052 DBG("Application session is being torn down. Skip application.");
8053 continue;
8054 }
8055
8056 cds_lfht_for_each_entry (
8057 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
8058 const int open_ret =
8059 consumer_open_channel_packets(socket, ua_chan->key);
8060
8061 if (open_ret < 0) {
8062 /*
8063 * Per-PID buffer and application going
8064 * away.
8065 */
8066 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
8067 continue;
8068 }
8069
8070 ret = LTTNG_ERR_UNK;
8071 goto error;
8072 }
8073 }
8074 }
8075 break;
8076 }
8077 default:
8078 abort();
8079 break;
8080 }
8081
8082 error:
8083 return ret;
8084 }
8085
8086 lsu::ctl_field_quirks ust_app::ctl_field_quirks() const
8087 {
8088 /*
8089 * Application contexts are expressed as variants. LTTng-UST announces
8090 * those by registering an enumeration named `..._tag`. It then registers a
8091 * variant as part of the event context that contains the various possible
8092 * types.
8093 *
8094 * Unfortunately, the names used in the enumeration and variant don't
8095 * match: the enumeration names are all prefixed with an underscore while
8096 * the variant type tag fields aren't.
8097 *
8098 * While the CTF 1.8.3 specification mentions that
8099 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8100 * 1.x (and possibly others) expect a perfect match between the names used
8101 * by tags and variants.
8102 *
8103 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8104 * the variant's fields are modified to match the mappings of its tag.
8105 *
8106 * From ABI version >= 10.x, the variant fields and tag mapping names
8107 * correctly match, making this quirk unnecessary.
8108 */
8109 return v_major <= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS :
8110 lsu::ctl_field_quirks::NONE;
8111 }
This page took 0.224867 seconds and 5 git commands to generate.