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