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