Fix: sessiond: leak of application context in channel
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10
11 #include "buffer-registry.hpp"
12 #include "condition-internal.hpp"
13 #include "event-notifier-error-accounting.hpp"
14 #include "event.hpp"
15 #include "fd-limit.hpp"
16 #include "field.hpp"
17 #include "health-sessiond.hpp"
18 #include "lttng-sessiond.hpp"
19 #include "lttng-ust-ctl.hpp"
20 #include "lttng-ust-error.hpp"
21 #include "notification-thread-commands.hpp"
22 #include "session.hpp"
23 #include "ust-app.hpp"
24 #include "ust-consumer.hpp"
25 #include "ust-field-convert.hpp"
26 #include "utils.hpp"
27
28 #include <common/bytecode/bytecode.hpp>
29 #include <common/common.hpp>
30 #include <common/compat/errno.hpp>
31 #include <common/exception.hpp>
32 #include <common/format.hpp>
33 #include <common/hashtable/utils.hpp>
34 #include <common/make-unique.hpp>
35 #include <common/sessiond-comm/sessiond-comm.hpp>
36 #include <common/urcu.hpp>
37
38 #include <lttng/condition/condition.h>
39 #include <lttng/condition/event-rule-matches-internal.hpp>
40 #include <lttng/condition/event-rule-matches.h>
41 #include <lttng/event-rule/event-rule-internal.hpp>
42 #include <lttng/event-rule/event-rule.h>
43 #include <lttng/event-rule/user-tracepoint.h>
44 #include <lttng/trigger/trigger-internal.hpp>
45
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <inttypes.h>
49 #include <pthread.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/mman.h>
55 #include <sys/stat.h>
56 #include <sys/types.h>
57 #include <unistd.h>
58 #include <urcu/compiler.h>
59 #include <vector>
60
61 namespace lsu = lttng::sessiond::ust;
62 namespace lst = lttng::sessiond::trace;
63
64 struct lttng_ht *ust_app_ht;
65 struct lttng_ht *ust_app_ht_by_sock;
66 struct lttng_ht *ust_app_ht_by_notify_sock;
67
68 static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess);
69
70 /* Next available channel key. Access under next_channel_key_lock. */
71 static uint64_t _next_channel_key;
72 static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER;
73
74 /* Next available session ID. Access under next_session_id_lock. */
75 static uint64_t _next_session_id;
76 static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
77
78 namespace {
79
80 /*
81 * Return the session registry according to the buffer type of the given
82 * session.
83 *
84 * A registry per UID object MUST exists before calling this function or else
85 * it LTTNG_ASSERT() if not found. RCU read side lock must be acquired.
86 */
87 static lsu::registry_session *get_session_registry(const struct ust_app_session *ua_sess)
88 {
89 lsu::registry_session *registry = nullptr;
90
91 LTTNG_ASSERT(ua_sess);
92
93 switch (ua_sess->buffer_type) {
94 case LTTNG_BUFFER_PER_PID:
95 {
96 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
97 if (!reg_pid) {
98 goto error;
99 }
100 registry = reg_pid->registry->reg.ust;
101 break;
102 }
103 case LTTNG_BUFFER_PER_UID:
104 {
105 struct buffer_reg_uid *reg_uid =
106 buffer_reg_uid_find(ua_sess->tracing_id,
107 ua_sess->bits_per_long,
108 lttng_credentials_get_uid(&ua_sess->real_credentials));
109 if (!reg_uid) {
110 goto error;
111 }
112 registry = reg_uid->registry->reg.ust;
113 break;
114 }
115 default:
116 abort();
117 };
118
119 error:
120 return registry;
121 }
122
123 lsu::registry_session::locked_ptr get_locked_session_registry(const struct ust_app_session *ua_sess)
124 {
125 auto session = get_session_registry(ua_sess);
126 if (session) {
127 pthread_mutex_lock(&session->_lock);
128 }
129
130 return lsu::registry_session::locked_ptr{ session };
131 }
132 } /* namespace */
133
134 /*
135 * Return the incremented value of next_channel_key.
136 */
137 static uint64_t get_next_channel_key()
138 {
139 uint64_t ret;
140
141 pthread_mutex_lock(&next_channel_key_lock);
142 ret = ++_next_channel_key;
143 pthread_mutex_unlock(&next_channel_key_lock);
144 return ret;
145 }
146
147 /*
148 * Return the atomically incremented value of next_session_id.
149 */
150 static uint64_t get_next_session_id()
151 {
152 uint64_t ret;
153
154 pthread_mutex_lock(&next_session_id_lock);
155 ret = ++_next_session_id;
156 pthread_mutex_unlock(&next_session_id_lock);
157 return ret;
158 }
159
160 static void copy_channel_attr_to_ustctl(struct lttng_ust_ctl_consumer_channel_attr *attr,
161 struct lttng_ust_abi_channel_attr *uattr)
162 {
163 /* Copy event attributes since the layout is different. */
164 attr->subbuf_size = uattr->subbuf_size;
165 attr->num_subbuf = uattr->num_subbuf;
166 attr->overwrite = uattr->overwrite;
167 attr->switch_timer_interval = uattr->switch_timer_interval;
168 attr->read_timer_interval = uattr->read_timer_interval;
169 attr->output = (lttng_ust_abi_output) uattr->output;
170 attr->blocking_timeout = uattr->u.s.blocking_timeout;
171 }
172
173 /*
174 * Match function for the hash table lookup.
175 *
176 * It matches an ust app event based on three attributes which are the event
177 * name, the filter bytecode and the loglevel.
178 */
179 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
180 {
181 struct ust_app_event *event;
182 const struct ust_app_ht_key *key;
183
184 LTTNG_ASSERT(node);
185 LTTNG_ASSERT(_key);
186
187 event = caa_container_of(node, struct ust_app_event, node.node);
188 key = (ust_app_ht_key *) _key;
189
190 /* Match the 4 elements of the key: name, filter, loglevel, exclusions */
191
192 /* Event name */
193 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
194 goto no_match;
195 }
196
197 /* Event loglevel. */
198 if (!loglevels_match(event->attr.loglevel_type,
199 event->attr.loglevel,
200 key->loglevel_type,
201 key->loglevel_value,
202 LTTNG_UST_ABI_LOGLEVEL_ALL)) {
203 goto no_match;
204 }
205
206 /* One of the filters is NULL, fail. */
207 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
208 goto no_match;
209 }
210
211 if (key->filter && event->filter) {
212 /* Both filters exists, check length followed by the bytecode. */
213 if (event->filter->len != key->filter->len ||
214 memcmp(event->filter->data, key->filter->data, event->filter->len) != 0) {
215 goto no_match;
216 }
217 }
218
219 /* One of the exclusions is NULL, fail. */
220 if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) {
221 goto no_match;
222 }
223
224 if (key->exclusion && event->exclusion) {
225 /* Both exclusions exists, check count followed by the names. */
226 if (event->exclusion->count != key->exclusion->count ||
227 memcmp(event->exclusion->names,
228 key->exclusion->names,
229 event->exclusion->count * LTTNG_UST_ABI_SYM_NAME_LEN) != 0) {
230 goto no_match;
231 }
232 }
233
234 /* Match. */
235 return 1;
236
237 no_match:
238 return 0;
239 }
240
241 /*
242 * Unique add of an ust app event in the given ht. This uses the custom
243 * ht_match_ust_app_event match function and the event name as hash.
244 */
245 static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, struct ust_app_event *event)
246 {
247 struct cds_lfht_node *node_ptr;
248 struct ust_app_ht_key key;
249 struct lttng_ht *ht;
250
251 LTTNG_ASSERT(ua_chan);
252 LTTNG_ASSERT(ua_chan->events);
253 LTTNG_ASSERT(event);
254
255 ht = ua_chan->events;
256 key.name = event->attr.name;
257 key.filter = event->filter;
258 key.loglevel_type = (lttng_ust_abi_loglevel_type) event->attr.loglevel_type;
259 key.loglevel_value = event->attr.loglevel;
260 key.exclusion = event->exclusion;
261
262 node_ptr = cds_lfht_add_unique(ht->ht,
263 ht->hash_fct(event->node.key, lttng_ht_seed),
264 ht_match_ust_app_event,
265 &key,
266 &event->node.node);
267 LTTNG_ASSERT(node_ptr == &event->node.node);
268 }
269
270 /*
271 * Close the notify socket from the given RCU head object. This MUST be called
272 * through a call_rcu().
273 */
274 static void close_notify_sock_rcu(struct rcu_head *head)
275 {
276 int ret;
277 struct ust_app_notify_sock_obj *obj =
278 lttng::utils::container_of(head, &ust_app_notify_sock_obj::head);
279
280 /* Must have a valid fd here. */
281 LTTNG_ASSERT(obj->fd >= 0);
282
283 ret = close(obj->fd);
284 if (ret) {
285 ERR("close notify sock %d RCU", obj->fd);
286 }
287 lttng_fd_put(LTTNG_FD_APPS, 1);
288
289 free(obj);
290 }
291
292 /*
293 * Delete ust context safely. RCU read lock must be held before calling
294 * this function.
295 */
296 static void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx, struct ust_app *app)
297 {
298 int ret;
299
300 LTTNG_ASSERT(ua_ctx);
301 ASSERT_RCU_READ_LOCKED();
302
303 if (ua_ctx->obj) {
304 pthread_mutex_lock(&app->sock_lock);
305 ret = lttng_ust_ctl_release_object(sock, ua_ctx->obj);
306 pthread_mutex_unlock(&app->sock_lock);
307 if (ret < 0) {
308 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
309 DBG3("UST app release ctx failed. Application is dead: pid = %d, sock = %d",
310 app->pid,
311 app->sock);
312 } else if (ret == -EAGAIN) {
313 WARN("UST app release ctx failed. Communication time out: pid = %d, sock = %d",
314 app->pid,
315 app->sock);
316 } else {
317 ERR("UST app release ctx obj handle %d failed with ret %d: pid = %d, sock = %d",
318 ua_ctx->obj->handle,
319 ret,
320 app->pid,
321 app->sock);
322 }
323 }
324 free(ua_ctx->obj);
325 }
326
327 if (ua_ctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_APP_CONTEXT) {
328 free(ua_ctx->ctx.u.app_ctx.provider_name);
329 free(ua_ctx->ctx.u.app_ctx.ctx_name);
330 }
331
332 free(ua_ctx);
333 }
334
335 /*
336 * Delete ust app event safely. RCU read lock must be held before calling
337 * this function.
338 */
339 static void delete_ust_app_event(int sock, struct ust_app_event *ua_event, struct ust_app *app)
340 {
341 int ret;
342
343 LTTNG_ASSERT(ua_event);
344 ASSERT_RCU_READ_LOCKED();
345
346 free(ua_event->filter);
347 if (ua_event->exclusion != nullptr)
348 free(ua_event->exclusion);
349 if (ua_event->obj != nullptr) {
350 pthread_mutex_lock(&app->sock_lock);
351 ret = lttng_ust_ctl_release_object(sock, ua_event->obj);
352 pthread_mutex_unlock(&app->sock_lock);
353 if (ret < 0) {
354 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
355 DBG3("UST app release event failed. Application is dead: pid = %d, sock = %d",
356 app->pid,
357 app->sock);
358 } else if (ret == -EAGAIN) {
359 WARN("UST app release event failed. Communication time out: pid = %d, sock = %d",
360 app->pid,
361 app->sock);
362 } else {
363 ERR("UST app release event obj failed with ret %d: pid = %d, sock = %d",
364 ret,
365 app->pid,
366 app->sock);
367 }
368 }
369 free(ua_event->obj);
370 }
371 free(ua_event);
372 }
373
374 /*
375 * Delayed reclaim of a ust_app_event_notifier_rule object. This MUST be called
376 * through a call_rcu().
377 */
378 static void free_ust_app_event_notifier_rule_rcu(struct rcu_head *head)
379 {
380 struct ust_app_event_notifier_rule *obj =
381 lttng::utils::container_of(head, &ust_app_event_notifier_rule::rcu_head);
382
383 free(obj);
384 }
385
386 /*
387 * Delete ust app event notifier rule safely.
388 */
389 static void delete_ust_app_event_notifier_rule(
390 int sock, struct ust_app_event_notifier_rule *ua_event_notifier_rule, struct ust_app *app)
391 {
392 int ret;
393
394 LTTNG_ASSERT(ua_event_notifier_rule);
395
396 if (ua_event_notifier_rule->exclusion != nullptr) {
397 free(ua_event_notifier_rule->exclusion);
398 }
399
400 if (ua_event_notifier_rule->obj != nullptr) {
401 pthread_mutex_lock(&app->sock_lock);
402 ret = lttng_ust_ctl_release_object(sock, ua_event_notifier_rule->obj);
403 pthread_mutex_unlock(&app->sock_lock);
404 if (ret < 0) {
405 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
406 DBG3("UST app release event notifier failed. Application is dead: pid = %d, sock = %d",
407 app->pid,
408 app->sock);
409 } else if (ret == -EAGAIN) {
410 WARN("UST app release event notifier failed. Communication time out: pid = %d, sock = %d",
411 app->pid,
412 app->sock);
413 } else {
414 ERR("UST app release event notifier failed with ret %d: pid = %d, sock = %d",
415 ret,
416 app->pid,
417 app->sock);
418 }
419 }
420
421 free(ua_event_notifier_rule->obj);
422 }
423
424 lttng_trigger_put(ua_event_notifier_rule->trigger);
425 call_rcu(&ua_event_notifier_rule->rcu_head, free_ust_app_event_notifier_rule_rcu);
426 }
427
428 /*
429 * Release ust data object of the given stream.
430 *
431 * Return 0 on success or else a negative value.
432 */
433 static int release_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app)
434 {
435 int ret = 0;
436
437 LTTNG_ASSERT(stream);
438
439 if (stream->obj) {
440 pthread_mutex_lock(&app->sock_lock);
441 ret = lttng_ust_ctl_release_object(sock, stream->obj);
442 pthread_mutex_unlock(&app->sock_lock);
443 if (ret < 0) {
444 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
445 DBG3("UST app release stream failed. Application is dead: pid = %d, sock = %d",
446 app->pid,
447 app->sock);
448 } else if (ret == -EAGAIN) {
449 WARN("UST app release stream failed. Communication time out: pid = %d, sock = %d",
450 app->pid,
451 app->sock);
452 } else {
453 ERR("UST app release stream obj failed with ret %d: pid = %d, sock = %d",
454 ret,
455 app->pid,
456 app->sock);
457 }
458 }
459 lttng_fd_put(LTTNG_FD_APPS, 2);
460 free(stream->obj);
461 }
462
463 return ret;
464 }
465
466 /*
467 * Delete ust app stream safely. RCU read lock must be held before calling
468 * this function.
469 */
470 static void delete_ust_app_stream(int sock, struct ust_app_stream *stream, struct ust_app *app)
471 {
472 LTTNG_ASSERT(stream);
473 ASSERT_RCU_READ_LOCKED();
474
475 (void) release_ust_app_stream(sock, stream, app);
476 free(stream);
477 }
478
479 static void delete_ust_app_channel_rcu(struct rcu_head *head)
480 {
481 struct ust_app_channel *ua_chan =
482 lttng::utils::container_of(head, &ust_app_channel::rcu_head);
483
484 lttng_ht_destroy(ua_chan->ctx);
485 lttng_ht_destroy(ua_chan->events);
486 free(ua_chan);
487 }
488
489 /*
490 * Extract the lost packet or discarded events counter when the channel is
491 * being deleted and store the value in the parent channel so we can
492 * access it from lttng list and at stop/destroy.
493 *
494 * The session list lock must be held by the caller.
495 */
496 static void save_per_pid_lost_discarded_counters(struct ust_app_channel *ua_chan)
497 {
498 uint64_t discarded = 0, lost = 0;
499 struct ltt_session *session;
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 session = session_find_by_id(ua_chan->session->tracing_id);
508 if (!session || !session->ust_session) {
509 /*
510 * Not finding the session is not an error because there are
511 * multiple ways the channels can be torn down.
512 *
513 * 1) The session daemon can initiate the destruction of the
514 * ust app session after receiving a destroy command or
515 * during its shutdown/teardown.
516 * 2) The application, since we are in per-pid tracing, is
517 * unregistering and tearing down its ust app session.
518 *
519 * Both paths are protected by the session list lock which
520 * ensures that the accounting of lost packets and discarded
521 * events is done exactly once. The session is then unpublished
522 * from the session list, resulting in this condition.
523 */
524 goto end;
525 }
526
527 if (ua_chan->attr.overwrite) {
528 consumer_get_lost_packets(ua_chan->session->tracing_id,
529 ua_chan->key,
530 session->ust_session->consumer,
531 &lost);
532 } else {
533 consumer_get_discarded_events(ua_chan->session->tracing_id,
534 ua_chan->key,
535 session->ust_session->consumer,
536 &discarded);
537 }
538 uchan = trace_ust_find_channel_by_name(session->ust_session->domain_global.channels,
539 ua_chan->name);
540 if (!uchan) {
541 ERR("Missing UST channel to store discarded counters");
542 goto end;
543 }
544
545 uchan->per_pid_closed_app_discarded += discarded;
546 uchan->per_pid_closed_app_lost += lost;
547
548 end:
549 if (session) {
550 session_put(session);
551 }
552 }
553
554 /*
555 * Delete ust app channel safely. RCU read lock must be held before calling
556 * this function.
557 *
558 * The session list lock must be held by the caller.
559 */
560 static void delete_ust_app_channel(int sock,
561 struct ust_app_channel *ua_chan,
562 struct ust_app *app,
563 const lsu::registry_session::locked_ptr& locked_registry)
564 {
565 int ret;
566 struct lttng_ht_iter iter;
567 struct ust_app_event *ua_event;
568 struct ust_app_ctx *ua_ctx;
569 struct ust_app_stream *stream, *stmp;
570
571 LTTNG_ASSERT(ua_chan);
572 ASSERT_RCU_READ_LOCKED();
573
574 DBG3("UST app deleting channel %s", ua_chan->name);
575
576 /* Wipe stream */
577 cds_list_for_each_entry_safe (stream, stmp, &ua_chan->streams.head, list) {
578 cds_list_del(&stream->list);
579 delete_ust_app_stream(sock, stream, app);
580 }
581
582 /* Wipe context */
583 cds_lfht_for_each_entry (ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
584 cds_list_del(&ua_ctx->list);
585 ret = lttng_ht_del(ua_chan->ctx, &iter);
586 LTTNG_ASSERT(!ret);
587 delete_ust_app_ctx(sock, ua_ctx, app);
588 }
589
590 /* Wipe events */
591 cds_lfht_for_each_entry (ua_chan->events->ht, &iter.iter, ua_event, node.node) {
592 ret = lttng_ht_del(ua_chan->events, &iter);
593 LTTNG_ASSERT(!ret);
594 delete_ust_app_event(sock, ua_event, app);
595 }
596
597 if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) {
598 /* Wipe and free registry from session registry. */
599 if (locked_registry) {
600 try {
601 locked_registry->remove_channel(ua_chan->key, sock >= 0);
602 } catch (const std::exception& ex) {
603 DBG("Could not find channel for removal: %s", ex.what());
604 }
605 }
606
607 /*
608 * A negative socket can be used by the caller when
609 * cleaning-up a ua_chan in an error path. Skip the
610 * accounting in this case.
611 */
612 if (sock >= 0) {
613 save_per_pid_lost_discarded_counters(ua_chan);
614 }
615 }
616
617 if (ua_chan->obj != nullptr) {
618 /* Remove channel from application UST object descriptor. */
619 iter.iter.node = &ua_chan->ust_objd_node.node;
620 ret = lttng_ht_del(app->ust_objd, &iter);
621 LTTNG_ASSERT(!ret);
622 pthread_mutex_lock(&app->sock_lock);
623 ret = lttng_ust_ctl_release_object(sock, ua_chan->obj);
624 pthread_mutex_unlock(&app->sock_lock);
625 if (ret < 0) {
626 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
627 DBG3("UST app channel %s release failed. Application is dead: pid = %d, sock = %d",
628 ua_chan->name,
629 app->pid,
630 app->sock);
631 } else if (ret == -EAGAIN) {
632 WARN("UST app channel %s release failed. Communication time out: pid = %d, sock = %d",
633 ua_chan->name,
634 app->pid,
635 app->sock);
636 } else {
637 ERR("UST app channel %s release failed with ret %d: pid = %d, sock = %d",
638 ua_chan->name,
639 ret,
640 app->pid,
641 app->sock);
642 }
643 }
644 lttng_fd_put(LTTNG_FD_APPS, 1);
645 free(ua_chan->obj);
646 }
647 call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu);
648 }
649
650 int ust_app_register_done(struct ust_app *app)
651 {
652 int ret;
653
654 pthread_mutex_lock(&app->sock_lock);
655 ret = lttng_ust_ctl_register_done(app->sock);
656 pthread_mutex_unlock(&app->sock_lock);
657 return ret;
658 }
659
660 int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data)
661 {
662 int ret, sock;
663
664 if (app) {
665 pthread_mutex_lock(&app->sock_lock);
666 sock = app->sock;
667 } else {
668 sock = -1;
669 }
670 ret = lttng_ust_ctl_release_object(sock, data);
671 if (app) {
672 pthread_mutex_unlock(&app->sock_lock);
673 }
674 return ret;
675 }
676
677 /*
678 * Push metadata to consumer socket.
679 *
680 * RCU read-side lock must be held to guarantee existence of socket.
681 * Must be called with the ust app session lock held.
682 * Must be called with the registry lock held.
683 *
684 * On success, return the len of metadata pushed or else a negative value.
685 * Returning a -EPIPE return value means we could not send the metadata,
686 * but it can be caused by recoverable errors (e.g. the application has
687 * terminated concurrently).
688 */
689 ssize_t ust_app_push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
690 struct consumer_socket *socket,
691 int send_zero_data)
692 {
693 int ret;
694 char *metadata_str = nullptr;
695 size_t len, offset, new_metadata_len_sent;
696 ssize_t ret_val;
697 uint64_t metadata_key, metadata_version;
698
699 LTTNG_ASSERT(locked_registry);
700 LTTNG_ASSERT(socket);
701 ASSERT_RCU_READ_LOCKED();
702
703 metadata_key = locked_registry->_metadata_key;
704
705 /*
706 * Means that no metadata was assigned to the session. This can
707 * happens if no start has been done previously.
708 */
709 if (!metadata_key) {
710 return 0;
711 }
712
713 offset = locked_registry->_metadata_len_sent;
714 len = locked_registry->_metadata_len - locked_registry->_metadata_len_sent;
715 new_metadata_len_sent = locked_registry->_metadata_len;
716 metadata_version = locked_registry->_metadata_version;
717 if (len == 0) {
718 DBG3("No metadata to push for metadata key %" PRIu64,
719 locked_registry->_metadata_key);
720 ret_val = len;
721 if (send_zero_data) {
722 DBG("No metadata to push");
723 goto push_data;
724 }
725 goto end;
726 }
727
728 /* Allocate only what we have to send. */
729 metadata_str = calloc<char>(len);
730 if (!metadata_str) {
731 PERROR("zmalloc ust app metadata string");
732 ret_val = -ENOMEM;
733 goto error;
734 }
735 /* Copy what we haven't sent out. */
736 memcpy(metadata_str, locked_registry->_metadata + offset, len);
737
738 push_data:
739 pthread_mutex_unlock(&locked_registry->_lock);
740 /*
741 * We need to unlock the registry while we push metadata to
742 * break a circular dependency between the consumerd metadata
743 * lock and the sessiond registry lock. Indeed, pushing metadata
744 * to the consumerd awaits that it gets pushed all the way to
745 * relayd, but doing so requires grabbing the metadata lock. If
746 * a concurrent metadata request is being performed by
747 * consumerd, this can try to grab the registry lock on the
748 * sessiond while holding the metadata lock on the consumer
749 * daemon. Those push and pull schemes are performed on two
750 * different bidirectionnal communication sockets.
751 */
752 ret = consumer_push_metadata(
753 socket, metadata_key, metadata_str, len, offset, metadata_version);
754 pthread_mutex_lock(&locked_registry->_lock);
755 if (ret < 0) {
756 /*
757 * There is an acceptable race here between the registry
758 * metadata key assignment and the creation on the
759 * consumer. The session daemon can concurrently push
760 * metadata for this registry while being created on the
761 * consumer since the metadata key of the registry is
762 * assigned *before* it is setup to avoid the consumer
763 * to ask for metadata that could possibly be not found
764 * in the session daemon.
765 *
766 * The metadata will get pushed either by the session
767 * being stopped or the consumer requesting metadata if
768 * that race is triggered.
769 */
770 if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) {
771 ret = 0;
772 } else {
773 ERR("Error pushing metadata to consumer");
774 }
775 ret_val = ret;
776 goto error_push;
777 } else {
778 /*
779 * Metadata may have been concurrently pushed, since
780 * we're not holding the registry lock while pushing to
781 * consumer. This is handled by the fact that we send
782 * the metadata content, size, and the offset at which
783 * that metadata belongs. This may arrive out of order
784 * on the consumer side, and the consumer is able to
785 * deal with overlapping fragments. The consumer
786 * supports overlapping fragments, which must be
787 * contiguous starting from offset 0. We keep the
788 * largest metadata_len_sent value of the concurrent
789 * send.
790 */
791 locked_registry->_metadata_len_sent =
792 std::max(locked_registry->_metadata_len_sent, new_metadata_len_sent);
793 }
794 free(metadata_str);
795 return len;
796
797 end:
798 error:
799 if (ret_val) {
800 /*
801 * On error, flag the registry that the metadata is
802 * closed. We were unable to push anything and this
803 * means that either the consumer is not responding or
804 * the metadata cache has been destroyed on the
805 * consumer.
806 */
807 locked_registry->_metadata_closed = true;
808 }
809 error_push:
810 free(metadata_str);
811 return ret_val;
812 }
813
814 /*
815 * For a given application and session, push metadata to consumer.
816 * Either sock or consumer is required : if sock is NULL, the default
817 * socket to send the metadata is retrieved from consumer, if sock
818 * is not NULL we use it to send the metadata.
819 * RCU read-side lock must be held while calling this function,
820 * therefore ensuring existence of registry. It also ensures existence
821 * of socket throughout this function.
822 *
823 * Return 0 on success else a negative error.
824 * Returning a -EPIPE return value means we could not send the metadata,
825 * but it can be caused by recoverable errors (e.g. the application has
826 * terminated concurrently).
827 */
828 static int push_metadata(const lsu::registry_session::locked_ptr& locked_registry,
829 struct consumer_output *consumer)
830 {
831 int ret_val;
832 ssize_t ret;
833 struct consumer_socket *socket;
834
835 LTTNG_ASSERT(locked_registry);
836 LTTNG_ASSERT(consumer);
837 ASSERT_RCU_READ_LOCKED();
838
839 if (locked_registry->_metadata_closed) {
840 ret_val = -EPIPE;
841 goto error;
842 }
843
844 /* Get consumer socket to use to push the metadata.*/
845 socket = consumer_find_socket_by_bitness(locked_registry->abi.bits_per_long, consumer);
846 if (!socket) {
847 ret_val = -1;
848 goto error;
849 }
850
851 ret = ust_app_push_metadata(locked_registry, socket, 0);
852 if (ret < 0) {
853 ret_val = ret;
854 goto error;
855 }
856 return 0;
857
858 error:
859 return ret_val;
860 }
861
862 /*
863 * Send to the consumer a close metadata command for the given session. Once
864 * done, the metadata channel is deleted and the session metadata pointer is
865 * nullified. The session lock MUST be held unless the application is
866 * in the destroy path.
867 *
868 * Do not hold the registry lock while communicating with the consumerd, because
869 * doing so causes inter-process deadlocks between consumerd and sessiond with
870 * the metadata request notification.
871 *
872 * Return 0 on success else a negative value.
873 */
874 static int close_metadata(uint64_t metadata_key,
875 unsigned int consumer_bitness,
876 struct consumer_output *consumer)
877 {
878 int ret;
879 struct consumer_socket *socket;
880 lttng::urcu::read_lock_guard read_lock_guard;
881
882 LTTNG_ASSERT(consumer);
883
884 /* Get consumer socket to use to push the metadata. */
885 socket = consumer_find_socket_by_bitness(consumer_bitness, consumer);
886 if (!socket) {
887 ret = -1;
888 goto end;
889 }
890
891 ret = consumer_close_metadata(socket, metadata_key);
892 if (ret < 0) {
893 goto end;
894 }
895
896 end:
897 return ret;
898 }
899
900 static void delete_ust_app_session_rcu(struct rcu_head *head)
901 {
902 struct ust_app_session *ua_sess =
903 lttng::utils::container_of(head, &ust_app_session::rcu_head);
904
905 lttng_ht_destroy(ua_sess->channels);
906 free(ua_sess);
907 }
908
909 /*
910 * Delete ust app session safely. RCU read lock must be held before calling
911 * this function.
912 *
913 * The session list lock must be held by the caller.
914 */
915 static void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, struct ust_app *app)
916 {
917 int ret;
918 struct lttng_ht_iter iter;
919 struct ust_app_channel *ua_chan;
920
921 LTTNG_ASSERT(ua_sess);
922 ASSERT_RCU_READ_LOCKED();
923
924 pthread_mutex_lock(&ua_sess->lock);
925
926 LTTNG_ASSERT(!ua_sess->deleted);
927 ua_sess->deleted = true;
928
929 auto locked_registry = get_locked_session_registry(ua_sess);
930 /* Registry can be null on error path during initialization. */
931 if (locked_registry) {
932 /* Push metadata for application before freeing the application. */
933 (void) push_metadata(locked_registry, ua_sess->consumer);
934 }
935
936 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
937 ret = lttng_ht_del(ua_sess->channels, &iter);
938 LTTNG_ASSERT(!ret);
939 delete_ust_app_channel(sock, ua_chan, app, locked_registry);
940 }
941
942 if (locked_registry) {
943 /*
944 * Don't ask to close metadata for global per UID buffers. Close
945 * metadata only on destroy trace session in this case. Also, the
946 * previous push metadata could have flag the metadata registry to
947 * close so don't send a close command if closed.
948 */
949 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
950 const auto metadata_key = locked_registry->_metadata_key;
951 const auto consumer_bitness = locked_registry->abi.bits_per_long;
952
953 if (!locked_registry->_metadata_closed && metadata_key != 0) {
954 locked_registry->_metadata_closed = true;
955 }
956
957 /* Release lock before communication, see comments in close_metadata(). */
958 locked_registry.reset();
959 (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer);
960 }
961 }
962
963 /* In case of per PID, the registry is kept in the session. */
964 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
965 struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id);
966 if (reg_pid) {
967 /*
968 * Registry can be null on error path during
969 * initialization.
970 */
971 buffer_reg_pid_remove(reg_pid);
972 buffer_reg_pid_destroy(reg_pid);
973 }
974 }
975
976 if (ua_sess->handle != -1) {
977 pthread_mutex_lock(&app->sock_lock);
978 ret = lttng_ust_ctl_release_handle(sock, ua_sess->handle);
979 pthread_mutex_unlock(&app->sock_lock);
980 if (ret < 0) {
981 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
982 DBG3("UST app release session handle failed. Application is dead: pid = %d, sock = %d",
983 app->pid,
984 app->sock);
985 } else if (ret == -EAGAIN) {
986 WARN("UST app release session handle failed. Communication time out: pid = %d, sock = %d",
987 app->pid,
988 app->sock);
989 } else {
990 ERR("UST app release session handle failed with ret %d: pid = %d, sock = %d",
991 ret,
992 app->pid,
993 app->sock);
994 }
995 }
996
997 /* Remove session from application UST object descriptor. */
998 iter.iter.node = &ua_sess->ust_objd_node.node;
999 ret = lttng_ht_del(app->ust_sessions_objd, &iter);
1000 LTTNG_ASSERT(!ret);
1001 }
1002
1003 pthread_mutex_unlock(&ua_sess->lock);
1004
1005 consumer_output_put(ua_sess->consumer);
1006
1007 call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu);
1008 }
1009
1010 /*
1011 * Delete a traceable application structure from the global list. Never call
1012 * this function outside of a call_rcu call.
1013 */
1014 static void delete_ust_app(struct ust_app *app)
1015 {
1016 int ret, sock;
1017 struct ust_app_session *ua_sess, *tmp_ua_sess;
1018 struct lttng_ht_iter iter;
1019 struct ust_app_event_notifier_rule *event_notifier_rule;
1020 bool event_notifier_write_fd_is_open;
1021
1022 /*
1023 * The session list lock must be held during this function to guarantee
1024 * the existence of ua_sess.
1025 */
1026 session_lock_list();
1027 /* Delete ust app sessions info */
1028 sock = app->sock;
1029 app->sock = -1;
1030
1031 /* Wipe sessions */
1032 cds_list_for_each_entry_safe (ua_sess, tmp_ua_sess, &app->teardown_head, teardown_node) {
1033 /* Free every object in the session and the session. */
1034 lttng::urcu::read_lock_guard read_lock;
1035 delete_ust_app_session(sock, ua_sess, app);
1036 }
1037
1038 /* Remove the event notifier rules associated with this app. */
1039 {
1040 lttng::urcu::read_lock_guard read_lock;
1041
1042 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
1043 &iter.iter,
1044 event_notifier_rule,
1045 node.node) {
1046 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &iter);
1047 LTTNG_ASSERT(!ret);
1048
1049 delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app);
1050 }
1051 }
1052
1053 lttng_ht_destroy(app->sessions);
1054 lttng_ht_destroy(app->ust_sessions_objd);
1055 lttng_ht_destroy(app->ust_objd);
1056 lttng_ht_destroy(app->token_to_event_notifier_rule_ht);
1057
1058 /*
1059 * This could be NULL if the event notifier setup failed (e.g the app
1060 * was killed or the tracer does not support this feature).
1061 */
1062 if (app->event_notifier_group.object) {
1063 enum lttng_error_code ret_code;
1064 enum event_notifier_error_accounting_status status;
1065
1066 const int event_notifier_read_fd =
1067 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe);
1068
1069 ret_code = notification_thread_command_remove_tracer_event_source(
1070 the_notification_thread_handle, event_notifier_read_fd);
1071 if (ret_code != LTTNG_OK) {
1072 ERR("Failed to remove application tracer event source from notification thread");
1073 }
1074
1075 status = event_notifier_error_accounting_unregister_app(app);
1076 if (status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
1077 ERR("Error unregistering app from event notifier error accounting");
1078 }
1079
1080 lttng_ust_ctl_release_object(sock, app->event_notifier_group.object);
1081 free(app->event_notifier_group.object);
1082 }
1083
1084 event_notifier_write_fd_is_open =
1085 lttng_pipe_is_write_open(app->event_notifier_group.event_pipe);
1086 lttng_pipe_destroy(app->event_notifier_group.event_pipe);
1087 /*
1088 * Release the file descriptors reserved for the event notifier pipe.
1089 * The app could be destroyed before the write end of the pipe could be
1090 * passed to the application (and closed). In that case, both file
1091 * descriptors must be released.
1092 */
1093 lttng_fd_put(LTTNG_FD_APPS, event_notifier_write_fd_is_open ? 2 : 1);
1094
1095 /*
1096 * Wait until we have deleted the application from the sock hash table
1097 * before closing this socket, otherwise an application could re-use the
1098 * socket ID and race with the teardown, using the same hash table entry.
1099 *
1100 * It's OK to leave the close in call_rcu. We want it to stay unique for
1101 * all RCU readers that could run concurrently with unregister app,
1102 * therefore we _need_ to only close that socket after a grace period. So
1103 * it should stay in this RCU callback.
1104 *
1105 * This close() is a very important step of the synchronization model so
1106 * every modification to this function must be carefully reviewed.
1107 */
1108 ret = close(sock);
1109 if (ret) {
1110 PERROR("close");
1111 }
1112 lttng_fd_put(LTTNG_FD_APPS, 1);
1113
1114 DBG2("UST app pid %d deleted", app->pid);
1115 free(app);
1116 session_unlock_list();
1117 }
1118
1119 /*
1120 * URCU intermediate call to delete an UST app.
1121 */
1122 static void delete_ust_app_rcu(struct rcu_head *head)
1123 {
1124 struct lttng_ht_node_ulong *node =
1125 lttng::utils::container_of(head, &lttng_ht_node_ulong::head);
1126 struct ust_app *app = lttng::utils::container_of(node, &ust_app::pid_n);
1127
1128 DBG3("Call RCU deleting app PID %d", app->pid);
1129 delete_ust_app(app);
1130 }
1131
1132 /*
1133 * Delete the session from the application ht and delete the data structure by
1134 * freeing every object inside and releasing them.
1135 *
1136 * The session list lock must be held by the caller.
1137 */
1138 static void destroy_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
1139 {
1140 int ret;
1141 struct lttng_ht_iter iter;
1142
1143 LTTNG_ASSERT(app);
1144 LTTNG_ASSERT(ua_sess);
1145
1146 iter.iter.node = &ua_sess->node.node;
1147 ret = lttng_ht_del(app->sessions, &iter);
1148 if (ret) {
1149 /* Already scheduled for teardown. */
1150 goto end;
1151 }
1152
1153 /* Once deleted, free the data structure. */
1154 delete_ust_app_session(app->sock, ua_sess, app);
1155
1156 end:
1157 return;
1158 }
1159
1160 /*
1161 * Alloc new UST app session.
1162 */
1163 static struct ust_app_session *alloc_ust_app_session()
1164 {
1165 struct ust_app_session *ua_sess;
1166
1167 /* Init most of the default value by allocating and zeroing */
1168 ua_sess = zmalloc<ust_app_session>();
1169 if (ua_sess == nullptr) {
1170 PERROR("malloc");
1171 goto error_free;
1172 }
1173
1174 ua_sess->handle = -1;
1175 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
1176 ua_sess->metadata_attr.type = LTTNG_UST_ABI_CHAN_METADATA;
1177 pthread_mutex_init(&ua_sess->lock, nullptr);
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_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_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_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_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_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_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_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 struct ltt_session *session = nullptr;
3447 enum lttng_error_code notification_ret;
3448
3449 LTTNG_ASSERT(app);
3450 LTTNG_ASSERT(usess);
3451 LTTNG_ASSERT(ua_sess);
3452 LTTNG_ASSERT(ua_chan);
3453 ASSERT_RCU_READ_LOCKED();
3454
3455 DBG("UST app creating channel %s with per UID buffers", ua_chan->name);
3456
3457 reg_uid = buffer_reg_uid_find(usess->id, app->abi.bits_per_long, app->uid);
3458 /*
3459 * The session creation handles the creation of this global registry
3460 * object. If none can be find, there is a code flow problem or a
3461 * teardown race.
3462 */
3463 LTTNG_ASSERT(reg_uid);
3464
3465 buf_reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, reg_uid);
3466 if (buf_reg_chan) {
3467 goto send_channel;
3468 }
3469
3470 /* Create the buffer registry channel object. */
3471 ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, &buf_reg_chan);
3472 if (ret < 0) {
3473 ERR("Error creating the UST channel \"%s\" registry instance", ua_chan->name);
3474 goto error;
3475 }
3476
3477 session = session_find_by_id(ua_sess->tracing_id);
3478 LTTNG_ASSERT(session);
3479 ASSERT_LOCKED(session->lock);
3480 ASSERT_SESSION_LIST_LOCKED();
3481
3482 /*
3483 * Create the buffers on the consumer side. This call populates the
3484 * ust app channel object with all streams and data object.
3485 */
3486 ret = do_consumer_create_channel(
3487 usess, ua_sess, ua_chan, app->abi.bits_per_long, reg_uid->registry->reg.ust);
3488 if (ret < 0) {
3489 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name);
3490
3491 /*
3492 * Let's remove the previously created buffer registry channel so
3493 * it's not visible anymore in the session registry.
3494 */
3495 auto locked_registry = reg_uid->registry->reg.ust->lock();
3496 try {
3497 locked_registry->remove_channel(ua_chan->tracing_channel_id, false);
3498 } catch (const std::exception& ex) {
3499 DBG("Could not find channel for removal: %s", ex.what());
3500 }
3501 buffer_reg_channel_remove(reg_uid->registry, buf_reg_chan);
3502 buffer_reg_channel_destroy(buf_reg_chan, LTTNG_DOMAIN_UST);
3503 goto error;
3504 }
3505
3506 /*
3507 * Setup the streams and add it to the session registry.
3508 */
3509 ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, buf_reg_chan, app);
3510 if (ret < 0) {
3511 ERR("Error setting up UST channel \"%s\"", ua_chan->name);
3512 goto error;
3513 }
3514
3515 {
3516 auto locked_registry = reg_uid->registry->reg.ust->lock();
3517 auto& ust_reg_chan = locked_registry->channel(ua_chan->tracing_channel_id);
3518
3519 ust_reg_chan._consumer_key = ua_chan->key;
3520 }
3521
3522 /* Notify the notification subsystem of the channel's creation. */
3523 notification_ret = notification_thread_command_add_channel(
3524 the_notification_thread_handle,
3525 session->id,
3526 ua_chan->name,
3527 ua_chan->key,
3528 LTTNG_DOMAIN_UST,
3529 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3530 if (notification_ret != LTTNG_OK) {
3531 ret = -(int) notification_ret;
3532 ERR("Failed to add channel to notification thread");
3533 goto error;
3534 }
3535
3536 send_channel:
3537 /* Send buffers to the application. */
3538 ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan);
3539 if (ret < 0) {
3540 if (ret != -ENOTCONN) {
3541 ERR("Error sending channel to application");
3542 }
3543 goto error;
3544 }
3545
3546 error:
3547 if (session) {
3548 session_put(session);
3549 }
3550 return ret;
3551 }
3552
3553 /*
3554 * Create and send to the application the created buffers with per PID buffers.
3555 *
3556 * Called with UST app session lock held.
3557 * The session list lock and the session's lock must be acquired.
3558 *
3559 * Return 0 on success else a negative value.
3560 */
3561 static int create_channel_per_pid(struct ust_app *app,
3562 struct ltt_ust_session *usess,
3563 struct ust_app_session *ua_sess,
3564 struct ust_app_channel *ua_chan)
3565 {
3566 int ret;
3567 lsu::registry_session *registry;
3568 enum lttng_error_code cmd_ret;
3569 struct ltt_session *session = nullptr;
3570 uint64_t chan_reg_key;
3571
3572 LTTNG_ASSERT(app);
3573 LTTNG_ASSERT(usess);
3574 LTTNG_ASSERT(ua_sess);
3575 LTTNG_ASSERT(ua_chan);
3576
3577 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3578
3579 lttng::urcu::read_lock_guard read_lock;
3580
3581 registry = get_session_registry(ua_sess);
3582 /* The UST app session lock is held, registry shall not be null. */
3583 LTTNG_ASSERT(registry);
3584
3585 /* Create and add a new channel registry to session. */
3586 try {
3587 registry->add_channel(ua_chan->key);
3588 } catch (const std::exception& ex) {
3589 ERR("Error creating the UST channel \"%s\" registry instance: %s",
3590 ua_chan->name,
3591 ex.what());
3592 ret = -1;
3593 goto error;
3594 }
3595
3596 session = session_find_by_id(ua_sess->tracing_id);
3597 LTTNG_ASSERT(session);
3598 ASSERT_LOCKED(session->lock);
3599 ASSERT_SESSION_LIST_LOCKED();
3600
3601 /* Create and get channel on the consumer side. */
3602 ret = do_consumer_create_channel(usess, ua_sess, ua_chan, app->abi.bits_per_long, registry);
3603 if (ret < 0) {
3604 ERR("Error creating UST channel \"%s\" on the consumer daemon", ua_chan->name);
3605 goto error_remove_from_registry;
3606 }
3607
3608 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3609 if (ret < 0) {
3610 if (ret != -ENOTCONN) {
3611 ERR("Error sending channel to application");
3612 }
3613 goto error_remove_from_registry;
3614 }
3615
3616 chan_reg_key = ua_chan->key;
3617 {
3618 auto locked_registry = registry->lock();
3619
3620 auto& ust_reg_chan = locked_registry->channel(chan_reg_key);
3621 ust_reg_chan._consumer_key = ua_chan->key;
3622 }
3623
3624 cmd_ret = notification_thread_command_add_channel(the_notification_thread_handle,
3625 session->id,
3626 ua_chan->name,
3627 ua_chan->key,
3628 LTTNG_DOMAIN_UST,
3629 ua_chan->attr.subbuf_size *
3630 ua_chan->attr.num_subbuf);
3631 if (cmd_ret != LTTNG_OK) {
3632 ret = -(int) cmd_ret;
3633 ERR("Failed to add channel to notification thread");
3634 goto error_remove_from_registry;
3635 }
3636
3637 error_remove_from_registry:
3638 if (ret) {
3639 try {
3640 auto locked_registry = registry->lock();
3641 locked_registry->remove_channel(ua_chan->key, false);
3642 } catch (const std::exception& ex) {
3643 DBG("Could not find channel for removal: %s", ex.what());
3644 }
3645 }
3646 error:
3647 if (session) {
3648 session_put(session);
3649 }
3650 return ret;
3651 }
3652
3653 /*
3654 * From an already allocated ust app channel, create the channel buffers if
3655 * needed and send them to the application. This MUST be called with a RCU read
3656 * side lock acquired.
3657 *
3658 * Called with UST app session lock held.
3659 *
3660 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3661 * the application exited concurrently.
3662 */
3663 static int ust_app_channel_send(struct ust_app *app,
3664 struct ltt_ust_session *usess,
3665 struct ust_app_session *ua_sess,
3666 struct ust_app_channel *ua_chan)
3667 {
3668 int ret;
3669
3670 LTTNG_ASSERT(app);
3671 LTTNG_ASSERT(usess);
3672 LTTNG_ASSERT(usess->active);
3673 LTTNG_ASSERT(ua_sess);
3674 LTTNG_ASSERT(ua_chan);
3675 ASSERT_RCU_READ_LOCKED();
3676
3677 /* Handle buffer type before sending the channel to the application. */
3678 switch (usess->buffer_type) {
3679 case LTTNG_BUFFER_PER_UID:
3680 {
3681 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3682 if (ret < 0) {
3683 goto error;
3684 }
3685 break;
3686 }
3687 case LTTNG_BUFFER_PER_PID:
3688 {
3689 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3690 if (ret < 0) {
3691 goto error;
3692 }
3693 break;
3694 }
3695 default:
3696 abort();
3697 ret = -EINVAL;
3698 goto error;
3699 }
3700
3701 /* Initialize ust objd object using the received handle and add it. */
3702 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3703 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
3704
3705 /* If channel is not enabled, disable it on the tracer */
3706 if (!ua_chan->enabled) {
3707 ret = disable_ust_channel(app, ua_sess, ua_chan);
3708 if (ret < 0) {
3709 goto error;
3710 }
3711 }
3712
3713 error:
3714 return ret;
3715 }
3716
3717 /*
3718 * Create UST app channel and return it through ua_chanp if not NULL.
3719 *
3720 * Called with UST app session lock and RCU read-side lock held.
3721 *
3722 * Return 0 on success or else a negative value.
3723 */
3724 static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3725 struct ltt_ust_channel *uchan,
3726 enum lttng_ust_abi_chan_type type,
3727 struct ltt_ust_session *usess __attribute__((unused)),
3728 struct ust_app_channel **ua_chanp)
3729 {
3730 int ret = 0;
3731 struct lttng_ht_iter iter;
3732 struct lttng_ht_node_str *ua_chan_node;
3733 struct ust_app_channel *ua_chan;
3734
3735 ASSERT_RCU_READ_LOCKED();
3736
3737 /* Lookup channel in the ust app session */
3738 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
3739 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3740 if (ua_chan_node != nullptr) {
3741 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
3742 goto end;
3743 }
3744
3745 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
3746 if (ua_chan == nullptr) {
3747 /* Only malloc can fail here */
3748 ret = -ENOMEM;
3749 goto error;
3750 }
3751 shadow_copy_channel(ua_chan, uchan);
3752
3753 /* Set channel type. */
3754 ua_chan->attr.type = type;
3755
3756 /* Only add the channel if successful on the tracer side. */
3757 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3758 end:
3759 if (ua_chanp) {
3760 *ua_chanp = ua_chan;
3761 }
3762
3763 /* Everything went well. */
3764 return 0;
3765
3766 error:
3767 return ret;
3768 }
3769
3770 /*
3771 * Create UST app event and create it on the tracer side.
3772 *
3773 * Must be called with the RCU read side lock held.
3774 * Called with ust app session mutex held.
3775 */
3776 static int create_ust_app_event(struct ust_app_channel *ua_chan,
3777 struct ltt_ust_event *uevent,
3778 struct ust_app *app)
3779 {
3780 int ret = 0;
3781 struct ust_app_event *ua_event;
3782
3783 ASSERT_RCU_READ_LOCKED();
3784
3785 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3786 if (ua_event == nullptr) {
3787 /* Only failure mode of alloc_ust_app_event(). */
3788 ret = -ENOMEM;
3789 goto end;
3790 }
3791 shadow_copy_event(ua_event, uevent);
3792
3793 /* Create it on the tracer side */
3794 ret = create_ust_event(app, ua_chan, ua_event);
3795 if (ret < 0) {
3796 /*
3797 * Not found previously means that it does not exist on the
3798 * tracer. If the application reports that the event existed,
3799 * it means there is a bug in the sessiond or lttng-ust
3800 * (or corruption, etc.)
3801 */
3802 if (ret == -LTTNG_UST_ERR_EXIST) {
3803 ERR("Tracer for application reported that an event being created already existed: "
3804 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3805 uevent->attr.name,
3806 app->pid,
3807 app->ppid,
3808 app->uid,
3809 app->gid);
3810 }
3811 goto error;
3812 }
3813
3814 add_unique_ust_app_event(ua_chan, ua_event);
3815
3816 DBG2("UST app create event completed: app = '%s' pid = %d", app->name, app->pid);
3817
3818 end:
3819 return ret;
3820
3821 error:
3822 /* Valid. Calling here is already in a read side lock */
3823 delete_ust_app_event(-1, ua_event, app);
3824 return ret;
3825 }
3826
3827 /*
3828 * Create UST app event notifier rule and create it on the tracer side.
3829 *
3830 * Must be called with the RCU read side lock held.
3831 * Called with ust app session mutex held.
3832 */
3833 static int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger, struct ust_app *app)
3834 {
3835 int ret = 0;
3836 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
3837
3838 ASSERT_RCU_READ_LOCKED();
3839
3840 ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger);
3841 if (ua_event_notifier_rule == nullptr) {
3842 ret = -ENOMEM;
3843 goto end;
3844 }
3845
3846 /* Create it on the tracer side. */
3847 ret = create_ust_event_notifier(app, ua_event_notifier_rule);
3848 if (ret < 0) {
3849 /*
3850 * Not found previously means that it does not exist on the
3851 * tracer. If the application reports that the event existed,
3852 * it means there is a bug in the sessiond or lttng-ust
3853 * (or corruption, etc.)
3854 */
3855 if (ret == -LTTNG_UST_ERR_EXIST) {
3856 ERR("Tracer for application reported that an event notifier being created already exists: "
3857 "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d",
3858 lttng_trigger_get_tracer_token(trigger),
3859 app->pid,
3860 app->ppid,
3861 app->uid,
3862 app->gid);
3863 }
3864 goto error;
3865 }
3866
3867 lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht,
3868 &ua_event_notifier_rule->node);
3869
3870 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64,
3871 app->name,
3872 app->pid,
3873 lttng_trigger_get_tracer_token(trigger));
3874
3875 goto end;
3876
3877 error:
3878 /* The RCU read side lock is already being held by the caller. */
3879 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app);
3880 end:
3881 return ret;
3882 }
3883
3884 /*
3885 * Create UST metadata and open it on the tracer side.
3886 *
3887 * Called with UST app session lock held and RCU read side lock.
3888 */
3889 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
3890 struct ust_app *app,
3891 struct consumer_output *consumer)
3892 {
3893 int ret = 0;
3894 struct ust_app_channel *metadata;
3895 struct consumer_socket *socket;
3896 struct ltt_session *session = nullptr;
3897
3898 LTTNG_ASSERT(ua_sess);
3899 LTTNG_ASSERT(app);
3900 LTTNG_ASSERT(consumer);
3901 ASSERT_RCU_READ_LOCKED();
3902
3903 auto locked_registry = get_locked_session_registry(ua_sess);
3904 /* The UST app session is held registry shall not be null. */
3905 LTTNG_ASSERT(locked_registry);
3906
3907 /* Metadata already exists for this registry or it was closed previously */
3908 if (locked_registry->_metadata_key || locked_registry->_metadata_closed) {
3909 ret = 0;
3910 goto error;
3911 }
3912
3913 /* Allocate UST metadata */
3914 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, nullptr);
3915 if (!metadata) {
3916 /* malloc() failed */
3917 ret = -ENOMEM;
3918 goto error;
3919 }
3920
3921 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3922
3923 /* Need one fd for the channel. */
3924 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3925 if (ret < 0) {
3926 ERR("Exhausted number of available FD upon create metadata");
3927 goto error;
3928 }
3929
3930 /* Get the right consumer socket for the application. */
3931 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, consumer);
3932 if (!socket) {
3933 ret = -EINVAL;
3934 goto error_consumer;
3935 }
3936
3937 /*
3938 * Keep metadata key so we can identify it on the consumer side. Assign it
3939 * to the registry *before* we ask the consumer so we avoid the race of the
3940 * consumer requesting the metadata and the ask_channel call on our side
3941 * did not returned yet.
3942 */
3943 locked_registry->_metadata_key = metadata->key;
3944
3945 session = session_find_by_id(ua_sess->tracing_id);
3946 LTTNG_ASSERT(session);
3947 ASSERT_LOCKED(session->lock);
3948 ASSERT_SESSION_LIST_LOCKED();
3949
3950 /*
3951 * Ask the metadata channel creation to the consumer. The metadata object
3952 * will be created by the consumer and kept their. However, the stream is
3953 * never added or monitored until we do a first push metadata to the
3954 * consumer.
3955 */
3956 ret = ust_consumer_ask_channel(ua_sess,
3957 metadata,
3958 consumer,
3959 socket,
3960 locked_registry.get(),
3961 session->current_trace_chunk);
3962 if (ret < 0) {
3963 /* Nullify the metadata key so we don't try to close it later on. */
3964 locked_registry->_metadata_key = 0;
3965 goto error_consumer;
3966 }
3967
3968 /*
3969 * The setup command will make the metadata stream be sent to the relayd,
3970 * if applicable, and the thread managing the metadatas. This is important
3971 * because after this point, if an error occurs, the only way the stream
3972 * can be deleted is to be monitored in the consumer.
3973 */
3974 ret = consumer_setup_metadata(socket, metadata->key);
3975 if (ret < 0) {
3976 /* Nullify the metadata key so we don't try to close it later on. */
3977 locked_registry->_metadata_key = 0;
3978 goto error_consumer;
3979 }
3980
3981 DBG2("UST metadata with key %" PRIu64 " created for app pid %d", metadata->key, app->pid);
3982
3983 error_consumer:
3984 lttng_fd_put(LTTNG_FD_APPS, 1);
3985 delete_ust_app_channel(-1, metadata, app, locked_registry);
3986 error:
3987 if (session) {
3988 session_put(session);
3989 }
3990 return ret;
3991 }
3992
3993 /*
3994 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3995 * acquired before calling this function.
3996 */
3997 struct ust_app *ust_app_find_by_pid(pid_t pid)
3998 {
3999 struct ust_app *app = nullptr;
4000 struct lttng_ht_node_ulong *node;
4001 struct lttng_ht_iter iter;
4002
4003 lttng_ht_lookup(ust_app_ht, (void *) ((unsigned long) pid), &iter);
4004 node = lttng_ht_iter_get_node_ulong(&iter);
4005 if (node == nullptr) {
4006 DBG2("UST app no found with pid %d", pid);
4007 goto error;
4008 }
4009
4010 DBG2("Found UST app by pid %d", pid);
4011
4012 app = lttng::utils::container_of(node, &ust_app::pid_n);
4013
4014 error:
4015 return app;
4016 }
4017
4018 /*
4019 * Allocate and init an UST app object using the registration information and
4020 * the command socket. This is called when the command socket connects to the
4021 * session daemon.
4022 *
4023 * The object is returned on success or else NULL.
4024 */
4025 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
4026 {
4027 int ret;
4028 struct ust_app *lta = nullptr;
4029 struct lttng_pipe *event_notifier_event_source_pipe = nullptr;
4030
4031 LTTNG_ASSERT(msg);
4032 LTTNG_ASSERT(sock >= 0);
4033
4034 DBG3("UST app creating application for socket %d", sock);
4035
4036 if ((msg->bits_per_long == 64 && (uatomic_read(&the_ust_consumerd64_fd) == -EINVAL)) ||
4037 (msg->bits_per_long == 32 && (uatomic_read(&the_ust_consumerd32_fd) == -EINVAL))) {
4038 ERR("Registration failed: application \"%s\" (pid: %d) has "
4039 "%d-bit long, but no consumerd for this size is available.\n",
4040 msg->name,
4041 msg->pid,
4042 msg->bits_per_long);
4043 goto error;
4044 }
4045
4046 /*
4047 * Reserve the two file descriptors of the event source pipe. The write
4048 * end will be closed once it is passed to the application, at which
4049 * point a single 'put' will be performed.
4050 */
4051 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
4052 if (ret) {
4053 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
4054 msg->name,
4055 (int) msg->pid);
4056 goto error;
4057 }
4058
4059 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
4060 if (!event_notifier_event_source_pipe) {
4061 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
4062 msg->name,
4063 msg->pid);
4064 goto error;
4065 }
4066
4067 lta = zmalloc<ust_app>();
4068 if (lta == nullptr) {
4069 PERROR("malloc");
4070 goto error_free_pipe;
4071 }
4072
4073 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
4074
4075 lta->ppid = msg->ppid;
4076 lta->uid = msg->uid;
4077 lta->gid = msg->gid;
4078
4079 lta->abi = {
4080 .bits_per_long = msg->bits_per_long,
4081 .long_alignment = msg->long_alignment,
4082 .uint8_t_alignment = msg->uint8_t_alignment,
4083 .uint16_t_alignment = msg->uint16_t_alignment,
4084 .uint32_t_alignment = msg->uint32_t_alignment,
4085 .uint64_t_alignment = msg->uint64_t_alignment,
4086 .byte_order = msg->byte_order == LITTLE_ENDIAN ?
4087 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_ :
4088 lttng::sessiond::trace::byte_order::BIG_ENDIAN_,
4089 };
4090
4091 lta->v_major = msg->major;
4092 lta->v_minor = msg->minor;
4093 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4094 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4095 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4096 lta->notify_sock = -1;
4097 lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4098
4099 /* Copy name and make sure it's NULL terminated. */
4100 strncpy(lta->name, msg->name, sizeof(lta->name));
4101 lta->name[UST_APP_PROCNAME_LEN] = '\0';
4102
4103 /*
4104 * Before this can be called, when receiving the registration information,
4105 * the application compatibility is checked. So, at this point, the
4106 * application can work with this session daemon.
4107 */
4108 lta->compatible = 1;
4109
4110 lta->pid = msg->pid;
4111 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
4112 lta->sock = sock;
4113 pthread_mutex_init(&lta->sock_lock, nullptr);
4114 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
4115
4116 CDS_INIT_LIST_HEAD(&lta->teardown_head);
4117 return lta;
4118
4119 error_free_pipe:
4120 lttng_pipe_destroy(event_notifier_event_source_pipe);
4121 lttng_fd_put(LTTNG_FD_APPS, 2);
4122 error:
4123 return nullptr;
4124 }
4125
4126 /*
4127 * For a given application object, add it to every hash table.
4128 */
4129 void ust_app_add(struct ust_app *app)
4130 {
4131 LTTNG_ASSERT(app);
4132 LTTNG_ASSERT(app->notify_sock >= 0);
4133
4134 app->registration_time = time(nullptr);
4135
4136 lttng::urcu::read_lock_guard read_lock;
4137
4138 /*
4139 * On a re-registration, we want to kick out the previous registration of
4140 * that pid
4141 */
4142 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
4143
4144 /*
4145 * The socket _should_ be unique until _we_ call close. So, a add_unique
4146 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4147 * already in the table.
4148 */
4149 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
4150
4151 /* Add application to the notify socket hash table. */
4152 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
4153 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
4154
4155 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4156 "notify_sock =%d (version %d.%d)",
4157 app->pid,
4158 app->ppid,
4159 app->uid,
4160 app->gid,
4161 app->sock,
4162 app->name,
4163 app->notify_sock,
4164 app->v_major,
4165 app->v_minor);
4166 }
4167
4168 /*
4169 * Set the application version into the object.
4170 *
4171 * Return 0 on success else a negative value either an errno code or a
4172 * LTTng-UST error code.
4173 */
4174 int ust_app_version(struct ust_app *app)
4175 {
4176 int ret;
4177
4178 LTTNG_ASSERT(app);
4179
4180 pthread_mutex_lock(&app->sock_lock);
4181 ret = lttng_ust_ctl_tracer_version(app->sock, &app->version);
4182 pthread_mutex_unlock(&app->sock_lock);
4183 if (ret < 0) {
4184 if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) {
4185 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4186 app->pid,
4187 app->sock);
4188 } else if (ret == -EAGAIN) {
4189 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4190 app->pid,
4191 app->sock);
4192 } else {
4193 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4194 ret,
4195 app->pid,
4196 app->sock);
4197 }
4198 }
4199
4200 return ret;
4201 }
4202
4203 bool ust_app_supports_notifiers(const struct ust_app *app)
4204 {
4205 return app->v_major >= 9;
4206 }
4207
4208 bool ust_app_supports_counters(const struct ust_app *app)
4209 {
4210 return app->v_major >= 9;
4211 }
4212
4213 /*
4214 * Setup the base event notifier group.
4215 *
4216 * Return 0 on success else a negative value either an errno code or a
4217 * LTTng-UST error code.
4218 */
4219 int ust_app_setup_event_notifier_group(struct ust_app *app)
4220 {
4221 int ret;
4222 int event_pipe_write_fd;
4223 struct lttng_ust_abi_object_data *event_notifier_group = nullptr;
4224 enum lttng_error_code lttng_ret;
4225 enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
4226
4227 LTTNG_ASSERT(app);
4228
4229 if (!ust_app_supports_notifiers(app)) {
4230 ret = -ENOSYS;
4231 goto error;
4232 }
4233
4234 /* Get the write side of the pipe. */
4235 event_pipe_write_fd = lttng_pipe_get_writefd(app->event_notifier_group.event_pipe);
4236
4237 pthread_mutex_lock(&app->sock_lock);
4238 ret = lttng_ust_ctl_create_event_notifier_group(
4239 app->sock, event_pipe_write_fd, &event_notifier_group);
4240 pthread_mutex_unlock(&app->sock_lock);
4241 if (ret < 0) {
4242 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4243 ret = 0;
4244 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4245 app->pid,
4246 app->sock);
4247 } else if (ret == -EAGAIN) {
4248 ret = 0;
4249 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4250 app->pid,
4251 app->sock);
4252 } else {
4253 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4254 ret,
4255 app->pid,
4256 app->sock,
4257 event_pipe_write_fd);
4258 }
4259 goto error;
4260 }
4261
4262 ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe);
4263 if (ret) {
4264 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4265 app->name,
4266 app->pid);
4267 goto error;
4268 }
4269
4270 /*
4271 * Release the file descriptor that was reserved for the write-end of
4272 * the pipe.
4273 */
4274 lttng_fd_put(LTTNG_FD_APPS, 1);
4275
4276 lttng_ret = notification_thread_command_add_tracer_event_source(
4277 the_notification_thread_handle,
4278 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe),
4279 LTTNG_DOMAIN_UST);
4280 if (lttng_ret != LTTNG_OK) {
4281 ERR("Failed to add tracer event source to notification thread");
4282 ret = -1;
4283 goto error;
4284 }
4285
4286 /* Assign handle only when the complete setup is valid. */
4287 app->event_notifier_group.object = event_notifier_group;
4288
4289 event_notifier_error_accounting_status = event_notifier_error_accounting_register_app(app);
4290 switch (event_notifier_error_accounting_status) {
4291 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK:
4292 break;
4293 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED:
4294 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",
4295 app->sock,
4296 app->name,
4297 (int) app->pid);
4298 ret = 0;
4299 goto error_accounting;
4300 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD:
4301 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4302 app->sock,
4303 app->name,
4304 (int) app->pid);
4305 ret = 0;
4306 goto error_accounting;
4307 default:
4308 ERR("Failed to setup event notifier error accounting for app");
4309 ret = -1;
4310 goto error_accounting;
4311 }
4312
4313 return ret;
4314
4315 error_accounting:
4316 lttng_ret = notification_thread_command_remove_tracer_event_source(
4317 the_notification_thread_handle,
4318 lttng_pipe_get_readfd(app->event_notifier_group.event_pipe));
4319 if (lttng_ret != LTTNG_OK) {
4320 ERR("Failed to remove application tracer event source from notification thread");
4321 }
4322
4323 error:
4324 lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object);
4325 free(app->event_notifier_group.object);
4326 app->event_notifier_group.object = nullptr;
4327 return ret;
4328 }
4329
4330 /*
4331 * Unregister app by removing it from the global traceable app list and freeing
4332 * the data struct.
4333 *
4334 * The socket is already closed at this point so no close to sock.
4335 */
4336 void ust_app_unregister(int sock)
4337 {
4338 struct ust_app *lta;
4339 struct lttng_ht_node_ulong *node;
4340 struct lttng_ht_iter ust_app_sock_iter;
4341 struct lttng_ht_iter iter;
4342 struct ust_app_session *ua_sess;
4343 int ret;
4344
4345 lttng::urcu::read_lock_guard read_lock;
4346
4347 /* Get the node reference for a call_rcu */
4348 lttng_ht_lookup(ust_app_ht_by_sock, (void *) ((unsigned long) sock), &ust_app_sock_iter);
4349 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
4350 LTTNG_ASSERT(node);
4351
4352 lta = lttng::utils::container_of(node, &ust_app::sock_n);
4353 DBG("PID %d unregistering with sock %d", lta->pid, sock);
4354
4355 /*
4356 * For per-PID buffers, perform "push metadata" and flush all
4357 * application streams before removing app from hash tables,
4358 * ensuring proper behavior of data_pending check.
4359 * Remove sessions so they are not visible during deletion.
4360 */
4361 cds_lfht_for_each_entry (lta->sessions->ht, &iter.iter, ua_sess, node.node) {
4362 ret = lttng_ht_del(lta->sessions, &iter);
4363 if (ret) {
4364 /* The session was already removed so scheduled for teardown. */
4365 continue;
4366 }
4367
4368 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
4369 (void) ust_app_flush_app_session(lta, ua_sess);
4370 }
4371
4372 /*
4373 * Add session to list for teardown. This is safe since at this point we
4374 * are the only one using this list.
4375 */
4376 pthread_mutex_lock(&ua_sess->lock);
4377
4378 if (ua_sess->deleted) {
4379 pthread_mutex_unlock(&ua_sess->lock);
4380 continue;
4381 }
4382
4383 /*
4384 * Normally, this is done in the delete session process which is
4385 * executed in the call rcu below. However, upon registration we can't
4386 * afford to wait for the grace period before pushing data or else the
4387 * data pending feature can race between the unregistration and stop
4388 * command where the data pending command is sent *before* the grace
4389 * period ended.
4390 *
4391 * The close metadata below nullifies the metadata pointer in the
4392 * session so the delete session will NOT push/close a second time.
4393 */
4394 auto locked_registry = get_locked_session_registry(ua_sess);
4395 if (locked_registry) {
4396 /* Push metadata for application before freeing the application. */
4397 (void) push_metadata(locked_registry, ua_sess->consumer);
4398
4399 /*
4400 * Don't ask to close metadata for global per UID buffers. Close
4401 * metadata only on destroy trace session in this case. Also, the
4402 * previous push metadata could have flag the metadata registry to
4403 * close so don't send a close command if closed.
4404 */
4405 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
4406 const auto metadata_key = locked_registry->_metadata_key;
4407 const auto consumer_bitness = locked_registry->abi.bits_per_long;
4408
4409 if (!locked_registry->_metadata_closed && metadata_key != 0) {
4410 locked_registry->_metadata_closed = true;
4411 }
4412
4413 /* Release lock before communication, see comments in
4414 * close_metadata(). */
4415 locked_registry.reset();
4416 (void) close_metadata(
4417 metadata_key, consumer_bitness, ua_sess->consumer);
4418 } else {
4419 locked_registry.reset();
4420 }
4421 }
4422 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
4423
4424 pthread_mutex_unlock(&ua_sess->lock);
4425 }
4426
4427 /* Remove application from PID hash table */
4428 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
4429 LTTNG_ASSERT(!ret);
4430
4431 /*
4432 * Remove application from notify hash table. The thread handling the
4433 * notify socket could have deleted the node so ignore on error because
4434 * either way it's valid. The close of that socket is handled by the
4435 * apps_notify_thread.
4436 */
4437 iter.iter.node = &lta->notify_sock_n.node;
4438 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
4439
4440 /*
4441 * Ignore return value since the node might have been removed before by an
4442 * add replace during app registration because the PID can be reassigned by
4443 * the OS.
4444 */
4445 iter.iter.node = &lta->pid_n.node;
4446 ret = lttng_ht_del(ust_app_ht, &iter);
4447 if (ret) {
4448 DBG3("Unregister app by PID %d failed. This can happen on pid reuse", lta->pid);
4449 }
4450
4451 /* Free memory */
4452 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
4453
4454 return;
4455 }
4456
4457 /*
4458 * Fill events array with all events name of all registered apps.
4459 */
4460 int ust_app_list_events(struct lttng_event **events)
4461 {
4462 int ret, handle;
4463 size_t nbmem, count = 0;
4464 struct lttng_ht_iter iter;
4465 struct ust_app *app;
4466 struct lttng_event *tmp_event;
4467
4468 nbmem = UST_APP_EVENT_LIST_SIZE;
4469 tmp_event = calloc<lttng_event>(nbmem);
4470 if (tmp_event == nullptr) {
4471 PERROR("zmalloc ust app events");
4472 ret = -ENOMEM;
4473 goto error;
4474 }
4475
4476 {
4477 lttng::urcu::read_lock_guard read_lock;
4478
4479 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4480 struct lttng_ust_abi_tracepoint_iter uiter;
4481
4482 health_code_update();
4483
4484 if (!app->compatible) {
4485 /*
4486 * TODO: In time, we should notice the caller of this error by
4487 * telling him that this is a version error.
4488 */
4489 continue;
4490 }
4491
4492 pthread_mutex_lock(&app->sock_lock);
4493 handle = lttng_ust_ctl_tracepoint_list(app->sock);
4494 if (handle < 0) {
4495 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4496 ERR("UST app list events getting handle failed for app pid %d",
4497 app->pid);
4498 }
4499 pthread_mutex_unlock(&app->sock_lock);
4500 continue;
4501 }
4502
4503 while ((ret = lttng_ust_ctl_tracepoint_list_get(
4504 app->sock, handle, &uiter)) != -LTTNG_UST_ERR_NOENT) {
4505 /* Handle ustctl error. */
4506 if (ret < 0) {
4507 int release_ret;
4508
4509 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4510 ERR("UST app tp list get failed for app %d with ret %d",
4511 app->sock,
4512 ret);
4513 } else {
4514 DBG3("UST app tp list get failed. Application is dead");
4515 break;
4516 }
4517
4518 free(tmp_event);
4519 release_ret =
4520 lttng_ust_ctl_release_handle(app->sock, handle);
4521 if (release_ret < 0 &&
4522 release_ret != -LTTNG_UST_ERR_EXITING &&
4523 release_ret != -EPIPE) {
4524 ERR("Error releasing app handle for app %d with ret %d",
4525 app->sock,
4526 release_ret);
4527 }
4528
4529 pthread_mutex_unlock(&app->sock_lock);
4530 goto rcu_error;
4531 }
4532
4533 health_code_update();
4534 if (count >= nbmem) {
4535 /* In case the realloc fails, we free the memory */
4536 struct lttng_event *new_tmp_event;
4537 size_t new_nbmem;
4538
4539 new_nbmem = nbmem << 1;
4540 DBG2("Reallocating event list from %zu to %zu entries",
4541 nbmem,
4542 new_nbmem);
4543 new_tmp_event = (lttng_event *) realloc(
4544 tmp_event, new_nbmem * sizeof(struct lttng_event));
4545 if (new_tmp_event == nullptr) {
4546 int release_ret;
4547
4548 PERROR("realloc ust app events");
4549 free(tmp_event);
4550 ret = -ENOMEM;
4551 release_ret = lttng_ust_ctl_release_handle(
4552 app->sock, handle);
4553 if (release_ret < 0 &&
4554 release_ret != -LTTNG_UST_ERR_EXITING &&
4555 release_ret != -EPIPE) {
4556 ERR("Error releasing app handle for app %d with ret %d",
4557 app->sock,
4558 release_ret);
4559 }
4560
4561 pthread_mutex_unlock(&app->sock_lock);
4562 goto rcu_error;
4563 }
4564 /* Zero the new memory */
4565 memset(new_tmp_event + nbmem,
4566 0,
4567 (new_nbmem - nbmem) * sizeof(struct lttng_event));
4568 nbmem = new_nbmem;
4569 tmp_event = new_tmp_event;
4570 }
4571
4572 memcpy(tmp_event[count].name,
4573 uiter.name,
4574 LTTNG_UST_ABI_SYM_NAME_LEN);
4575 tmp_event[count].loglevel = uiter.loglevel;
4576 tmp_event[count].type =
4577 (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT;
4578 tmp_event[count].pid = app->pid;
4579 tmp_event[count].enabled = -1;
4580 count++;
4581 }
4582
4583 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4584 pthread_mutex_unlock(&app->sock_lock);
4585 if (ret < 0) {
4586 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4587 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4588 app->pid,
4589 app->sock);
4590 } else if (ret == -EAGAIN) {
4591 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4592 app->pid,
4593 app->sock);
4594 } else {
4595 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4596 ret,
4597 app->pid,
4598 app->sock);
4599 }
4600 }
4601 }
4602 }
4603
4604 ret = count;
4605 *events = tmp_event;
4606
4607 DBG2("UST app list events done (%zu events)", count);
4608
4609 rcu_error:
4610 error:
4611 health_code_update();
4612 return ret;
4613 }
4614
4615 /*
4616 * Fill events array with all events name of all registered apps.
4617 */
4618 int ust_app_list_event_fields(struct lttng_event_field **fields)
4619 {
4620 int ret, handle;
4621 size_t nbmem, count = 0;
4622 struct lttng_ht_iter iter;
4623 struct ust_app *app;
4624 struct lttng_event_field *tmp_event;
4625
4626 nbmem = UST_APP_EVENT_LIST_SIZE;
4627 tmp_event = calloc<lttng_event_field>(nbmem);
4628 if (tmp_event == nullptr) {
4629 PERROR("zmalloc ust app event fields");
4630 ret = -ENOMEM;
4631 goto error;
4632 }
4633
4634 {
4635 lttng::urcu::read_lock_guard read_lock;
4636
4637 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4638 struct lttng_ust_abi_field_iter uiter;
4639
4640 health_code_update();
4641
4642 if (!app->compatible) {
4643 /*
4644 * TODO: In time, we should notice the caller of this error by
4645 * telling him that this is a version error.
4646 */
4647 continue;
4648 }
4649
4650 pthread_mutex_lock(&app->sock_lock);
4651 handle = lttng_ust_ctl_tracepoint_field_list(app->sock);
4652 if (handle < 0) {
4653 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4654 ERR("UST app list field getting handle failed for app pid %d",
4655 app->pid);
4656 }
4657 pthread_mutex_unlock(&app->sock_lock);
4658 continue;
4659 }
4660
4661 while ((ret = lttng_ust_ctl_tracepoint_field_list_get(
4662 app->sock, handle, &uiter)) != -LTTNG_UST_ERR_NOENT) {
4663 /* Handle ustctl error. */
4664 if (ret < 0) {
4665 int release_ret;
4666
4667 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4668 ERR("UST app tp list field failed for app %d with ret %d",
4669 app->sock,
4670 ret);
4671 } else {
4672 DBG3("UST app tp list field failed. Application is dead");
4673 break;
4674 }
4675
4676 free(tmp_event);
4677 release_ret =
4678 lttng_ust_ctl_release_handle(app->sock, handle);
4679 pthread_mutex_unlock(&app->sock_lock);
4680 if (release_ret < 0 &&
4681 release_ret != -LTTNG_UST_ERR_EXITING &&
4682 release_ret != -EPIPE) {
4683 ERR("Error releasing app handle for app %d with ret %d",
4684 app->sock,
4685 release_ret);
4686 }
4687
4688 goto rcu_error;
4689 }
4690
4691 health_code_update();
4692 if (count >= nbmem) {
4693 /* In case the realloc fails, we free the memory */
4694 struct lttng_event_field *new_tmp_event;
4695 size_t new_nbmem;
4696
4697 new_nbmem = nbmem << 1;
4698 DBG2("Reallocating event field list from %zu to %zu entries",
4699 nbmem,
4700 new_nbmem);
4701 new_tmp_event = (lttng_event_field *) realloc(
4702 tmp_event,
4703 new_nbmem * sizeof(struct lttng_event_field));
4704 if (new_tmp_event == nullptr) {
4705 int release_ret;
4706
4707 PERROR("realloc ust app event fields");
4708 free(tmp_event);
4709 ret = -ENOMEM;
4710 release_ret = lttng_ust_ctl_release_handle(
4711 app->sock, handle);
4712 pthread_mutex_unlock(&app->sock_lock);
4713 if (release_ret &&
4714 release_ret != -LTTNG_UST_ERR_EXITING &&
4715 release_ret != -EPIPE) {
4716 ERR("Error releasing app handle for app %d with ret %d",
4717 app->sock,
4718 release_ret);
4719 }
4720
4721 goto rcu_error;
4722 }
4723
4724 /* Zero the new memory */
4725 memset(new_tmp_event + nbmem,
4726 0,
4727 (new_nbmem - nbmem) *
4728 sizeof(struct lttng_event_field));
4729 nbmem = new_nbmem;
4730 tmp_event = new_tmp_event;
4731 }
4732
4733 memcpy(tmp_event[count].field_name,
4734 uiter.field_name,
4735 LTTNG_UST_ABI_SYM_NAME_LEN);
4736 /* Mapping between these enums matches 1 to 1. */
4737 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
4738 tmp_event[count].nowrite = uiter.nowrite;
4739
4740 memcpy(tmp_event[count].event.name,
4741 uiter.event_name,
4742 LTTNG_UST_ABI_SYM_NAME_LEN);
4743 tmp_event[count].event.loglevel = uiter.loglevel;
4744 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
4745 tmp_event[count].event.pid = app->pid;
4746 tmp_event[count].event.enabled = -1;
4747 count++;
4748 }
4749
4750 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4751 pthread_mutex_unlock(&app->sock_lock);
4752 if (ret < 0 && ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4753 ERR("Error releasing app handle for app %d with ret %d",
4754 app->sock,
4755 ret);
4756 }
4757 }
4758 }
4759
4760 ret = count;
4761 *fields = tmp_event;
4762
4763 DBG2("UST app list event fields done (%zu events)", count);
4764
4765 rcu_error:
4766 error:
4767 health_code_update();
4768 return ret;
4769 }
4770
4771 /*
4772 * Free and clean all traceable apps of the global list.
4773 */
4774 void ust_app_clean_list()
4775 {
4776 int ret;
4777 struct ust_app *app;
4778 struct lttng_ht_iter iter;
4779
4780 DBG2("UST app cleaning registered apps hash table");
4781
4782 /* Cleanup notify socket hash table */
4783 if (ust_app_ht_by_notify_sock) {
4784 lttng::urcu::read_lock_guard read_lock;
4785
4786 cds_lfht_for_each_entry (
4787 ust_app_ht_by_notify_sock->ht, &iter.iter, app, notify_sock_n.node) {
4788 /*
4789 * Assert that all notifiers are gone as all triggers
4790 * are unregistered prior to this clean-up.
4791 */
4792 LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0);
4793
4794 ust_app_notify_sock_unregister(app->notify_sock);
4795 }
4796 }
4797
4798 if (ust_app_ht) {
4799 lttng::urcu::read_lock_guard read_lock;
4800
4801 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4802 ret = lttng_ht_del(ust_app_ht, &iter);
4803 LTTNG_ASSERT(!ret);
4804 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
4805 }
4806 }
4807
4808 /* Cleanup socket hash table */
4809 if (ust_app_ht_by_sock) {
4810 lttng::urcu::read_lock_guard read_lock;
4811
4812 cds_lfht_for_each_entry (ust_app_ht_by_sock->ht, &iter.iter, app, sock_n.node) {
4813 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
4814 LTTNG_ASSERT(!ret);
4815 }
4816 }
4817
4818 /* Destroy is done only when the ht is empty */
4819 if (ust_app_ht) {
4820 lttng_ht_destroy(ust_app_ht);
4821 }
4822 if (ust_app_ht_by_sock) {
4823 lttng_ht_destroy(ust_app_ht_by_sock);
4824 }
4825 if (ust_app_ht_by_notify_sock) {
4826 lttng_ht_destroy(ust_app_ht_by_notify_sock);
4827 }
4828 }
4829
4830 /*
4831 * Init UST app hash table.
4832 */
4833 int ust_app_ht_alloc()
4834 {
4835 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4836 if (!ust_app_ht) {
4837 return -1;
4838 }
4839 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4840 if (!ust_app_ht_by_sock) {
4841 return -1;
4842 }
4843 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4844 if (!ust_app_ht_by_notify_sock) {
4845 return -1;
4846 }
4847 return 0;
4848 }
4849
4850 /*
4851 * For a specific UST session, disable the channel for all registered apps.
4852 */
4853 int ust_app_disable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan)
4854 {
4855 int ret = 0;
4856 struct lttng_ht_iter iter;
4857 struct lttng_ht_node_str *ua_chan_node;
4858 struct ust_app *app;
4859 struct ust_app_session *ua_sess;
4860 struct ust_app_channel *ua_chan;
4861
4862 LTTNG_ASSERT(usess->active);
4863 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
4864 uchan->name,
4865 usess->id);
4866
4867 {
4868 lttng::urcu::read_lock_guard read_lock;
4869
4870 /* For every registered applications */
4871 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4872 struct lttng_ht_iter uiter;
4873 if (!app->compatible) {
4874 /*
4875 * TODO: In time, we should notice the caller of this error by
4876 * telling him that this is a version error.
4877 */
4878 continue;
4879 }
4880 ua_sess = lookup_session_by_app(usess, app);
4881 if (ua_sess == nullptr) {
4882 continue;
4883 }
4884
4885 /* Get channel */
4886 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
4887 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4888 /* If the session if found for the app, the channel must be there */
4889 LTTNG_ASSERT(ua_chan_node);
4890
4891 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
4892 /* The channel must not be already disabled */
4893 LTTNG_ASSERT(ua_chan->enabled);
4894
4895 /* Disable channel onto application */
4896 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
4897 if (ret < 0) {
4898 /* XXX: We might want to report this error at some point... */
4899 continue;
4900 }
4901 }
4902 }
4903
4904 return ret;
4905 }
4906
4907 /*
4908 * For a specific UST session, enable the channel for all registered apps.
4909 */
4910 int ust_app_enable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan)
4911 {
4912 int ret = 0;
4913 struct lttng_ht_iter iter;
4914 struct ust_app *app;
4915 struct ust_app_session *ua_sess;
4916
4917 LTTNG_ASSERT(usess->active);
4918 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
4919 uchan->name,
4920 usess->id);
4921
4922 {
4923 lttng::urcu::read_lock_guard read_lock;
4924
4925 /* For every registered applications */
4926 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4927 if (!app->compatible) {
4928 /*
4929 * TODO: In time, we should notice the caller of this error by
4930 * telling him that this is a version error.
4931 */
4932 continue;
4933 }
4934 ua_sess = lookup_session_by_app(usess, app);
4935 if (ua_sess == nullptr) {
4936 continue;
4937 }
4938
4939 /* Enable channel onto application */
4940 ret = enable_ust_app_channel(ua_sess, uchan, app);
4941 if (ret < 0) {
4942 /* XXX: We might want to report this error at some point... */
4943 continue;
4944 }
4945 }
4946 }
4947
4948 return ret;
4949 }
4950
4951 /*
4952 * Disable an event in a channel and for a specific session.
4953 */
4954 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4955 struct ltt_ust_channel *uchan,
4956 struct ltt_ust_event *uevent)
4957 {
4958 int ret = 0;
4959 struct lttng_ht_iter iter, uiter;
4960 struct lttng_ht_node_str *ua_chan_node;
4961 struct ust_app *app;
4962 struct ust_app_session *ua_sess;
4963 struct ust_app_channel *ua_chan;
4964 struct ust_app_event *ua_event;
4965
4966 LTTNG_ASSERT(usess->active);
4967 DBG("UST app disabling event %s for all apps in channel "
4968 "%s for session id %" PRIu64,
4969 uevent->attr.name,
4970 uchan->name,
4971 usess->id);
4972
4973 {
4974 lttng::urcu::read_lock_guard read_lock;
4975
4976 /* For all registered applications */
4977 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4978 if (!app->compatible) {
4979 /*
4980 * TODO: In time, we should notice the caller of this error by
4981 * telling him that this is a version error.
4982 */
4983 continue;
4984 }
4985 ua_sess = lookup_session_by_app(usess, app);
4986 if (ua_sess == nullptr) {
4987 /* Next app */
4988 continue;
4989 }
4990
4991 /* Lookup channel in the ust app session */
4992 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
4993 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4994 if (ua_chan_node == nullptr) {
4995 DBG2("Channel %s not found in session id %" PRIu64
4996 " for app pid %d."
4997 "Skipping",
4998 uchan->name,
4999 usess->id,
5000 app->pid);
5001 continue;
5002 }
5003 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5004
5005 ua_event = find_ust_app_event(
5006 ua_chan->events,
5007 uevent->attr.name,
5008 uevent->filter,
5009 (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type,
5010 uevent->attr.loglevel,
5011 uevent->exclusion);
5012 if (ua_event == nullptr) {
5013 DBG2("Event %s not found in channel %s for app pid %d."
5014 "Skipping",
5015 uevent->attr.name,
5016 uchan->name,
5017 app->pid);
5018 continue;
5019 }
5020
5021 ret = disable_ust_app_event(ua_event, app);
5022 if (ret < 0) {
5023 /* XXX: Report error someday... */
5024 continue;
5025 }
5026 }
5027 }
5028
5029 return ret;
5030 }
5031
5032 /* The ua_sess lock must be held by the caller. */
5033 static int ust_app_channel_create(struct ltt_ust_session *usess,
5034 struct ust_app_session *ua_sess,
5035 struct ltt_ust_channel *uchan,
5036 struct ust_app *app,
5037 struct ust_app_channel **_ua_chan)
5038 {
5039 int ret = 0;
5040 struct ust_app_channel *ua_chan = nullptr;
5041
5042 LTTNG_ASSERT(ua_sess);
5043 ASSERT_LOCKED(ua_sess->lock);
5044
5045 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME, sizeof(uchan->name))) {
5046 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, &uchan->attr);
5047 ret = 0;
5048 } else {
5049 struct ltt_ust_context *uctx = nullptr;
5050
5051 /*
5052 * Create channel onto application and synchronize its
5053 * configuration.
5054 */
5055 ret = ust_app_channel_allocate(
5056 ua_sess, uchan, LTTNG_UST_ABI_CHAN_PER_CPU, usess, &ua_chan);
5057 if (ret < 0) {
5058 goto error;
5059 }
5060
5061 ret = ust_app_channel_send(app, usess, ua_sess, ua_chan);
5062 if (ret) {
5063 goto error;
5064 }
5065
5066 /* Add contexts. */
5067 cds_list_for_each_entry (uctx, &uchan->ctx_list, list) {
5068 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
5069 if (ret) {
5070 goto error;
5071 }
5072 }
5073 }
5074
5075 error:
5076 if (ret < 0) {
5077 switch (ret) {
5078 case -ENOTCONN:
5079 /*
5080 * The application's socket is not valid. Either a bad socket
5081 * or a timeout on it. We can't inform the caller that for a
5082 * specific app, the session failed so lets continue here.
5083 */
5084 ret = 0; /* Not an error. */
5085 break;
5086 case -ENOMEM:
5087 default:
5088 break;
5089 }
5090 }
5091
5092 if (ret == 0 && _ua_chan) {
5093 /*
5094 * Only return the application's channel on success. Note
5095 * that the channel can still be part of the application's
5096 * channel hashtable on error.
5097 */
5098 *_ua_chan = ua_chan;
5099 }
5100 return ret;
5101 }
5102
5103 /*
5104 * Enable event for a specific session and channel on the tracer.
5105 */
5106 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
5107 struct ltt_ust_channel *uchan,
5108 struct ltt_ust_event *uevent)
5109 {
5110 int ret = 0;
5111 struct lttng_ht_iter iter, uiter;
5112 struct lttng_ht_node_str *ua_chan_node;
5113 struct ust_app *app;
5114 struct ust_app_session *ua_sess;
5115 struct ust_app_channel *ua_chan;
5116 struct ust_app_event *ua_event;
5117
5118 LTTNG_ASSERT(usess->active);
5119 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
5120 uevent->attr.name,
5121 usess->id);
5122
5123 /*
5124 * NOTE: At this point, this function is called only if the session and
5125 * channel passed are already created for all apps. and enabled on the
5126 * tracer also.
5127 */
5128
5129 {
5130 lttng::urcu::read_lock_guard read_lock;
5131
5132 /* For all registered applications */
5133 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5134 if (!app->compatible) {
5135 /*
5136 * TODO: In time, we should notice the caller of this error by
5137 * telling him that this is a version error.
5138 */
5139 continue;
5140 }
5141 ua_sess = lookup_session_by_app(usess, app);
5142 if (!ua_sess) {
5143 /* The application has problem or is probably dead. */
5144 continue;
5145 }
5146
5147 pthread_mutex_lock(&ua_sess->lock);
5148
5149 if (ua_sess->deleted) {
5150 pthread_mutex_unlock(&ua_sess->lock);
5151 continue;
5152 }
5153
5154 /* Lookup channel in the ust app session */
5155 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
5156 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5157 /*
5158 * It is possible that the channel cannot be found is
5159 * the channel/event creation occurs concurrently with
5160 * an application exit.
5161 */
5162 if (!ua_chan_node) {
5163 pthread_mutex_unlock(&ua_sess->lock);
5164 continue;
5165 }
5166
5167 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5168
5169 /* Get event node */
5170 ua_event = find_ust_app_event(
5171 ua_chan->events,
5172 uevent->attr.name,
5173 uevent->filter,
5174 (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type,
5175 uevent->attr.loglevel,
5176 uevent->exclusion);
5177 if (ua_event == nullptr) {
5178 DBG3("UST app enable event %s not found for app PID %d."
5179 "Skipping app",
5180 uevent->attr.name,
5181 app->pid);
5182 goto next_app;
5183 }
5184
5185 ret = enable_ust_app_event(ua_event, app);
5186 if (ret < 0) {
5187 pthread_mutex_unlock(&ua_sess->lock);
5188 goto error;
5189 }
5190 next_app:
5191 pthread_mutex_unlock(&ua_sess->lock);
5192 }
5193 }
5194 error:
5195 return ret;
5196 }
5197
5198 /*
5199 * For a specific existing UST session and UST channel, creates the event for
5200 * all registered apps.
5201 */
5202 int ust_app_create_event_glb(struct ltt_ust_session *usess,
5203 struct ltt_ust_channel *uchan,
5204 struct ltt_ust_event *uevent)
5205 {
5206 int ret = 0;
5207 struct lttng_ht_iter iter, uiter;
5208 struct lttng_ht_node_str *ua_chan_node;
5209 struct ust_app *app;
5210 struct ust_app_session *ua_sess;
5211 struct ust_app_channel *ua_chan;
5212
5213 LTTNG_ASSERT(usess->active);
5214 DBG("UST app creating event %s for all apps for session id %" PRIu64,
5215 uevent->attr.name,
5216 usess->id);
5217
5218 {
5219 lttng::urcu::read_lock_guard read_lock;
5220
5221 /* For all registered applications */
5222 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5223 if (!app->compatible) {
5224 /*
5225 * TODO: In time, we should notice the caller of this error by
5226 * telling him that this is a version error.
5227 */
5228 continue;
5229 }
5230
5231 ua_sess = lookup_session_by_app(usess, app);
5232 if (!ua_sess) {
5233 /* The application has problem or is probably dead. */
5234 continue;
5235 }
5236
5237 pthread_mutex_lock(&ua_sess->lock);
5238
5239 if (ua_sess->deleted) {
5240 pthread_mutex_unlock(&ua_sess->lock);
5241 continue;
5242 }
5243
5244 /* Lookup channel in the ust app session */
5245 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
5246 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5247 /* If the channel is not found, there is a code flow error */
5248 LTTNG_ASSERT(ua_chan_node);
5249
5250 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
5251
5252 ret = create_ust_app_event(ua_chan, uevent, app);
5253 pthread_mutex_unlock(&ua_sess->lock);
5254 if (ret < 0) {
5255 if (ret != -LTTNG_UST_ERR_EXIST) {
5256 /* Possible value at this point: -ENOMEM. If so, we stop! */
5257 break;
5258 }
5259
5260 DBG2("UST app event %s already exist on app PID %d",
5261 uevent->attr.name,
5262 app->pid);
5263 continue;
5264 }
5265 }
5266 }
5267
5268 return ret;
5269 }
5270
5271 /*
5272 * Start tracing for a specific UST session and app.
5273 *
5274 * Called with UST app session lock held.
5275 *
5276 */
5277 static int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
5278 {
5279 int ret = 0;
5280 struct ust_app_session *ua_sess;
5281
5282 DBG("Starting tracing for ust app pid %d", app->pid);
5283
5284 lttng::urcu::read_lock_guard read_lock;
5285
5286 if (!app->compatible) {
5287 goto end;
5288 }
5289
5290 ua_sess = lookup_session_by_app(usess, app);
5291 if (ua_sess == nullptr) {
5292 /* The session is in teardown process. Ignore and continue. */
5293 goto end;
5294 }
5295
5296 pthread_mutex_lock(&ua_sess->lock);
5297
5298 if (ua_sess->deleted) {
5299 pthread_mutex_unlock(&ua_sess->lock);
5300 goto end;
5301 }
5302
5303 if (ua_sess->enabled) {
5304 pthread_mutex_unlock(&ua_sess->lock);
5305 goto end;
5306 }
5307
5308 /* Upon restart, we skip the setup, already done */
5309 if (ua_sess->started) {
5310 goto skip_setup;
5311 }
5312
5313 health_code_update();
5314
5315 skip_setup:
5316 /* This starts the UST tracing */
5317 pthread_mutex_lock(&app->sock_lock);
5318 ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle);
5319 pthread_mutex_unlock(&app->sock_lock);
5320 if (ret < 0) {
5321 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5322 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5323 app->pid,
5324 app->sock);
5325 pthread_mutex_unlock(&ua_sess->lock);
5326 goto end;
5327 } else if (ret == -EAGAIN) {
5328 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5329 app->pid,
5330 app->sock);
5331 pthread_mutex_unlock(&ua_sess->lock);
5332 goto end;
5333
5334 } else {
5335 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5336 ret,
5337 app->pid,
5338 app->sock);
5339 }
5340 goto error_unlock;
5341 }
5342
5343 /* Indicate that the session has been started once */
5344 ua_sess->started = true;
5345 ua_sess->enabled = true;
5346
5347 pthread_mutex_unlock(&ua_sess->lock);
5348
5349 health_code_update();
5350
5351 /* Quiescent wait after starting trace */
5352 pthread_mutex_lock(&app->sock_lock);
5353 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5354 pthread_mutex_unlock(&app->sock_lock);
5355 if (ret < 0) {
5356 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5357 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5358 app->pid,
5359 app->sock);
5360 } else if (ret == -EAGAIN) {
5361 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5362 app->pid,
5363 app->sock);
5364 } else {
5365 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5366 ret,
5367 app->pid,
5368 app->sock);
5369 }
5370 }
5371
5372 end:
5373 health_code_update();
5374 return 0;
5375
5376 error_unlock:
5377 pthread_mutex_unlock(&ua_sess->lock);
5378 health_code_update();
5379 return -1;
5380 }
5381
5382 /*
5383 * Stop tracing for a specific UST session and app.
5384 */
5385 static int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
5386 {
5387 int ret = 0;
5388 struct ust_app_session *ua_sess;
5389
5390 DBG("Stopping tracing for ust app pid %d", app->pid);
5391
5392 lttng::urcu::read_lock_guard read_lock;
5393
5394 if (!app->compatible) {
5395 goto end_no_session;
5396 }
5397
5398 ua_sess = lookup_session_by_app(usess, app);
5399 if (ua_sess == nullptr) {
5400 goto end_no_session;
5401 }
5402
5403 pthread_mutex_lock(&ua_sess->lock);
5404
5405 if (ua_sess->deleted) {
5406 pthread_mutex_unlock(&ua_sess->lock);
5407 goto end_no_session;
5408 }
5409
5410 /*
5411 * If started = 0, it means that stop trace has been called for a session
5412 * that was never started. It's possible since we can have a fail start
5413 * from either the application manager thread or the command thread. Simply
5414 * indicate that this is a stop error.
5415 */
5416 if (!ua_sess->started) {
5417 goto error_rcu_unlock;
5418 }
5419
5420 health_code_update();
5421
5422 /* This inhibits UST tracing */
5423 pthread_mutex_lock(&app->sock_lock);
5424 ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle);
5425 pthread_mutex_unlock(&app->sock_lock);
5426 if (ret < 0) {
5427 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5428 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5429 app->pid,
5430 app->sock);
5431 goto end_unlock;
5432 } else if (ret == -EAGAIN) {
5433 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5434 app->pid,
5435 app->sock);
5436 goto end_unlock;
5437
5438 } else {
5439 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5440 ret,
5441 app->pid,
5442 app->sock);
5443 }
5444 goto error_rcu_unlock;
5445 }
5446
5447 health_code_update();
5448 ua_sess->enabled = false;
5449
5450 /* Quiescent wait after stopping trace */
5451 pthread_mutex_lock(&app->sock_lock);
5452 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5453 pthread_mutex_unlock(&app->sock_lock);
5454 if (ret < 0) {
5455 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5456 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5457 app->pid,
5458 app->sock);
5459 } else if (ret == -EAGAIN) {
5460 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5461 app->pid,
5462 app->sock);
5463 } else {
5464 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5465 ret,
5466 app->pid,
5467 app->sock);
5468 }
5469 }
5470
5471 health_code_update();
5472
5473 {
5474 auto locked_registry = get_locked_session_registry(ua_sess);
5475
5476 /* The UST app session is held registry shall not be null. */
5477 LTTNG_ASSERT(locked_registry);
5478
5479 /* Push metadata for application before freeing the application. */
5480 (void) push_metadata(locked_registry, ua_sess->consumer);
5481 }
5482
5483 end_unlock:
5484 pthread_mutex_unlock(&ua_sess->lock);
5485 end_no_session:
5486 health_code_update();
5487 return 0;
5488
5489 error_rcu_unlock:
5490 pthread_mutex_unlock(&ua_sess->lock);
5491 health_code_update();
5492 return -1;
5493 }
5494
5495 static int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
5496 {
5497 int ret, retval = 0;
5498 struct lttng_ht_iter iter;
5499 struct ust_app_channel *ua_chan;
5500 struct consumer_socket *socket;
5501
5502 DBG("Flushing app session buffers for ust app pid %d", app->pid);
5503
5504 if (!app->compatible) {
5505 goto end_not_compatible;
5506 }
5507
5508 pthread_mutex_lock(&ua_sess->lock);
5509
5510 if (ua_sess->deleted) {
5511 goto end_deleted;
5512 }
5513
5514 health_code_update();
5515
5516 /* Flushing buffers */
5517 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, ua_sess->consumer);
5518
5519 /* Flush buffers and push metadata. */
5520 switch (ua_sess->buffer_type) {
5521 case LTTNG_BUFFER_PER_PID:
5522 {
5523 lttng::urcu::read_lock_guard read_lock;
5524
5525 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
5526 health_code_update();
5527 ret = consumer_flush_channel(socket, ua_chan->key);
5528 if (ret) {
5529 ERR("Error flushing consumer channel");
5530 retval = -1;
5531 continue;
5532 }
5533 }
5534
5535 break;
5536 }
5537 case LTTNG_BUFFER_PER_UID:
5538 default:
5539 abort();
5540 break;
5541 }
5542
5543 health_code_update();
5544
5545 end_deleted:
5546 pthread_mutex_unlock(&ua_sess->lock);
5547
5548 end_not_compatible:
5549 health_code_update();
5550 return retval;
5551 }
5552
5553 /*
5554 * Flush buffers for all applications for a specific UST session.
5555 * Called with UST session lock held.
5556 */
5557 static int ust_app_flush_session(struct ltt_ust_session *usess)
5558
5559 {
5560 int ret = 0;
5561
5562 DBG("Flushing session buffers for all ust apps");
5563
5564 /* Flush buffers and push metadata. */
5565 switch (usess->buffer_type) {
5566 case LTTNG_BUFFER_PER_UID:
5567 {
5568 struct buffer_reg_uid *reg;
5569 struct lttng_ht_iter iter;
5570
5571 /* Flush all per UID buffers associated to that session. */
5572 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
5573 lttng::urcu::read_lock_guard read_lock;
5574 lsu::registry_session *ust_session_reg;
5575 struct buffer_reg_channel *buf_reg_chan;
5576 struct consumer_socket *socket;
5577
5578 /* Get consumer socket to use to push the metadata.*/
5579 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5580 usess->consumer);
5581 if (!socket) {
5582 /* Ignore request if no consumer is found for the session. */
5583 continue;
5584 }
5585
5586 cds_lfht_for_each_entry (
5587 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
5588 /*
5589 * The following call will print error values so the return
5590 * code is of little importance because whatever happens, we
5591 * have to try them all.
5592 */
5593 (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key);
5594 }
5595
5596 ust_session_reg = reg->registry->reg.ust;
5597 /* Push metadata. */
5598 auto locked_registry = ust_session_reg->lock();
5599 (void) push_metadata(locked_registry, usess->consumer);
5600 }
5601
5602 break;
5603 }
5604 case LTTNG_BUFFER_PER_PID:
5605 {
5606 struct ust_app_session *ua_sess;
5607 struct lttng_ht_iter iter;
5608 struct ust_app *app;
5609 lttng::urcu::read_lock_guard read_lock;
5610
5611 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5612 ua_sess = lookup_session_by_app(usess, app);
5613 if (ua_sess == nullptr) {
5614 continue;
5615 }
5616
5617 (void) ust_app_flush_app_session(app, ua_sess);
5618 }
5619
5620 break;
5621 }
5622 default:
5623 ret = -1;
5624 abort();
5625 break;
5626 }
5627
5628 health_code_update();
5629 return ret;
5630 }
5631
5632 static int ust_app_clear_quiescent_app_session(struct ust_app *app, struct ust_app_session *ua_sess)
5633 {
5634 int ret = 0;
5635 struct lttng_ht_iter iter;
5636 struct ust_app_channel *ua_chan;
5637 struct consumer_socket *socket;
5638
5639 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
5640
5641 lttng::urcu::read_lock_guard read_lock;
5642
5643 if (!app->compatible) {
5644 goto end_not_compatible;
5645 }
5646
5647 pthread_mutex_lock(&ua_sess->lock);
5648
5649 if (ua_sess->deleted) {
5650 goto end_unlock;
5651 }
5652
5653 health_code_update();
5654
5655 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, ua_sess->consumer);
5656 if (!socket) {
5657 ERR("Failed to find consumer (%" PRIu32 ") socket", app->abi.bits_per_long);
5658 ret = -1;
5659 goto end_unlock;
5660 }
5661
5662 /* Clear quiescent state. */
5663 switch (ua_sess->buffer_type) {
5664 case LTTNG_BUFFER_PER_PID:
5665 cds_lfht_for_each_entry (ua_sess->channels->ht, &iter.iter, ua_chan, node.node) {
5666 health_code_update();
5667 ret = consumer_clear_quiescent_channel(socket, ua_chan->key);
5668 if (ret) {
5669 ERR("Error clearing quiescent state for consumer channel");
5670 ret = -1;
5671 continue;
5672 }
5673 }
5674 break;
5675 case LTTNG_BUFFER_PER_UID:
5676 default:
5677 abort();
5678 ret = -1;
5679 break;
5680 }
5681
5682 health_code_update();
5683
5684 end_unlock:
5685 pthread_mutex_unlock(&ua_sess->lock);
5686
5687 end_not_compatible:
5688 health_code_update();
5689 return ret;
5690 }
5691
5692 /*
5693 * Clear quiescent state in each stream for all applications for a
5694 * specific UST session.
5695 * Called with UST session lock held.
5696 */
5697 static int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
5698
5699 {
5700 int ret = 0;
5701
5702 DBG("Clearing stream quiescent state for all ust apps");
5703
5704 switch (usess->buffer_type) {
5705 case LTTNG_BUFFER_PER_UID:
5706 {
5707 struct lttng_ht_iter iter;
5708 struct buffer_reg_uid *reg;
5709
5710 /*
5711 * Clear quiescent for all per UID buffers associated to
5712 * that session.
5713 */
5714 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
5715 struct consumer_socket *socket;
5716 struct buffer_reg_channel *buf_reg_chan;
5717 lttng::urcu::read_lock_guard read_lock;
5718
5719 /* Get associated consumer socket.*/
5720 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5721 usess->consumer);
5722 if (!socket) {
5723 /*
5724 * Ignore request if no consumer is found for
5725 * the session.
5726 */
5727 continue;
5728 }
5729
5730 cds_lfht_for_each_entry (
5731 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
5732 /*
5733 * The following call will print error values so
5734 * the return code is of little importance
5735 * because whatever happens, we have to try them
5736 * all.
5737 */
5738 (void) consumer_clear_quiescent_channel(socket,
5739 buf_reg_chan->consumer_key);
5740 }
5741 }
5742
5743 break;
5744 }
5745 case LTTNG_BUFFER_PER_PID:
5746 {
5747 struct ust_app_session *ua_sess;
5748 struct lttng_ht_iter iter;
5749 struct ust_app *app;
5750 lttng::urcu::read_lock_guard read_lock;
5751
5752 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5753 ua_sess = lookup_session_by_app(usess, app);
5754 if (ua_sess == nullptr) {
5755 continue;
5756 }
5757 (void) ust_app_clear_quiescent_app_session(app, ua_sess);
5758 }
5759
5760 break;
5761 }
5762 default:
5763 ret = -1;
5764 abort();
5765 break;
5766 }
5767
5768 health_code_update();
5769 return ret;
5770 }
5771
5772 /*
5773 * Destroy a specific UST session in apps.
5774 */
5775 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
5776 {
5777 int ret;
5778 struct ust_app_session *ua_sess;
5779 struct lttng_ht_iter iter;
5780 struct lttng_ht_node_u64 *node;
5781
5782 DBG("Destroy tracing for ust app pid %d", app->pid);
5783
5784 lttng::urcu::read_lock_guard read_lock;
5785
5786 if (!app->compatible) {
5787 goto end;
5788 }
5789
5790 __lookup_session_by_app(usess, app, &iter);
5791 node = lttng_ht_iter_get_node_u64(&iter);
5792 if (node == nullptr) {
5793 /* Session is being or is deleted. */
5794 goto end;
5795 }
5796 ua_sess = lttng::utils::container_of(node, &ust_app_session::node);
5797
5798 health_code_update();
5799 destroy_app_session(app, ua_sess);
5800
5801 health_code_update();
5802
5803 /* Quiescent wait after stopping trace */
5804 pthread_mutex_lock(&app->sock_lock);
5805 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5806 pthread_mutex_unlock(&app->sock_lock);
5807 if (ret < 0) {
5808 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5809 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5810 app->pid,
5811 app->sock);
5812 } else if (ret == -EAGAIN) {
5813 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5814 app->pid,
5815 app->sock);
5816 } else {
5817 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5818 ret,
5819 app->pid,
5820 app->sock);
5821 }
5822 }
5823 end:
5824 health_code_update();
5825 return 0;
5826 }
5827
5828 /*
5829 * Start tracing for the UST session.
5830 */
5831 int ust_app_start_trace_all(struct ltt_ust_session *usess)
5832 {
5833 struct lttng_ht_iter iter;
5834 struct ust_app *app;
5835
5836 DBG("Starting all UST traces");
5837
5838 /*
5839 * Even though the start trace might fail, flag this session active so
5840 * other application coming in are started by default.
5841 */
5842 usess->active = true;
5843
5844 /*
5845 * In a start-stop-start use-case, we need to clear the quiescent state
5846 * of each channel set by the prior stop command, thus ensuring that a
5847 * following stop or destroy is sure to grab a timestamp_end near those
5848 * operations, even if the packet is empty.
5849 */
5850 (void) ust_app_clear_quiescent_session(usess);
5851
5852 {
5853 lttng::urcu::read_lock_guard read_lock;
5854
5855 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5856 ust_app_global_update(usess, app);
5857 }
5858 }
5859
5860 return 0;
5861 }
5862
5863 /*
5864 * Start tracing for the UST session.
5865 * Called with UST session lock held.
5866 */
5867 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5868 {
5869 int ret = 0;
5870 struct lttng_ht_iter iter;
5871 struct ust_app *app;
5872
5873 DBG("Stopping all UST traces");
5874
5875 /*
5876 * Even though the stop trace might fail, flag this session inactive so
5877 * other application coming in are not started by default.
5878 */
5879 usess->active = false;
5880
5881 {
5882 lttng::urcu::read_lock_guard read_lock;
5883
5884 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5885 ret = ust_app_stop_trace(usess, app);
5886 if (ret < 0) {
5887 /* Continue to next apps even on error */
5888 continue;
5889 }
5890 }
5891 }
5892
5893 (void) ust_app_flush_session(usess);
5894
5895 return 0;
5896 }
5897
5898 /*
5899 * Destroy app UST session.
5900 */
5901 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5902 {
5903 int ret = 0;
5904 struct lttng_ht_iter iter;
5905 struct ust_app *app;
5906
5907 DBG("Destroy all UST traces");
5908
5909 {
5910 lttng::urcu::read_lock_guard read_lock;
5911
5912 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5913 ret = destroy_trace(usess, app);
5914 if (ret < 0) {
5915 /* Continue to next apps even on error */
5916 continue;
5917 }
5918 }
5919 }
5920
5921 return 0;
5922 }
5923
5924 /* The ua_sess lock must be held by the caller. */
5925 static int find_or_create_ust_app_channel(struct ltt_ust_session *usess,
5926 struct ust_app_session *ua_sess,
5927 struct ust_app *app,
5928 struct ltt_ust_channel *uchan,
5929 struct ust_app_channel **ua_chan)
5930 {
5931 int ret = 0;
5932 struct lttng_ht_iter iter;
5933 struct lttng_ht_node_str *ua_chan_node;
5934
5935 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5936 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5937 if (ua_chan_node) {
5938 *ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5939 goto end;
5940 }
5941
5942 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5943 if (ret) {
5944 goto end;
5945 }
5946 end:
5947 return ret;
5948 }
5949
5950 static int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5951 struct ltt_ust_event *uevent,
5952 struct ust_app *app)
5953 {
5954 int ret = 0;
5955 struct ust_app_event *ua_event = nullptr;
5956
5957 ua_event = find_ust_app_event(ua_chan->events,
5958 uevent->attr.name,
5959 uevent->filter,
5960 (enum lttng_ust_abi_loglevel_type) uevent->attr.loglevel_type,
5961 uevent->attr.loglevel,
5962 uevent->exclusion);
5963 if (!ua_event) {
5964 ret = create_ust_app_event(ua_chan, uevent, app);
5965 if (ret < 0) {
5966 goto end;
5967 }
5968 } else {
5969 if (ua_event->enabled != uevent->enabled) {
5970 ret = uevent->enabled ? enable_ust_app_event(ua_event, app) :
5971 disable_ust_app_event(ua_event, app);
5972 }
5973 }
5974
5975 end:
5976 return ret;
5977 }
5978
5979 /* Called with RCU read-side lock held. */
5980 static void ust_app_synchronize_event_notifier_rules(struct ust_app *app)
5981 {
5982 int ret = 0;
5983 enum lttng_error_code ret_code;
5984 enum lttng_trigger_status t_status;
5985 struct lttng_ht_iter app_trigger_iter;
5986 struct lttng_triggers *triggers = nullptr;
5987 struct ust_app_event_notifier_rule *event_notifier_rule;
5988 unsigned int count, i;
5989
5990 ASSERT_RCU_READ_LOCKED();
5991
5992 if (!ust_app_supports_notifiers(app)) {
5993 goto end;
5994 }
5995
5996 /*
5997 * Currrently, registering or unregistering a trigger with an
5998 * event rule condition causes a full synchronization of the event
5999 * notifiers.
6000 *
6001 * The first step attempts to add an event notifier for all registered
6002 * triggers that apply to the user space tracers. Then, the
6003 * application's event notifiers rules are all checked against the list
6004 * of registered triggers. Any event notifier that doesn't have a
6005 * matching trigger can be assumed to have been disabled.
6006 *
6007 * All of this is inefficient, but is put in place to get the feature
6008 * rolling as it is simpler at this moment. It will be optimized Soon™
6009 * to allow the state of enabled
6010 * event notifiers to be synchronized in a piece-wise way.
6011 */
6012
6013 /* Get all triggers using uid 0 (root) */
6014 ret_code = notification_thread_command_list_triggers(
6015 the_notification_thread_handle, 0, &triggers);
6016 if (ret_code != LTTNG_OK) {
6017 goto end;
6018 }
6019
6020 LTTNG_ASSERT(triggers);
6021
6022 t_status = lttng_triggers_get_count(triggers, &count);
6023 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
6024 goto end;
6025 }
6026
6027 for (i = 0; i < count; i++) {
6028 struct lttng_condition *condition;
6029 struct lttng_event_rule *event_rule;
6030 struct lttng_trigger *trigger;
6031 const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule;
6032 enum lttng_condition_status condition_status;
6033 uint64_t token;
6034
6035 trigger = lttng_triggers_borrow_mutable_at_index(triggers, i);
6036 LTTNG_ASSERT(trigger);
6037
6038 token = lttng_trigger_get_tracer_token(trigger);
6039 condition = lttng_trigger_get_condition(trigger);
6040
6041 if (lttng_condition_get_type(condition) !=
6042 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) {
6043 /* Does not apply */
6044 continue;
6045 }
6046
6047 condition_status = lttng_condition_event_rule_matches_borrow_rule_mutable(
6048 condition, &event_rule);
6049 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
6050
6051 if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) {
6052 /* Skip kernel related triggers. */
6053 continue;
6054 }
6055
6056 /*
6057 * Find or create the associated token event rule. The caller
6058 * holds the RCU read lock, so this is safe to call without
6059 * explicitly acquiring it here.
6060 */
6061 looked_up_event_notifier_rule = find_ust_app_event_notifier_rule(
6062 app->token_to_event_notifier_rule_ht, token);
6063 if (!looked_up_event_notifier_rule) {
6064 ret = create_ust_app_event_notifier_rule(trigger, app);
6065 if (ret < 0) {
6066 goto end;
6067 }
6068 }
6069 }
6070
6071 {
6072 lttng::urcu::read_lock_guard read_lock;
6073
6074 /* Remove all unknown event sources from the app. */
6075 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
6076 &app_trigger_iter.iter,
6077 event_notifier_rule,
6078 node.node) {
6079 const uint64_t app_token = event_notifier_rule->token;
6080 bool found = false;
6081
6082 /*
6083 * Check if the app event trigger still exists on the
6084 * notification side.
6085 */
6086 for (i = 0; i < count; i++) {
6087 uint64_t notification_thread_token;
6088 const struct lttng_trigger *trigger =
6089 lttng_triggers_get_at_index(triggers, i);
6090
6091 LTTNG_ASSERT(trigger);
6092
6093 notification_thread_token = lttng_trigger_get_tracer_token(trigger);
6094
6095 if (notification_thread_token == app_token) {
6096 found = true;
6097 break;
6098 }
6099 }
6100
6101 if (found) {
6102 /* Still valid. */
6103 continue;
6104 }
6105
6106 /*
6107 * This trigger was unregistered, disable it on the tracer's
6108 * side.
6109 */
6110 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht, &app_trigger_iter);
6111 LTTNG_ASSERT(ret == 0);
6112
6113 /* Callee logs errors. */
6114 (void) disable_ust_object(app, event_notifier_rule->obj);
6115
6116 delete_ust_app_event_notifier_rule(app->sock, event_notifier_rule, app);
6117 }
6118 }
6119
6120 end:
6121 lttng_triggers_destroy(triggers);
6122 return;
6123 }
6124
6125 /*
6126 * RCU read lock must be held by the caller.
6127 */
6128 static void ust_app_synchronize_all_channels(struct ltt_ust_session *usess,
6129 struct ust_app_session *ua_sess,
6130 struct ust_app *app)
6131 {
6132 int ret = 0;
6133 struct cds_lfht_iter uchan_iter;
6134 struct ltt_ust_channel *uchan;
6135
6136 LTTNG_ASSERT(usess);
6137 LTTNG_ASSERT(ua_sess);
6138 LTTNG_ASSERT(app);
6139 ASSERT_RCU_READ_LOCKED();
6140
6141 cds_lfht_for_each_entry (usess->domain_global.channels->ht, &uchan_iter, uchan, node.node) {
6142 struct ust_app_channel *ua_chan;
6143 struct cds_lfht_iter uevent_iter;
6144 struct ltt_ust_event *uevent;
6145
6146 /*
6147 * Search for a matching ust_app_channel. If none is found,
6148 * create it. Creating the channel will cause the ua_chan
6149 * structure to be allocated, the channel buffers to be
6150 * allocated (if necessary) and sent to the application, and
6151 * all enabled contexts will be added to the channel.
6152 */
6153 ret = find_or_create_ust_app_channel(usess, ua_sess, app, uchan, &ua_chan);
6154 if (ret) {
6155 /* Tracer is probably gone or ENOMEM. */
6156 goto end;
6157 }
6158
6159 if (!ua_chan) {
6160 /* ua_chan will be NULL for the metadata channel */
6161 continue;
6162 }
6163
6164 cds_lfht_for_each_entry (uchan->events->ht, &uevent_iter, uevent, node.node) {
6165 ret = ust_app_channel_synchronize_event(ua_chan, uevent, app);
6166 if (ret) {
6167 goto end;
6168 }
6169 }
6170
6171 if (ua_chan->enabled != uchan->enabled) {
6172 ret = uchan->enabled ? enable_ust_app_channel(ua_sess, uchan, app) :
6173 disable_ust_app_channel(ua_sess, ua_chan, app);
6174 if (ret) {
6175 goto end;
6176 }
6177 }
6178 }
6179 end:
6180 return;
6181 }
6182
6183 /*
6184 * The caller must ensure that the application is compatible and is tracked
6185 * by the process attribute trackers.
6186 */
6187 static void ust_app_synchronize(struct ltt_ust_session *usess, struct ust_app *app)
6188 {
6189 int ret = 0;
6190 struct ust_app_session *ua_sess = nullptr;
6191
6192 /*
6193 * The application's configuration should only be synchronized for
6194 * active sessions.
6195 */
6196 LTTNG_ASSERT(usess->active);
6197
6198 ret = find_or_create_ust_app_session(usess, app, &ua_sess, nullptr);
6199 if (ret < 0) {
6200 /* Tracer is probably gone or ENOMEM. */
6201 goto end;
6202 }
6203
6204 LTTNG_ASSERT(ua_sess);
6205
6206 pthread_mutex_lock(&ua_sess->lock);
6207 if (ua_sess->deleted) {
6208 goto deleted_session;
6209 }
6210
6211 {
6212 lttng::urcu::read_lock_guard read_lock;
6213
6214 ust_app_synchronize_all_channels(usess, ua_sess, app);
6215
6216 /*
6217 * Create the metadata for the application. This returns gracefully if a
6218 * metadata was already set for the session.
6219 *
6220 * The metadata channel must be created after the data channels as the
6221 * consumer daemon assumes this ordering. When interacting with a relay
6222 * daemon, the consumer will use this assumption to send the
6223 * "STREAMS_SENT" message to the relay daemon.
6224 */
6225 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
6226 if (ret < 0) {
6227 ERR("Metadata creation failed for app sock %d for session id %" PRIu64,
6228 app->sock,
6229 usess->id);
6230 }
6231 }
6232
6233 deleted_session:
6234 pthread_mutex_unlock(&ua_sess->lock);
6235 end:
6236 return;
6237 }
6238
6239 static void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
6240 {
6241 struct ust_app_session *ua_sess;
6242
6243 ua_sess = lookup_session_by_app(usess, app);
6244 if (ua_sess == nullptr) {
6245 return;
6246 }
6247 destroy_app_session(app, ua_sess);
6248 }
6249
6250 /*
6251 * Add channels/events from UST global domain to registered apps at sock.
6252 *
6253 * Called with session lock held.
6254 * Called with RCU read-side lock held.
6255 */
6256 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
6257 {
6258 LTTNG_ASSERT(usess);
6259 LTTNG_ASSERT(usess->active);
6260 ASSERT_RCU_READ_LOCKED();
6261
6262 DBG2("UST app global update for app sock %d for session id %" PRIu64, app->sock, usess->id);
6263
6264 if (!app->compatible) {
6265 return;
6266 }
6267 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID, usess, app->pid) &&
6268 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID, usess, app->uid) &&
6269 trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID, usess, app->gid)) {
6270 /*
6271 * Synchronize the application's internal tracing configuration
6272 * and start tracing.
6273 */
6274 ust_app_synchronize(usess, app);
6275 ust_app_start_trace(usess, app);
6276 } else {
6277 ust_app_global_destroy(usess, app);
6278 }
6279 }
6280
6281 /*
6282 * Add all event notifiers to an application.
6283 *
6284 * Called with session lock held.
6285 * Called with RCU read-side lock held.
6286 */
6287 void ust_app_global_update_event_notifier_rules(struct ust_app *app)
6288 {
6289 ASSERT_RCU_READ_LOCKED();
6290
6291 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6292 app->name,
6293 app->pid);
6294
6295 if (!app->compatible || !ust_app_supports_notifiers(app)) {
6296 return;
6297 }
6298
6299 if (app->event_notifier_group.object == nullptr) {
6300 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6301 app->name,
6302 app->pid);
6303 return;
6304 }
6305
6306 ust_app_synchronize_event_notifier_rules(app);
6307 }
6308
6309 /*
6310 * Called with session lock held.
6311 */
6312 void ust_app_global_update_all(struct ltt_ust_session *usess)
6313 {
6314 struct lttng_ht_iter iter;
6315 struct ust_app *app;
6316
6317 {
6318 lttng::urcu::read_lock_guard read_lock;
6319
6320 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6321 ust_app_global_update(usess, app);
6322 }
6323 }
6324 }
6325
6326 void ust_app_global_update_all_event_notifier_rules()
6327 {
6328 struct lttng_ht_iter iter;
6329 struct ust_app *app;
6330
6331 lttng::urcu::read_lock_guard read_lock;
6332 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6333 ust_app_global_update_event_notifier_rules(app);
6334 }
6335 }
6336
6337 /*
6338 * Add context to a specific channel for global UST domain.
6339 */
6340 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
6341 struct ltt_ust_channel *uchan,
6342 struct ltt_ust_context *uctx)
6343 {
6344 int ret = 0;
6345 struct lttng_ht_node_str *ua_chan_node;
6346 struct lttng_ht_iter iter, uiter;
6347 struct ust_app_channel *ua_chan = nullptr;
6348 struct ust_app_session *ua_sess;
6349 struct ust_app *app;
6350
6351 LTTNG_ASSERT(usess->active);
6352
6353 {
6354 lttng::urcu::read_lock_guard read_lock;
6355 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6356 if (!app->compatible) {
6357 /*
6358 * TODO: In time, we should notice the caller of this error by
6359 * telling him that this is a version error.
6360 */
6361 continue;
6362 }
6363 ua_sess = lookup_session_by_app(usess, app);
6364 if (ua_sess == nullptr) {
6365 continue;
6366 }
6367
6368 pthread_mutex_lock(&ua_sess->lock);
6369
6370 if (ua_sess->deleted) {
6371 pthread_mutex_unlock(&ua_sess->lock);
6372 continue;
6373 }
6374
6375 /* Lookup channel in the ust app session */
6376 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
6377 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6378 if (ua_chan_node == nullptr) {
6379 goto next_app;
6380 }
6381 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
6382 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
6383 if (ret < 0) {
6384 goto next_app;
6385 }
6386 next_app:
6387 pthread_mutex_unlock(&ua_sess->lock);
6388 }
6389 }
6390
6391 return ret;
6392 }
6393
6394 /*
6395 * Receive registration and populate the given msg structure.
6396 *
6397 * On success return 0 else a negative value returned by the ustctl call.
6398 */
6399 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
6400 {
6401 int ret;
6402 uint32_t pid, ppid, uid, gid;
6403
6404 LTTNG_ASSERT(msg);
6405
6406 ret = lttng_ust_ctl_recv_reg_msg(sock,
6407 &msg->type,
6408 &msg->major,
6409 &msg->minor,
6410 &pid,
6411 &ppid,
6412 &uid,
6413 &gid,
6414 &msg->bits_per_long,
6415 &msg->uint8_t_alignment,
6416 &msg->uint16_t_alignment,
6417 &msg->uint32_t_alignment,
6418 &msg->uint64_t_alignment,
6419 &msg->long_alignment,
6420 &msg->byte_order,
6421 msg->name);
6422 if (ret < 0) {
6423 switch (-ret) {
6424 case EPIPE:
6425 case ECONNRESET:
6426 case LTTNG_UST_ERR_EXITING:
6427 DBG3("UST app recv reg message failed. Application died");
6428 break;
6429 case LTTNG_UST_ERR_UNSUP_MAJOR:
6430 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6431 msg->major,
6432 msg->minor,
6433 LTTNG_UST_ABI_MAJOR_VERSION,
6434 LTTNG_UST_ABI_MINOR_VERSION);
6435 break;
6436 default:
6437 ERR("UST app recv reg message failed with ret %d", ret);
6438 break;
6439 }
6440 goto error;
6441 }
6442 msg->pid = (pid_t) pid;
6443 msg->ppid = (pid_t) ppid;
6444 msg->uid = (uid_t) uid;
6445 msg->gid = (gid_t) gid;
6446
6447 error:
6448 return ret;
6449 }
6450
6451 /*
6452 * Return a ust app session object using the application object and the
6453 * session object descriptor has a key. If not found, NULL is returned.
6454 * A RCU read side lock MUST be acquired when calling this function.
6455 */
6456 static struct ust_app_session *find_session_by_objd(struct ust_app *app, int objd)
6457 {
6458 struct lttng_ht_node_ulong *node;
6459 struct lttng_ht_iter iter;
6460 struct ust_app_session *ua_sess = nullptr;
6461
6462 LTTNG_ASSERT(app);
6463 ASSERT_RCU_READ_LOCKED();
6464
6465 lttng_ht_lookup(app->ust_sessions_objd, (void *) ((unsigned long) objd), &iter);
6466 node = lttng_ht_iter_get_node_ulong(&iter);
6467 if (node == nullptr) {
6468 DBG2("UST app session find by objd %d not found", objd);
6469 goto error;
6470 }
6471
6472 ua_sess = lttng::utils::container_of(node, &ust_app_session::ust_objd_node);
6473
6474 error:
6475 return ua_sess;
6476 }
6477
6478 /*
6479 * Return a ust app channel object using the application object and the channel
6480 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6481 * lock MUST be acquired before calling this function.
6482 */
6483 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app, int objd)
6484 {
6485 struct lttng_ht_node_ulong *node;
6486 struct lttng_ht_iter iter;
6487 struct ust_app_channel *ua_chan = nullptr;
6488
6489 LTTNG_ASSERT(app);
6490 ASSERT_RCU_READ_LOCKED();
6491
6492 lttng_ht_lookup(app->ust_objd, (void *) ((unsigned long) objd), &iter);
6493 node = lttng_ht_iter_get_node_ulong(&iter);
6494 if (node == nullptr) {
6495 DBG2("UST app channel find by objd %d not found", objd);
6496 goto error;
6497 }
6498
6499 ua_chan = lttng::utils::container_of(node, &ust_app_channel::ust_objd_node);
6500
6501 error:
6502 return ua_chan;
6503 }
6504
6505 /*
6506 * Reply to a register channel notification from an application on the notify
6507 * socket. The channel metadata is also created.
6508 *
6509 * The session UST registry lock is acquired in this function.
6510 *
6511 * On success 0 is returned else a negative value.
6512 */
6513 static int handle_app_register_channel_notification(int sock,
6514 int cobjd,
6515 struct lttng_ust_ctl_field *raw_context_fields,
6516 size_t context_field_count)
6517 {
6518 int ret, ret_code = 0;
6519 uint32_t chan_id;
6520 uint64_t chan_reg_key;
6521 struct ust_app *app;
6522 struct ust_app_channel *ua_chan;
6523 struct ust_app_session *ua_sess;
6524 auto ust_ctl_context_fields =
6525 lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_context_fields);
6526
6527 lttng::urcu::read_lock_guard read_lock_guard;
6528
6529 /* Lookup application. If not found, there is a code flow error. */
6530 app = find_app_by_notify_sock(sock);
6531 if (!app) {
6532 DBG("Application socket %d is being torn down. Abort event notify", sock);
6533 return -1;
6534 }
6535
6536 /* Lookup channel by UST object descriptor. */
6537 ua_chan = find_channel_by_objd(app, cobjd);
6538 if (!ua_chan) {
6539 DBG("Application channel is being torn down. Abort event notify");
6540 return 0;
6541 }
6542
6543 LTTNG_ASSERT(ua_chan->session);
6544 ua_sess = ua_chan->session;
6545
6546 /* Get right session registry depending on the session buffer type. */
6547 auto locked_registry_session = get_locked_session_registry(ua_sess);
6548 if (!locked_registry_session) {
6549 DBG("Application session is being torn down. Abort event notify");
6550 return 0;
6551 };
6552
6553 /* Depending on the buffer type, a different channel key is used. */
6554 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6555 chan_reg_key = ua_chan->tracing_channel_id;
6556 } else {
6557 chan_reg_key = ua_chan->key;
6558 }
6559
6560 auto& ust_reg_chan = locked_registry_session->channel(chan_reg_key);
6561
6562 /* Channel id is set during the object creation. */
6563 chan_id = ust_reg_chan.id;
6564
6565 /*
6566 * The application returns the typing information of the channel's
6567 * context fields. In per-PID buffering mode, this is the first and only
6568 * time we get this information. It is our chance to finalize the
6569 * initialiation of the channel and serialize it's layout's description
6570 * to the trace's metadata.
6571 *
6572 * However, in per-UID buffering mode, every application will provide
6573 * this information (redundantly). The first time will allow us to
6574 * complete the initialization. The following times, we simply validate
6575 * that all apps provide the same typing for the context fields as a
6576 * sanity check.
6577 */
6578 try {
6579 auto app_context_fields = lsu::create_trace_fields_from_ust_ctl_fields(
6580 *locked_registry_session,
6581 ust_ctl_context_fields.get(),
6582 context_field_count,
6583 lst::field_location::root::EVENT_RECORD_COMMON_CONTEXT,
6584 lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS);
6585
6586 if (!ust_reg_chan.is_registered()) {
6587 lst::type::cuptr event_context = app_context_fields.size() ?
6588 lttng::make_unique<lst::structure_type>(
6589 0, std::move(app_context_fields)) :
6590 nullptr;
6591
6592 ust_reg_chan.event_context(std::move(event_context));
6593 } else {
6594 /*
6595 * Validate that the context fields match between
6596 * registry and newcoming application.
6597 */
6598 bool context_fields_match;
6599 const auto *previous_event_context = ust_reg_chan.event_context();
6600
6601 if (!previous_event_context) {
6602 context_fields_match = app_context_fields.size() == 0;
6603 } else {
6604 const lst::structure_type app_event_context_struct(
6605 0, std::move(app_context_fields));
6606
6607 context_fields_match = *previous_event_context ==
6608 app_event_context_struct;
6609 }
6610
6611 if (!context_fields_match) {
6612 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6613 app->pid,
6614 app->sock);
6615 ret_code = -EINVAL;
6616 goto reply;
6617 }
6618 }
6619 } catch (const std::exception& ex) {
6620 ERR("Failed to handle application context: %s", ex.what());
6621 ret_code = -EINVAL;
6622 goto reply;
6623 }
6624
6625 reply:
6626 DBG3("UST app replying to register channel key %" PRIu64 " with id %u, ret = %d",
6627 chan_reg_key,
6628 chan_id,
6629 ret_code);
6630
6631 ret = lttng_ust_ctl_reply_register_channel(
6632 sock,
6633 chan_id,
6634 ust_reg_chan.header_type_ == lst::stream_class::header_type::COMPACT ?
6635 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT :
6636 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE,
6637 ret_code);
6638 if (ret < 0) {
6639 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6640 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6641 app->pid,
6642 app->sock);
6643 } else if (ret == -EAGAIN) {
6644 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6645 app->pid,
6646 app->sock);
6647 } else {
6648 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6649 ret,
6650 app->pid,
6651 app->sock);
6652 }
6653
6654 return ret;
6655 }
6656
6657 /* This channel registry's registration is completed. */
6658 ust_reg_chan.set_as_registered();
6659
6660 return ret;
6661 }
6662
6663 /*
6664 * Add event to the UST channel registry. When the event is added to the
6665 * registry, the metadata is also created. Once done, this replies to the
6666 * application with the appropriate error code.
6667 *
6668 * The session UST registry lock is acquired in the function.
6669 *
6670 * On success 0 is returned else a negative value.
6671 */
6672 static int add_event_ust_registry(int sock,
6673 int sobjd,
6674 int cobjd,
6675 const char *name,
6676 char *raw_signature,
6677 size_t nr_fields,
6678 struct lttng_ust_ctl_field *raw_fields,
6679 int loglevel_value,
6680 char *raw_model_emf_uri)
6681 {
6682 int ret, ret_code;
6683 uint32_t event_id = 0;
6684 uint64_t chan_reg_key;
6685 struct ust_app *app;
6686 struct ust_app_channel *ua_chan;
6687 struct ust_app_session *ua_sess;
6688 lttng::urcu::read_lock_guard rcu_lock;
6689 auto signature = lttng::make_unique_wrapper<char, lttng::free>(raw_signature);
6690 auto fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_fields);
6691 auto model_emf_uri = lttng::make_unique_wrapper<char, lttng::free>(raw_model_emf_uri);
6692
6693 /* Lookup application. If not found, there is a code flow error. */
6694 app = find_app_by_notify_sock(sock);
6695 if (!app) {
6696 DBG("Application socket %d is being torn down. Abort event notify", sock);
6697 return -1;
6698 }
6699
6700 /* Lookup channel by UST object descriptor. */
6701 ua_chan = find_channel_by_objd(app, cobjd);
6702 if (!ua_chan) {
6703 DBG("Application channel is being torn down. Abort event notify");
6704 return 0;
6705 }
6706
6707 LTTNG_ASSERT(ua_chan->session);
6708 ua_sess = ua_chan->session;
6709
6710 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6711 chan_reg_key = ua_chan->tracing_channel_id;
6712 } else {
6713 chan_reg_key = ua_chan->key;
6714 }
6715
6716 {
6717 auto locked_registry = get_locked_session_registry(ua_sess);
6718 if (locked_registry) {
6719 /*
6720 * From this point on, this call acquires the ownership of the signature,
6721 * fields and model_emf_uri meaning any free are done inside it if needed.
6722 * These three variables MUST NOT be read/write after this.
6723 */
6724 try {
6725 auto& channel = locked_registry->channel(chan_reg_key);
6726
6727 /* event_id is set on success. */
6728 channel.add_event(
6729 sobjd,
6730 cobjd,
6731 name,
6732 signature.get(),
6733 lsu::create_trace_fields_from_ust_ctl_fields(
6734 *locked_registry,
6735 fields.get(),
6736 nr_fields,
6737 lst::field_location::root::EVENT_RECORD_PAYLOAD,
6738 lsu::ctl_field_quirks::
6739 UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS),
6740 loglevel_value,
6741 model_emf_uri.get() ?
6742 nonstd::optional<std::string>(model_emf_uri.get()) :
6743 nonstd::nullopt,
6744 ua_sess->buffer_type,
6745 *app,
6746 event_id);
6747 ret_code = 0;
6748 } catch (const std::exception& ex) {
6749 ERR("Failed to add event `%s` to registry session: %s",
6750 name,
6751 ex.what());
6752 /* Inform the application of the error; don't return directly. */
6753 ret_code = -EINVAL;
6754 }
6755 } else {
6756 DBG("Application session is being torn down. Abort event notify");
6757 return 0;
6758 }
6759 }
6760
6761 /*
6762 * The return value is returned to ustctl so in case of an error, the
6763 * application can be notified. In case of an error, it's important not to
6764 * return a negative error or else the application will get closed.
6765 */
6766 ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code);
6767 if (ret < 0) {
6768 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6769 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6770 app->pid,
6771 app->sock);
6772 } else if (ret == -EAGAIN) {
6773 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6774 app->pid,
6775 app->sock);
6776 } else {
6777 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6778 ret,
6779 app->pid,
6780 app->sock);
6781 }
6782 /*
6783 * No need to wipe the create event since the application socket will
6784 * get close on error hence cleaning up everything by itself.
6785 */
6786 return ret;
6787 }
6788
6789 DBG3("UST registry event %s with id %" PRId32 " added successfully", name, event_id);
6790 return ret;
6791 }
6792
6793 /*
6794 * Add enum to the UST session registry. Once done, this replies to the
6795 * application with the appropriate error code.
6796 *
6797 * The session UST registry lock is acquired within this function.
6798 *
6799 * On success 0 is returned else a negative value.
6800 */
6801 static int add_enum_ust_registry(int sock,
6802 int sobjd,
6803 const char *name,
6804 struct lttng_ust_ctl_enum_entry *raw_entries,
6805 size_t nr_entries)
6806 {
6807 int ret = 0;
6808 struct ust_app *app;
6809 struct ust_app_session *ua_sess;
6810 uint64_t enum_id = -1ULL;
6811 lttng::urcu::read_lock_guard read_lock_guard;
6812 auto entries = lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::free>(
6813 raw_entries);
6814
6815 /* Lookup application. If not found, there is a code flow error. */
6816 app = find_app_by_notify_sock(sock);
6817 if (!app) {
6818 /* Return an error since this is not an error */
6819 DBG("Application socket %d is being torn down. Aborting enum registration", sock);
6820 return -1;
6821 }
6822
6823 /* Lookup session by UST object descriptor. */
6824 ua_sess = find_session_by_objd(app, sobjd);
6825 if (!ua_sess) {
6826 /* Return an error since this is not an error */
6827 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6828 return 0;
6829 }
6830
6831 auto locked_registry = get_locked_session_registry(ua_sess);
6832 if (!locked_registry) {
6833 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6834 return 0;
6835 }
6836
6837 /*
6838 * From this point on, the callee acquires the ownership of
6839 * entries. The variable entries MUST NOT be read/written after
6840 * call.
6841 */
6842 int application_reply_code;
6843 try {
6844 locked_registry->create_or_find_enum(
6845 sobjd, name, entries.release(), nr_entries, &enum_id);
6846 application_reply_code = 0;
6847 } catch (const std::exception& ex) {
6848 ERR("%s: %s",
6849 lttng::format(
6850 "Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6851 *app,
6852 name)
6853 .c_str(),
6854 ex.what());
6855 application_reply_code = -1;
6856 }
6857
6858 /*
6859 * The return value is returned to ustctl so in case of an error, the
6860 * application can be notified. In case of an error, it's important not to
6861 * return a negative error or else the application will get closed.
6862 */
6863 ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code);
6864 if (ret < 0) {
6865 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6866 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6867 app->pid,
6868 app->sock);
6869 } else if (ret == -EAGAIN) {
6870 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6871 app->pid,
6872 app->sock);
6873 } else {
6874 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6875 ret,
6876 app->pid,
6877 app->sock);
6878 }
6879 /*
6880 * No need to wipe the create enum since the application socket will
6881 * get close on error hence cleaning up everything by itself.
6882 */
6883 return ret;
6884 }
6885
6886 DBG3("UST registry enum %s added successfully or already found", name);
6887 return 0;
6888 }
6889
6890 /*
6891 * Handle application notification through the given notify socket.
6892 *
6893 * Return 0 on success or else a negative value.
6894 */
6895 int ust_app_recv_notify(int sock)
6896 {
6897 int ret;
6898 enum lttng_ust_ctl_notify_cmd cmd;
6899
6900 DBG3("UST app receiving notify from sock %d", sock);
6901
6902 ret = lttng_ust_ctl_recv_notify(sock, &cmd);
6903 if (ret < 0) {
6904 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6905 DBG3("UST app recv notify failed. Application died: sock = %d", sock);
6906 } else if (ret == -EAGAIN) {
6907 WARN("UST app recv notify failed. Communication time out: sock = %d", sock);
6908 } else {
6909 ERR("UST app recv notify failed with ret %d: sock = %d", ret, sock);
6910 }
6911 goto error;
6912 }
6913
6914 switch (cmd) {
6915 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT:
6916 {
6917 int sobjd, cobjd, loglevel_value;
6918 char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri;
6919 size_t nr_fields;
6920 struct lttng_ust_ctl_field *fields;
6921
6922 DBG2("UST app ustctl register event received");
6923
6924 ret = lttng_ust_ctl_recv_register_event(sock,
6925 &sobjd,
6926 &cobjd,
6927 name,
6928 &loglevel_value,
6929 &sig,
6930 &nr_fields,
6931 &fields,
6932 &model_emf_uri);
6933 if (ret < 0) {
6934 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6935 DBG3("UST app recv event failed. Application died: sock = %d",
6936 sock);
6937 } else if (ret == -EAGAIN) {
6938 WARN("UST app recv event failed. Communication time out: sock = %d",
6939 sock);
6940 } else {
6941 ERR("UST app recv event failed with ret %d: sock = %d", ret, sock);
6942 }
6943 goto error;
6944 }
6945
6946 {
6947 lttng::urcu::read_lock_guard rcu_lock;
6948 const struct ust_app *app = find_app_by_notify_sock(sock);
6949 if (!app) {
6950 DBG("Application socket %d is being torn down. Abort event notify",
6951 sock);
6952 ret = -1;
6953 goto error;
6954 }
6955 }
6956
6957 if ((!fields && nr_fields > 0) || (fields && nr_fields == 0)) {
6958 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6959 fields,
6960 nr_fields);
6961 ret = -1;
6962 free(fields);
6963 goto error;
6964 }
6965
6966 /*
6967 * Add event to the UST registry coming from the notify socket. This
6968 * call will free if needed the sig, fields and model_emf_uri. This
6969 * code path loses the ownsership of these variables and transfer them
6970 * to the this function.
6971 */
6972 ret = add_event_ust_registry(sock,
6973 sobjd,
6974 cobjd,
6975 name,
6976 sig,
6977 nr_fields,
6978 fields,
6979 loglevel_value,
6980 model_emf_uri);
6981 if (ret < 0) {
6982 goto error;
6983 }
6984
6985 break;
6986 }
6987 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL:
6988 {
6989 int sobjd, cobjd;
6990 size_t field_count;
6991 struct lttng_ust_ctl_field *context_fields;
6992
6993 DBG2("UST app ustctl register channel received");
6994
6995 ret = lttng_ust_ctl_recv_register_channel(
6996 sock, &sobjd, &cobjd, &field_count, &context_fields);
6997 if (ret < 0) {
6998 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6999 DBG3("UST app recv channel failed. Application died: sock = %d",
7000 sock);
7001 } else if (ret == -EAGAIN) {
7002 WARN("UST app recv channel failed. Communication time out: sock = %d",
7003 sock);
7004 } else {
7005 ERR("UST app recv channel failed with ret %d: sock = %d",
7006 ret,
7007 sock);
7008 }
7009 goto error;
7010 }
7011
7012 /*
7013 * The fields ownership are transfered to this function call meaning
7014 * that if needed it will be freed. After this, it's invalid to access
7015 * fields or clean them up.
7016 */
7017 ret = handle_app_register_channel_notification(
7018 sock, cobjd, context_fields, field_count);
7019 if (ret < 0) {
7020 goto error;
7021 }
7022
7023 break;
7024 }
7025 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM:
7026 {
7027 int sobjd;
7028 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
7029 size_t nr_entries;
7030 struct lttng_ust_ctl_enum_entry *entries;
7031
7032 DBG2("UST app ustctl register enum received");
7033
7034 ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name, &entries, &nr_entries);
7035 if (ret < 0) {
7036 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
7037 DBG3("UST app recv enum failed. Application died: sock = %d", sock);
7038 } else if (ret == -EAGAIN) {
7039 WARN("UST app recv enum failed. Communication time out: sock = %d",
7040 sock);
7041 } else {
7042 ERR("UST app recv enum failed with ret %d: sock = %d", ret, sock);
7043 }
7044 goto error;
7045 }
7046
7047 /* Callee assumes ownership of entries. */
7048 ret = add_enum_ust_registry(sock, sobjd, name, entries, nr_entries);
7049 if (ret < 0) {
7050 goto error;
7051 }
7052
7053 break;
7054 }
7055 default:
7056 /* Should NEVER happen. */
7057 abort();
7058 }
7059
7060 error:
7061 return ret;
7062 }
7063
7064 /*
7065 * Once the notify socket hangs up, this is called. First, it tries to find the
7066 * corresponding application. On failure, the call_rcu to close the socket is
7067 * executed. If an application is found, it tries to delete it from the notify
7068 * socket hash table. Whathever the result, it proceeds to the call_rcu.
7069 *
7070 * Note that an object needs to be allocated here so on ENOMEM failure, the
7071 * call RCU is not done but the rest of the cleanup is.
7072 */
7073 void ust_app_notify_sock_unregister(int sock)
7074 {
7075 int err_enomem = 0;
7076 struct lttng_ht_iter iter;
7077 struct ust_app *app;
7078 struct ust_app_notify_sock_obj *obj;
7079
7080 LTTNG_ASSERT(sock >= 0);
7081
7082 lttng::urcu::read_lock_guard read_lock;
7083
7084 obj = zmalloc<ust_app_notify_sock_obj>();
7085 if (!obj) {
7086 /*
7087 * An ENOMEM is kind of uncool. If this strikes we continue the
7088 * procedure but the call_rcu will not be called. In this case, we
7089 * accept the fd leak rather than possibly creating an unsynchronized
7090 * state between threads.
7091 *
7092 * TODO: The notify object should be created once the notify socket is
7093 * registered and stored independantely from the ust app object. The
7094 * tricky part is to synchronize the teardown of the application and
7095 * this notify object. Let's keep that in mind so we can avoid this
7096 * kind of shenanigans with ENOMEM in the teardown path.
7097 */
7098 err_enomem = 1;
7099 } else {
7100 obj->fd = sock;
7101 }
7102
7103 DBG("UST app notify socket unregister %d", sock);
7104
7105 /*
7106 * Lookup application by notify socket. If this fails, this means that the
7107 * hash table delete has already been done by the application
7108 * unregistration process so we can safely close the notify socket in a
7109 * call RCU.
7110 */
7111 app = find_app_by_notify_sock(sock);
7112 if (!app) {
7113 goto close_socket;
7114 }
7115
7116 iter.iter.node = &app->notify_sock_n.node;
7117
7118 /*
7119 * Whatever happens here either we fail or succeed, in both cases we have
7120 * to close the socket after a grace period to continue to the call RCU
7121 * here. If the deletion is successful, the application is not visible
7122 * anymore by other threads and is it fails it means that it was already
7123 * deleted from the hash table so either way we just have to close the
7124 * socket.
7125 */
7126 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
7127
7128 close_socket:
7129
7130 /*
7131 * Close socket after a grace period to avoid for the socket to be reused
7132 * before the application object is freed creating potential race between
7133 * threads trying to add unique in the global hash table.
7134 */
7135 if (!err_enomem) {
7136 call_rcu(&obj->head, close_notify_sock_rcu);
7137 }
7138 }
7139
7140 /*
7141 * Destroy a ust app data structure and free its memory.
7142 */
7143 void ust_app_destroy(struct ust_app *app)
7144 {
7145 if (!app) {
7146 return;
7147 }
7148
7149 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
7150 }
7151
7152 /*
7153 * Take a snapshot for a given UST session. The snapshot is sent to the given
7154 * output.
7155 *
7156 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
7157 */
7158 enum lttng_error_code ust_app_snapshot_record(const struct ltt_ust_session *usess,
7159 const struct consumer_output *output,
7160 uint64_t nb_packets_per_stream)
7161 {
7162 int ret = 0;
7163 enum lttng_error_code status = LTTNG_OK;
7164 struct lttng_ht_iter iter;
7165 struct ust_app *app;
7166 char *trace_path = nullptr;
7167
7168 LTTNG_ASSERT(usess);
7169 LTTNG_ASSERT(output);
7170
7171 switch (usess->buffer_type) {
7172 case LTTNG_BUFFER_PER_UID:
7173 {
7174 struct buffer_reg_uid *reg;
7175
7176 lttng::urcu::read_lock_guard read_lock;
7177
7178 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7179 struct buffer_reg_channel *buf_reg_chan;
7180 struct consumer_socket *socket;
7181 char pathname[PATH_MAX];
7182 size_t consumer_path_offset = 0;
7183
7184 if (!reg->registry->reg.ust->_metadata_key) {
7185 /* Skip since no metadata is present */
7186 continue;
7187 }
7188
7189 /* Get consumer socket to use to push the metadata.*/
7190 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7191 usess->consumer);
7192 if (!socket) {
7193 status = LTTNG_ERR_INVALID;
7194 goto error;
7195 }
7196
7197 memset(pathname, 0, sizeof(pathname));
7198 ret = snprintf(pathname,
7199 sizeof(pathname),
7200 DEFAULT_UST_TRACE_UID_PATH,
7201 reg->uid,
7202 reg->bits_per_long);
7203 if (ret < 0) {
7204 PERROR("snprintf snapshot path");
7205 status = LTTNG_ERR_INVALID;
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 /* Add the UST default trace dir to path. */
7217 cds_lfht_for_each_entry (
7218 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7219 status =
7220 consumer_snapshot_channel(socket,
7221 buf_reg_chan->consumer_key,
7222 output,
7223 0,
7224 &trace_path[consumer_path_offset],
7225 nb_packets_per_stream);
7226 if (status != LTTNG_OK) {
7227 goto error;
7228 }
7229 }
7230 status = consumer_snapshot_channel(socket,
7231 reg->registry->reg.ust->_metadata_key,
7232 output,
7233 1,
7234 &trace_path[consumer_path_offset],
7235 0);
7236 if (status != LTTNG_OK) {
7237 goto error;
7238 }
7239 }
7240
7241 break;
7242 }
7243 case LTTNG_BUFFER_PER_PID:
7244 {
7245 lttng::urcu::read_lock_guard read_lock;
7246
7247 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7248 struct consumer_socket *socket;
7249 struct lttng_ht_iter chan_iter;
7250 struct ust_app_channel *ua_chan;
7251 struct ust_app_session *ua_sess;
7252 lsu::registry_session *registry;
7253 char pathname[PATH_MAX];
7254 size_t consumer_path_offset = 0;
7255
7256 ua_sess = lookup_session_by_app(usess, app);
7257 if (!ua_sess) {
7258 /* Session not associated with this app. */
7259 continue;
7260 }
7261
7262 /* Get the right consumer socket for the application. */
7263 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, output);
7264 if (!socket) {
7265 status = LTTNG_ERR_INVALID;
7266 goto error;
7267 }
7268
7269 /* Add the UST default trace dir to path. */
7270 memset(pathname, 0, sizeof(pathname));
7271 ret = snprintf(pathname, sizeof(pathname), "%s", ua_sess->path);
7272 if (ret < 0) {
7273 status = LTTNG_ERR_INVALID;
7274 PERROR("snprintf snapshot path");
7275 goto error;
7276 }
7277 /* Free path allowed on previous iteration. */
7278 free(trace_path);
7279 trace_path = setup_channel_trace_path(
7280 usess->consumer, pathname, &consumer_path_offset);
7281 if (!trace_path) {
7282 status = LTTNG_ERR_INVALID;
7283 goto error;
7284 }
7285 cds_lfht_for_each_entry (
7286 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7287 status =
7288 consumer_snapshot_channel(socket,
7289 ua_chan->key,
7290 output,
7291 0,
7292 &trace_path[consumer_path_offset],
7293 nb_packets_per_stream);
7294 switch (status) {
7295 case LTTNG_OK:
7296 break;
7297 case LTTNG_ERR_CHAN_NOT_FOUND:
7298 continue;
7299 default:
7300 goto error;
7301 }
7302 }
7303
7304 registry = get_session_registry(ua_sess);
7305 if (!registry) {
7306 DBG("Application session is being torn down. Skip application.");
7307 continue;
7308 }
7309 status = consumer_snapshot_channel(socket,
7310 registry->_metadata_key,
7311 output,
7312 1,
7313 &trace_path[consumer_path_offset],
7314 0);
7315 switch (status) {
7316 case LTTNG_OK:
7317 break;
7318 case LTTNG_ERR_CHAN_NOT_FOUND:
7319 continue;
7320 default:
7321 goto error;
7322 }
7323 }
7324 break;
7325 }
7326 default:
7327 abort();
7328 break;
7329 }
7330
7331 error:
7332 free(trace_path);
7333 return status;
7334 }
7335
7336 /*
7337 * Return the size taken by one more packet per stream.
7338 */
7339 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess,
7340 uint64_t cur_nr_packets)
7341 {
7342 uint64_t tot_size = 0;
7343 struct ust_app *app;
7344 struct lttng_ht_iter iter;
7345
7346 LTTNG_ASSERT(usess);
7347
7348 switch (usess->buffer_type) {
7349 case LTTNG_BUFFER_PER_UID:
7350 {
7351 struct buffer_reg_uid *reg;
7352
7353 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7354 struct buffer_reg_channel *buf_reg_chan;
7355
7356 lttng::urcu::read_lock_guard read_lock;
7357
7358 cds_lfht_for_each_entry (
7359 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7360 if (cur_nr_packets >= buf_reg_chan->num_subbuf) {
7361 /*
7362 * Don't take channel into account if we
7363 * already grab all its packets.
7364 */
7365 continue;
7366 }
7367 tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count;
7368 }
7369 }
7370 break;
7371 }
7372 case LTTNG_BUFFER_PER_PID:
7373 {
7374 lttng::urcu::read_lock_guard read_lock;
7375
7376 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7377 struct ust_app_channel *ua_chan;
7378 struct ust_app_session *ua_sess;
7379 struct lttng_ht_iter chan_iter;
7380
7381 ua_sess = lookup_session_by_app(usess, app);
7382 if (!ua_sess) {
7383 /* Session not associated with this app. */
7384 continue;
7385 }
7386
7387 cds_lfht_for_each_entry (
7388 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7389 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
7390 /*
7391 * Don't take channel into account if we
7392 * already grab all its packets.
7393 */
7394 continue;
7395 }
7396 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
7397 }
7398 }
7399 break;
7400 }
7401 default:
7402 abort();
7403 break;
7404 }
7405
7406 return tot_size;
7407 }
7408
7409 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
7410 struct cds_list_head *buffer_reg_uid_list,
7411 struct consumer_output *consumer,
7412 uint64_t uchan_id,
7413 int overwrite,
7414 uint64_t *discarded,
7415 uint64_t *lost)
7416 {
7417 int ret;
7418 uint64_t consumer_chan_key;
7419
7420 *discarded = 0;
7421 *lost = 0;
7422
7423 ret = buffer_reg_uid_consumer_channel_key(
7424 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
7425 if (ret < 0) {
7426 /* Not found */
7427 ret = 0;
7428 goto end;
7429 }
7430
7431 if (overwrite) {
7432 ret = consumer_get_lost_packets(ust_session_id, consumer_chan_key, consumer, lost);
7433 } else {
7434 ret = consumer_get_discarded_events(
7435 ust_session_id, consumer_chan_key, consumer, discarded);
7436 }
7437
7438 end:
7439 return ret;
7440 }
7441
7442 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
7443 struct ltt_ust_channel *uchan,
7444 struct consumer_output *consumer,
7445 int overwrite,
7446 uint64_t *discarded,
7447 uint64_t *lost)
7448 {
7449 int ret = 0;
7450 struct lttng_ht_iter iter;
7451 struct lttng_ht_node_str *ua_chan_node;
7452 struct ust_app *app;
7453 struct ust_app_session *ua_sess;
7454 struct ust_app_channel *ua_chan;
7455
7456 *discarded = 0;
7457 *lost = 0;
7458
7459 /*
7460 * Iterate over every registered applications. Sum counters for
7461 * all applications containing requested session and channel.
7462 */
7463 lttng::urcu::read_lock_guard read_lock;
7464
7465 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7466 struct lttng_ht_iter uiter;
7467
7468 ua_sess = lookup_session_by_app(usess, app);
7469 if (ua_sess == nullptr) {
7470 continue;
7471 }
7472
7473 /* Get channel */
7474 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
7475 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
7476 /* If the session is found for the app, the channel must be there */
7477 LTTNG_ASSERT(ua_chan_node);
7478
7479 ua_chan = lttng::utils::container_of(ua_chan_node, &ust_app_channel::node);
7480
7481 if (overwrite) {
7482 uint64_t _lost;
7483
7484 ret = consumer_get_lost_packets(usess->id, ua_chan->key, consumer, &_lost);
7485 if (ret < 0) {
7486 break;
7487 }
7488 (*lost) += _lost;
7489 } else {
7490 uint64_t _discarded;
7491
7492 ret = consumer_get_discarded_events(
7493 usess->id, ua_chan->key, consumer, &_discarded);
7494 if (ret < 0) {
7495 break;
7496 }
7497 (*discarded) += _discarded;
7498 }
7499 }
7500
7501 return ret;
7502 }
7503
7504 static int ust_app_regenerate_statedump(struct ltt_ust_session *usess, struct ust_app *app)
7505 {
7506 int ret = 0;
7507 struct ust_app_session *ua_sess;
7508
7509 DBG("Regenerating the metadata for ust app pid %d", app->pid);
7510
7511 lttng::urcu::read_lock_guard read_lock;
7512
7513 ua_sess = lookup_session_by_app(usess, app);
7514 if (ua_sess == nullptr) {
7515 /* The session is in teardown process. Ignore and continue. */
7516 goto end;
7517 }
7518
7519 pthread_mutex_lock(&ua_sess->lock);
7520
7521 if (ua_sess->deleted) {
7522 goto end_unlock;
7523 }
7524
7525 pthread_mutex_lock(&app->sock_lock);
7526 ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle);
7527 pthread_mutex_unlock(&app->sock_lock);
7528
7529 end_unlock:
7530 pthread_mutex_unlock(&ua_sess->lock);
7531
7532 end:
7533 health_code_update();
7534 return ret;
7535 }
7536
7537 /*
7538 * Regenerate the statedump for each app in the session.
7539 */
7540 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
7541 {
7542 int ret = 0;
7543 struct lttng_ht_iter iter;
7544 struct ust_app *app;
7545
7546 DBG("Regenerating the metadata for all UST apps");
7547
7548 lttng::urcu::read_lock_guard read_lock;
7549
7550 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7551 if (!app->compatible) {
7552 continue;
7553 }
7554
7555 ret = ust_app_regenerate_statedump(usess, app);
7556 if (ret < 0) {
7557 /* Continue to the next app even on error */
7558 continue;
7559 }
7560 }
7561
7562 return 0;
7563 }
7564
7565 /*
7566 * Rotate all the channels of a session.
7567 *
7568 * Return LTTNG_OK on success or else an LTTng error code.
7569 */
7570 enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
7571 {
7572 int ret;
7573 enum lttng_error_code cmd_ret = LTTNG_OK;
7574 struct lttng_ht_iter iter;
7575 struct ust_app *app;
7576 struct ltt_ust_session *usess = session->ust_session;
7577
7578 LTTNG_ASSERT(usess);
7579
7580 switch (usess->buffer_type) {
7581 case LTTNG_BUFFER_PER_UID:
7582 {
7583 struct buffer_reg_uid *reg;
7584
7585 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7586 struct buffer_reg_channel *buf_reg_chan;
7587 struct consumer_socket *socket;
7588 lttng::urcu::read_lock_guard read_lock;
7589
7590 /* Get consumer socket to use to push the metadata.*/
7591 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7592 usess->consumer);
7593 if (!socket) {
7594 cmd_ret = LTTNG_ERR_INVALID;
7595 goto error;
7596 }
7597
7598 /* Rotate the data channels. */
7599 cds_lfht_for_each_entry (
7600 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7601 ret = consumer_rotate_channel(socket,
7602 buf_reg_chan->consumer_key,
7603 usess->consumer,
7604 /* is_metadata_channel */ false);
7605 if (ret < 0) {
7606 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7607 goto error;
7608 }
7609 }
7610
7611 /*
7612 * The metadata channel might not be present.
7613 *
7614 * Consumer stream allocation can be done
7615 * asynchronously and can fail on intermediary
7616 * operations (i.e add context) and lead to data
7617 * channels created with no metadata channel.
7618 */
7619 if (!reg->registry->reg.ust->_metadata_key) {
7620 /* Skip since no metadata is present. */
7621 continue;
7622 }
7623
7624 {
7625 auto locked_registry = reg->registry->reg.ust->lock();
7626 (void) push_metadata(locked_registry, usess->consumer);
7627 }
7628
7629 ret = consumer_rotate_channel(socket,
7630 reg->registry->reg.ust->_metadata_key,
7631 usess->consumer,
7632 /* is_metadata_channel */ true);
7633 if (ret < 0) {
7634 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7635 goto error;
7636 }
7637 }
7638 break;
7639 }
7640 case LTTNG_BUFFER_PER_PID:
7641 {
7642 lttng::urcu::read_lock_guard read_lock;
7643
7644 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7645 struct consumer_socket *socket;
7646 struct lttng_ht_iter chan_iter;
7647 struct ust_app_channel *ua_chan;
7648 struct ust_app_session *ua_sess;
7649 lsu::registry_session *registry;
7650
7651 ua_sess = lookup_session_by_app(usess, app);
7652 if (!ua_sess) {
7653 /* Session not associated with this app. */
7654 continue;
7655 }
7656
7657 /* Get the right consumer socket for the application. */
7658 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7659 usess->consumer);
7660 if (!socket) {
7661 cmd_ret = LTTNG_ERR_INVALID;
7662 goto error;
7663 }
7664
7665 registry = get_session_registry(ua_sess);
7666 if (!registry) {
7667 DBG("Application session is being torn down. Skip application.");
7668 continue;
7669 }
7670
7671 /* Rotate the data channels. */
7672 cds_lfht_for_each_entry (
7673 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7674 ret = consumer_rotate_channel(socket,
7675 ua_chan->key,
7676 ua_sess->consumer,
7677 /* is_metadata_channel */ false);
7678 if (ret < 0) {
7679 /* Per-PID buffer and application going away. */
7680 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7681 continue;
7682 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7683 goto error;
7684 }
7685 }
7686
7687 /* Rotate the metadata channel. */
7688 {
7689 auto locked_registry = registry->lock();
7690
7691 (void) push_metadata(locked_registry, usess->consumer);
7692 }
7693 ret = consumer_rotate_channel(socket,
7694 registry->_metadata_key,
7695 ua_sess->consumer,
7696 /* is_metadata_channel */ true);
7697 if (ret < 0) {
7698 /* Per-PID buffer and application going away. */
7699 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7700 continue;
7701 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7702 goto error;
7703 }
7704 }
7705 break;
7706 }
7707 default:
7708 abort();
7709 break;
7710 }
7711
7712 cmd_ret = LTTNG_OK;
7713
7714 error:
7715 return cmd_ret;
7716 }
7717
7718 enum lttng_error_code ust_app_create_channel_subdirectories(const struct ltt_ust_session *usess)
7719 {
7720 enum lttng_error_code ret = LTTNG_OK;
7721 struct lttng_ht_iter iter;
7722 enum lttng_trace_chunk_status chunk_status;
7723 char *pathname_index;
7724 int fmt_ret;
7725
7726 LTTNG_ASSERT(usess->current_trace_chunk);
7727
7728 switch (usess->buffer_type) {
7729 case LTTNG_BUFFER_PER_UID:
7730 {
7731 struct buffer_reg_uid *reg;
7732 lttng::urcu::read_lock_guard read_lock;
7733
7734 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7735 fmt_ret = asprintf(&pathname_index,
7736 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH
7737 "/" DEFAULT_INDEX_DIR,
7738 reg->uid,
7739 reg->bits_per_long);
7740 if (fmt_ret < 0) {
7741 ERR("Failed to format channel index directory");
7742 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7743 goto error;
7744 }
7745
7746 /*
7747 * Create the index subdirectory which will take care
7748 * of implicitly creating the channel's path.
7749 */
7750 chunk_status = lttng_trace_chunk_create_subdirectory(
7751 usess->current_trace_chunk, pathname_index);
7752 free(pathname_index);
7753 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7754 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7755 goto error;
7756 }
7757 }
7758 break;
7759 }
7760 case LTTNG_BUFFER_PER_PID:
7761 {
7762 struct ust_app *app;
7763 lttng::urcu::read_lock_guard read_lock;
7764
7765 /*
7766 * Create the toplevel ust/ directory in case no apps are running.
7767 */
7768 chunk_status = lttng_trace_chunk_create_subdirectory(usess->current_trace_chunk,
7769 DEFAULT_UST_TRACE_DIR);
7770 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7771 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7772 goto error;
7773 }
7774
7775 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7776 struct ust_app_session *ua_sess;
7777 lsu::registry_session *registry;
7778
7779 ua_sess = lookup_session_by_app(usess, app);
7780 if (!ua_sess) {
7781 /* Session not associated with this app. */
7782 continue;
7783 }
7784
7785 registry = get_session_registry(ua_sess);
7786 if (!registry) {
7787 DBG("Application session is being torn down. Skip application.");
7788 continue;
7789 }
7790
7791 fmt_ret = asprintf(&pathname_index,
7792 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
7793 ua_sess->path);
7794 if (fmt_ret < 0) {
7795 ERR("Failed to format channel index directory");
7796 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7797 goto error;
7798 }
7799 /*
7800 * Create the index subdirectory which will take care
7801 * of implicitly creating the channel's path.
7802 */
7803 chunk_status = lttng_trace_chunk_create_subdirectory(
7804 usess->current_trace_chunk, pathname_index);
7805 free(pathname_index);
7806 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7807 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7808 goto error;
7809 }
7810 }
7811 break;
7812 }
7813 default:
7814 abort();
7815 }
7816
7817 ret = LTTNG_OK;
7818 error:
7819 return ret;
7820 }
7821
7822 /*
7823 * Clear all the channels of a session.
7824 *
7825 * Return LTTNG_OK on success or else an LTTng error code.
7826 */
7827 enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
7828 {
7829 int ret;
7830 enum lttng_error_code cmd_ret = LTTNG_OK;
7831 struct lttng_ht_iter iter;
7832 struct ust_app *app;
7833 struct ltt_ust_session *usess = session->ust_session;
7834
7835 LTTNG_ASSERT(usess);
7836
7837 if (usess->active) {
7838 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
7839 cmd_ret = LTTNG_ERR_FATAL;
7840 goto end;
7841 }
7842
7843 switch (usess->buffer_type) {
7844 case LTTNG_BUFFER_PER_UID:
7845 {
7846 struct buffer_reg_uid *reg;
7847 lttng::urcu::read_lock_guard read_lock;
7848
7849 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
7850 struct buffer_reg_channel *buf_reg_chan;
7851 struct consumer_socket *socket;
7852
7853 /* Get consumer socket to use to push the metadata.*/
7854 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7855 usess->consumer);
7856 if (!socket) {
7857 cmd_ret = LTTNG_ERR_INVALID;
7858 goto error_socket;
7859 }
7860
7861 /* Clear the data channels. */
7862 cds_lfht_for_each_entry (
7863 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
7864 ret = consumer_clear_channel(socket, buf_reg_chan->consumer_key);
7865 if (ret < 0) {
7866 goto error;
7867 }
7868 }
7869
7870 {
7871 auto locked_registry = reg->registry->reg.ust->lock();
7872 (void) push_metadata(locked_registry, usess->consumer);
7873 }
7874
7875 /*
7876 * Clear the metadata channel.
7877 * Metadata channel is not cleared per se but we still need to
7878 * perform a rotation operation on it behind the scene.
7879 */
7880 ret = consumer_clear_channel(socket, reg->registry->reg.ust->_metadata_key);
7881 if (ret < 0) {
7882 goto error;
7883 }
7884 }
7885 break;
7886 }
7887 case LTTNG_BUFFER_PER_PID:
7888 {
7889 lttng::urcu::read_lock_guard read_lock;
7890
7891 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7892 struct consumer_socket *socket;
7893 struct lttng_ht_iter chan_iter;
7894 struct ust_app_channel *ua_chan;
7895 struct ust_app_session *ua_sess;
7896 lsu::registry_session *registry;
7897
7898 ua_sess = lookup_session_by_app(usess, app);
7899 if (!ua_sess) {
7900 /* Session not associated with this app. */
7901 continue;
7902 }
7903
7904 /* Get the right consumer socket for the application. */
7905 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7906 usess->consumer);
7907 if (!socket) {
7908 cmd_ret = LTTNG_ERR_INVALID;
7909 goto error_socket;
7910 }
7911
7912 registry = get_session_registry(ua_sess);
7913 if (!registry) {
7914 DBG("Application session is being torn down. Skip application.");
7915 continue;
7916 }
7917
7918 /* Clear the data channels. */
7919 cds_lfht_for_each_entry (
7920 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
7921 ret = consumer_clear_channel(socket, ua_chan->key);
7922 if (ret < 0) {
7923 /* Per-PID buffer and application going away. */
7924 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7925 continue;
7926 }
7927 goto error;
7928 }
7929 }
7930
7931 {
7932 auto locked_registry = registry->lock();
7933 (void) push_metadata(locked_registry, usess->consumer);
7934 }
7935
7936 /*
7937 * Clear the metadata channel.
7938 * Metadata channel is not cleared per se but we still need to
7939 * perform rotation operation on it behind the scene.
7940 */
7941 ret = consumer_clear_channel(socket, registry->_metadata_key);
7942 if (ret < 0) {
7943 /* Per-PID buffer and application going away. */
7944 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7945 continue;
7946 }
7947 goto error;
7948 }
7949 }
7950 break;
7951 }
7952 default:
7953 abort();
7954 break;
7955 }
7956
7957 cmd_ret = LTTNG_OK;
7958 goto end;
7959
7960 error:
7961 switch (-ret) {
7962 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
7963 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
7964 break;
7965 default:
7966 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
7967 }
7968
7969 error_socket:
7970 end:
7971 return cmd_ret;
7972 }
7973
7974 /*
7975 * This function skips the metadata channel as the begin/end timestamps of a
7976 * metadata packet are useless.
7977 *
7978 * Moreover, opening a packet after a "clear" will cause problems for live
7979 * sessions as it will introduce padding that was not part of the first trace
7980 * chunk. The relay daemon expects the content of the metadata stream of
7981 * successive metadata trace chunks to be strict supersets of one another.
7982 *
7983 * For example, flushing a packet at the beginning of the metadata stream of
7984 * a trace chunk resulting from a "clear" session command will cause the
7985 * size of the metadata stream of the new trace chunk to not match the size of
7986 * the metadata stream of the original chunk. This will confuse the relay
7987 * daemon as the same "offset" in a metadata stream will no longer point
7988 * to the same content.
7989 */
7990 enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
7991 {
7992 enum lttng_error_code ret = LTTNG_OK;
7993 struct lttng_ht_iter iter;
7994 struct ltt_ust_session *usess = session->ust_session;
7995
7996 LTTNG_ASSERT(usess);
7997
7998 switch (usess->buffer_type) {
7999 case LTTNG_BUFFER_PER_UID:
8000 {
8001 struct buffer_reg_uid *reg;
8002
8003 cds_list_for_each_entry (reg, &usess->buffer_reg_uid_list, lnode) {
8004 struct buffer_reg_channel *buf_reg_chan;
8005 struct consumer_socket *socket;
8006 lttng::urcu::read_lock_guard read_lock;
8007
8008 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
8009 usess->consumer);
8010 if (!socket) {
8011 ret = LTTNG_ERR_FATAL;
8012 goto error;
8013 }
8014
8015 cds_lfht_for_each_entry (
8016 reg->registry->channels->ht, &iter.iter, buf_reg_chan, node.node) {
8017 const int open_ret = consumer_open_channel_packets(
8018 socket, buf_reg_chan->consumer_key);
8019
8020 if (open_ret < 0) {
8021 ret = LTTNG_ERR_UNK;
8022 goto error;
8023 }
8024 }
8025 }
8026 break;
8027 }
8028 case LTTNG_BUFFER_PER_PID:
8029 {
8030 struct ust_app *app;
8031 lttng::urcu::read_lock_guard read_lock;
8032
8033 cds_lfht_for_each_entry (ust_app_ht->ht, &iter.iter, app, pid_n.node) {
8034 struct consumer_socket *socket;
8035 struct lttng_ht_iter chan_iter;
8036 struct ust_app_channel *ua_chan;
8037 struct ust_app_session *ua_sess;
8038 lsu::registry_session *registry;
8039
8040 ua_sess = lookup_session_by_app(usess, app);
8041 if (!ua_sess) {
8042 /* Session not associated with this app. */
8043 continue;
8044 }
8045
8046 /* Get the right consumer socket for the application. */
8047 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
8048 usess->consumer);
8049 if (!socket) {
8050 ret = LTTNG_ERR_FATAL;
8051 goto error;
8052 }
8053
8054 registry = get_session_registry(ua_sess);
8055 if (!registry) {
8056 DBG("Application session is being torn down. Skip application.");
8057 continue;
8058 }
8059
8060 cds_lfht_for_each_entry (
8061 ua_sess->channels->ht, &chan_iter.iter, ua_chan, node.node) {
8062 const int open_ret =
8063 consumer_open_channel_packets(socket, ua_chan->key);
8064
8065 if (open_ret < 0) {
8066 /*
8067 * Per-PID buffer and application going
8068 * away.
8069 */
8070 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
8071 continue;
8072 }
8073
8074 ret = LTTNG_ERR_UNK;
8075 goto error;
8076 }
8077 }
8078 }
8079 break;
8080 }
8081 default:
8082 abort();
8083 break;
8084 }
8085
8086 error:
8087 return ret;
8088 }
8089
8090 lsu::ctl_field_quirks ust_app::ctl_field_quirks() const
8091 {
8092 /*
8093 * Application contexts are expressed as variants. LTTng-UST announces
8094 * those by registering an enumeration named `..._tag`. It then registers a
8095 * variant as part of the event context that contains the various possible
8096 * types.
8097 *
8098 * Unfortunately, the names used in the enumeration and variant don't
8099 * match: the enumeration names are all prefixed with an underscore while
8100 * the variant type tag fields aren't.
8101 *
8102 * While the CTF 1.8.3 specification mentions that
8103 * underscores *should* (not *must*) be removed by CTF readers. Babeltrace
8104 * 1.x (and possibly others) expect a perfect match between the names used
8105 * by tags and variants.
8106 *
8107 * When the UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS quirk is enabled,
8108 * the variant's fields are modified to match the mappings of its tag.
8109 *
8110 * From ABI version >= 10.x, the variant fields and tag mapping names
8111 * correctly match, making this quirk unnecessary.
8112 */
8113 return v_major <= 9 ? lsu::ctl_field_quirks::UNDERSCORE_PREFIXED_VARIANT_TAG_MAPPINGS :
8114 lsu::ctl_field_quirks::NONE;
8115 }
This page took 0.222723 seconds and 5 git commands to generate.