Clean-up: sessiond: move registry_session free functions under class
[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 caa_container_of(head, struct 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 = caa_container_of(
384 head, struct 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 caa_container_of(head, struct 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 caa_container_of(head, struct 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 caa_container_of(head, struct lttng_ht_node_ulong, head);
1130 struct ust_app *app =
1131 caa_container_of(node, struct 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 caa_container_of(node, struct 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 caa_container_of(node, struct 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 = caa_container_of(node, struct 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 = caa_container_of(
1569 node, struct 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 caa_container_of(node, struct 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 = caa_container_of(node, struct 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 = caa_container_of(ua_chan_node, struct 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->name,
3465 lttng_credentials_get_uid(
3466 &ua_sess->effective_credentials),
3467 lttng_credentials_get_gid(
3468 &ua_sess->effective_credentials),
3469 ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST,
3470 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3471 if (notification_ret != LTTNG_OK) {
3472 ret = - (int) notification_ret;
3473 ERR("Failed to add channel to notification thread");
3474 goto error;
3475 }
3476
3477 send_channel:
3478 /* Send buffers to the application. */
3479 ret = send_channel_uid_to_ust(buf_reg_chan, app, ua_sess, ua_chan);
3480 if (ret < 0) {
3481 if (ret != -ENOTCONN) {
3482 ERR("Error sending channel to application");
3483 }
3484 goto error;
3485 }
3486
3487 error:
3488 if (session) {
3489 session_put(session);
3490 }
3491 return ret;
3492 }
3493
3494 /*
3495 * Create and send to the application the created buffers with per PID buffers.
3496 *
3497 * Called with UST app session lock held.
3498 * The session list lock and the session's lock must be acquired.
3499 *
3500 * Return 0 on success else a negative value.
3501 */
3502 static int create_channel_per_pid(struct ust_app *app,
3503 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3504 struct ust_app_channel *ua_chan)
3505 {
3506 int ret;
3507 lsu::registry_session *registry;
3508 enum lttng_error_code cmd_ret;
3509 struct ltt_session *session = NULL;
3510 uint64_t chan_reg_key;
3511
3512 LTTNG_ASSERT(app);
3513 LTTNG_ASSERT(usess);
3514 LTTNG_ASSERT(ua_sess);
3515 LTTNG_ASSERT(ua_chan);
3516
3517 DBG("UST app creating channel %s with per PID buffers", ua_chan->name);
3518
3519 rcu_read_lock();
3520
3521 registry = get_session_registry(ua_sess);
3522 /* The UST app session lock is held, registry shall not be null. */
3523 LTTNG_ASSERT(registry);
3524
3525 /* Create and add a new channel registry to session. */
3526 try {
3527 registry->add_channel(ua_chan->key);
3528 } catch (const std::exception& ex) {
3529 ERR("Error creating the UST channel \"%s\" registry instance: %s", ua_chan->name,
3530 ex.what());
3531 ret = -1;
3532 goto error;
3533 }
3534
3535 session = session_find_by_id(ua_sess->tracing_id);
3536 LTTNG_ASSERT(session);
3537 ASSERT_LOCKED(session->lock);
3538 ASSERT_SESSION_LIST_LOCKED();
3539
3540 /* Create and get channel on the consumer side. */
3541 ret = do_consumer_create_channel(usess, ua_sess, ua_chan,
3542 app->abi.bits_per_long, registry);
3543 if (ret < 0) {
3544 ERR("Error creating UST channel \"%s\" on the consumer daemon",
3545 ua_chan->name);
3546 goto error_remove_from_registry;
3547 }
3548
3549 ret = send_channel_pid_to_ust(app, ua_sess, ua_chan);
3550 if (ret < 0) {
3551 if (ret != -ENOTCONN) {
3552 ERR("Error sending channel to application");
3553 }
3554 goto error_remove_from_registry;
3555 }
3556
3557 chan_reg_key = ua_chan->key;
3558 {
3559 auto locked_registry = registry->lock();
3560
3561 auto& ust_reg_chan = locked_registry->get_channel(chan_reg_key);
3562 ust_reg_chan._consumer_key = ua_chan->key;
3563 }
3564
3565 cmd_ret = notification_thread_command_add_channel(
3566 the_notification_thread_handle, session->name,
3567 lttng_credentials_get_uid(
3568 &ua_sess->effective_credentials),
3569 lttng_credentials_get_gid(
3570 &ua_sess->effective_credentials),
3571 ua_chan->name, ua_chan->key, LTTNG_DOMAIN_UST,
3572 ua_chan->attr.subbuf_size * ua_chan->attr.num_subbuf);
3573 if (cmd_ret != LTTNG_OK) {
3574 ret = - (int) cmd_ret;
3575 ERR("Failed to add channel to notification thread");
3576 goto error_remove_from_registry;
3577 }
3578
3579 error_remove_from_registry:
3580 if (ret) {
3581 try {
3582 auto locked_registry = registry->lock();
3583 locked_registry->remove_channel(ua_chan->key, false);
3584 } catch (const std::exception& ex) {
3585 DBG("Could not find channel for removal: %s", ex.what());
3586 }
3587 }
3588 error:
3589 rcu_read_unlock();
3590 if (session) {
3591 session_put(session);
3592 }
3593 return ret;
3594 }
3595
3596 /*
3597 * From an already allocated ust app channel, create the channel buffers if
3598 * needed and send them to the application. This MUST be called with a RCU read
3599 * side lock acquired.
3600 *
3601 * Called with UST app session lock held.
3602 *
3603 * Return 0 on success or else a negative value. Returns -ENOTCONN if
3604 * the application exited concurrently.
3605 */
3606 static int ust_app_channel_send(struct ust_app *app,
3607 struct ltt_ust_session *usess, struct ust_app_session *ua_sess,
3608 struct ust_app_channel *ua_chan)
3609 {
3610 int ret;
3611
3612 LTTNG_ASSERT(app);
3613 LTTNG_ASSERT(usess);
3614 LTTNG_ASSERT(usess->active);
3615 LTTNG_ASSERT(ua_sess);
3616 LTTNG_ASSERT(ua_chan);
3617 ASSERT_RCU_READ_LOCKED();
3618
3619 /* Handle buffer type before sending the channel to the application. */
3620 switch (usess->buffer_type) {
3621 case LTTNG_BUFFER_PER_UID:
3622 {
3623 ret = create_channel_per_uid(app, usess, ua_sess, ua_chan);
3624 if (ret < 0) {
3625 goto error;
3626 }
3627 break;
3628 }
3629 case LTTNG_BUFFER_PER_PID:
3630 {
3631 ret = create_channel_per_pid(app, usess, ua_sess, ua_chan);
3632 if (ret < 0) {
3633 goto error;
3634 }
3635 break;
3636 }
3637 default:
3638 abort();
3639 ret = -EINVAL;
3640 goto error;
3641 }
3642
3643 /* Initialize ust objd object using the received handle and add it. */
3644 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
3645 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
3646
3647 /* If channel is not enabled, disable it on the tracer */
3648 if (!ua_chan->enabled) {
3649 ret = disable_ust_channel(app, ua_sess, ua_chan);
3650 if (ret < 0) {
3651 goto error;
3652 }
3653 }
3654
3655 error:
3656 return ret;
3657 }
3658
3659 /*
3660 * Create UST app channel and return it through ua_chanp if not NULL.
3661 *
3662 * Called with UST app session lock and RCU read-side lock held.
3663 *
3664 * Return 0 on success or else a negative value.
3665 */
3666 static int ust_app_channel_allocate(struct ust_app_session *ua_sess,
3667 struct ltt_ust_channel *uchan,
3668 enum lttng_ust_abi_chan_type type,
3669 struct ltt_ust_session *usess __attribute__((unused)),
3670 struct ust_app_channel **ua_chanp)
3671 {
3672 int ret = 0;
3673 struct lttng_ht_iter iter;
3674 struct lttng_ht_node_str *ua_chan_node;
3675 struct ust_app_channel *ua_chan;
3676
3677 ASSERT_RCU_READ_LOCKED();
3678
3679 /* Lookup channel in the ust app session */
3680 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3681 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3682 if (ua_chan_node != NULL) {
3683 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3684 goto end;
3685 }
3686
3687 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
3688 if (ua_chan == NULL) {
3689 /* Only malloc can fail here */
3690 ret = -ENOMEM;
3691 goto error;
3692 }
3693 shadow_copy_channel(ua_chan, uchan);
3694
3695 /* Set channel type. */
3696 ua_chan->attr.type = type;
3697
3698 /* Only add the channel if successful on the tracer side. */
3699 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
3700 end:
3701 if (ua_chanp) {
3702 *ua_chanp = ua_chan;
3703 }
3704
3705 /* Everything went well. */
3706 return 0;
3707
3708 error:
3709 return ret;
3710 }
3711
3712 /*
3713 * Create UST app event and create it on the tracer side.
3714 *
3715 * Must be called with the RCU read side lock held.
3716 * Called with ust app session mutex held.
3717 */
3718 static
3719 int create_ust_app_event(struct ust_app_channel *ua_chan,
3720 struct ltt_ust_event *uevent,
3721 struct ust_app *app)
3722 {
3723 int ret = 0;
3724 struct ust_app_event *ua_event;
3725
3726 ASSERT_RCU_READ_LOCKED();
3727
3728 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
3729 if (ua_event == NULL) {
3730 /* Only failure mode of alloc_ust_app_event(). */
3731 ret = -ENOMEM;
3732 goto end;
3733 }
3734 shadow_copy_event(ua_event, uevent);
3735
3736 /* Create it on the tracer side */
3737 ret = create_ust_event(app, ua_chan, ua_event);
3738 if (ret < 0) {
3739 /*
3740 * Not found previously means that it does not exist on the
3741 * tracer. If the application reports that the event existed,
3742 * it means there is a bug in the sessiond or lttng-ust
3743 * (or corruption, etc.)
3744 */
3745 if (ret == -LTTNG_UST_ERR_EXIST) {
3746 ERR("Tracer for application reported that an event being created already existed: "
3747 "event_name = \"%s\", pid = %d, ppid = %d, uid = %d, gid = %d",
3748 uevent->attr.name,
3749 app->pid, app->ppid, app->uid,
3750 app->gid);
3751 }
3752 goto error;
3753 }
3754
3755 add_unique_ust_app_event(ua_chan, ua_event);
3756
3757 DBG2("UST app create event completed: app = '%s' pid = %d",
3758 app->name, app->pid);
3759
3760 end:
3761 return ret;
3762
3763 error:
3764 /* Valid. Calling here is already in a read side lock */
3765 delete_ust_app_event(-1, ua_event, app);
3766 return ret;
3767 }
3768
3769 /*
3770 * Create UST app event notifier rule and create it on the tracer side.
3771 *
3772 * Must be called with the RCU read side lock held.
3773 * Called with ust app session mutex held.
3774 */
3775 static
3776 int create_ust_app_event_notifier_rule(struct lttng_trigger *trigger,
3777 struct ust_app *app)
3778 {
3779 int ret = 0;
3780 struct ust_app_event_notifier_rule *ua_event_notifier_rule;
3781
3782 ASSERT_RCU_READ_LOCKED();
3783
3784 ua_event_notifier_rule = alloc_ust_app_event_notifier_rule(trigger);
3785 if (ua_event_notifier_rule == NULL) {
3786 ret = -ENOMEM;
3787 goto end;
3788 }
3789
3790 /* Create it on the tracer side. */
3791 ret = create_ust_event_notifier(app, ua_event_notifier_rule);
3792 if (ret < 0) {
3793 /*
3794 * Not found previously means that it does not exist on the
3795 * tracer. If the application reports that the event existed,
3796 * it means there is a bug in the sessiond or lttng-ust
3797 * (or corruption, etc.)
3798 */
3799 if (ret == -LTTNG_UST_ERR_EXIST) {
3800 ERR("Tracer for application reported that an event notifier being created already exists: "
3801 "token = \"%" PRIu64 "\", pid = %d, ppid = %d, uid = %d, gid = %d",
3802 lttng_trigger_get_tracer_token(trigger),
3803 app->pid, app->ppid, app->uid,
3804 app->gid);
3805 }
3806 goto error;
3807 }
3808
3809 lttng_ht_add_unique_u64(app->token_to_event_notifier_rule_ht,
3810 &ua_event_notifier_rule->node);
3811
3812 DBG2("UST app create token event rule completed: app = '%s', pid = %d, token = %" PRIu64,
3813 app->name, app->pid, lttng_trigger_get_tracer_token(trigger));
3814
3815 goto end;
3816
3817 error:
3818 /* The RCU read side lock is already being held by the caller. */
3819 delete_ust_app_event_notifier_rule(-1, ua_event_notifier_rule, app);
3820 end:
3821 return ret;
3822 }
3823
3824 /*
3825 * Create UST metadata and open it on the tracer side.
3826 *
3827 * Called with UST app session lock held and RCU read side lock.
3828 */
3829 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
3830 struct ust_app *app, struct consumer_output *consumer)
3831 {
3832 int ret = 0;
3833 struct ust_app_channel *metadata;
3834 struct consumer_socket *socket;
3835 struct ltt_session *session = NULL;
3836
3837 LTTNG_ASSERT(ua_sess);
3838 LTTNG_ASSERT(app);
3839 LTTNG_ASSERT(consumer);
3840 ASSERT_RCU_READ_LOCKED();
3841
3842 auto locked_registry = get_locked_session_registry(ua_sess);
3843 /* The UST app session is held registry shall not be null. */
3844 LTTNG_ASSERT(locked_registry);
3845
3846 /* Metadata already exists for this registry or it was closed previously */
3847 if (locked_registry->_metadata_key || locked_registry->_metadata_closed) {
3848 ret = 0;
3849 goto error;
3850 }
3851
3852 /* Allocate UST metadata */
3853 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
3854 if (!metadata) {
3855 /* malloc() failed */
3856 ret = -ENOMEM;
3857 goto error;
3858 }
3859
3860 memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr));
3861
3862 /* Need one fd for the channel. */
3863 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
3864 if (ret < 0) {
3865 ERR("Exhausted number of available FD upon create metadata");
3866 goto error;
3867 }
3868
3869 /* Get the right consumer socket for the application. */
3870 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long, consumer);
3871 if (!socket) {
3872 ret = -EINVAL;
3873 goto error_consumer;
3874 }
3875
3876 /*
3877 * Keep metadata key so we can identify it on the consumer side. Assign it
3878 * to the registry *before* we ask the consumer so we avoid the race of the
3879 * consumer requesting the metadata and the ask_channel call on our side
3880 * did not returned yet.
3881 */
3882 locked_registry->_metadata_key = metadata->key;
3883
3884 session = session_find_by_id(ua_sess->tracing_id);
3885 LTTNG_ASSERT(session);
3886 ASSERT_LOCKED(session->lock);
3887 ASSERT_SESSION_LIST_LOCKED();
3888
3889 /*
3890 * Ask the metadata channel creation to the consumer. The metadata object
3891 * will be created by the consumer and kept their. However, the stream is
3892 * never added or monitored until we do a first push metadata to the
3893 * consumer.
3894 */
3895 ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket,
3896 locked_registry.get(), session->current_trace_chunk);
3897 if (ret < 0) {
3898 /* Nullify the metadata key so we don't try to close it later on. */
3899 locked_registry->_metadata_key = 0;
3900 goto error_consumer;
3901 }
3902
3903 /*
3904 * The setup command will make the metadata stream be sent to the relayd,
3905 * if applicable, and the thread managing the metadatas. This is important
3906 * because after this point, if an error occurs, the only way the stream
3907 * can be deleted is to be monitored in the consumer.
3908 */
3909 ret = consumer_setup_metadata(socket, metadata->key);
3910 if (ret < 0) {
3911 /* Nullify the metadata key so we don't try to close it later on. */
3912 locked_registry->_metadata_key = 0;
3913 goto error_consumer;
3914 }
3915
3916 DBG2("UST metadata with key %" PRIu64 " created for app pid %d",
3917 metadata->key, app->pid);
3918
3919 error_consumer:
3920 lttng_fd_put(LTTNG_FD_APPS, 1);
3921 delete_ust_app_channel(-1, metadata, app, locked_registry);
3922 error:
3923 if (session) {
3924 session_put(session);
3925 }
3926 return ret;
3927 }
3928
3929 /*
3930 * Return ust app pointer or NULL if not found. RCU read side lock MUST be
3931 * acquired before calling this function.
3932 */
3933 struct ust_app *ust_app_find_by_pid(pid_t pid)
3934 {
3935 struct ust_app *app = NULL;
3936 struct lttng_ht_node_ulong *node;
3937 struct lttng_ht_iter iter;
3938
3939 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
3940 node = lttng_ht_iter_get_node_ulong(&iter);
3941 if (node == NULL) {
3942 DBG2("UST app no found with pid %d", pid);
3943 goto error;
3944 }
3945
3946 DBG2("Found UST app by pid %d", pid);
3947
3948 app = caa_container_of(node, struct ust_app, pid_n);
3949
3950 error:
3951 return app;
3952 }
3953
3954 /*
3955 * Allocate and init an UST app object using the registration information and
3956 * the command socket. This is called when the command socket connects to the
3957 * session daemon.
3958 *
3959 * The object is returned on success or else NULL.
3960 */
3961 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
3962 {
3963 int ret;
3964 struct ust_app *lta = NULL;
3965 struct lttng_pipe *event_notifier_event_source_pipe = NULL;
3966
3967 LTTNG_ASSERT(msg);
3968 LTTNG_ASSERT(sock >= 0);
3969
3970 DBG3("UST app creating application for socket %d", sock);
3971
3972 if ((msg->bits_per_long == 64 &&
3973 (uatomic_read(&the_ust_consumerd64_fd) ==
3974 -EINVAL)) ||
3975 (msg->bits_per_long == 32 &&
3976 (uatomic_read(&the_ust_consumerd32_fd) ==
3977 -EINVAL))) {
3978 ERR("Registration failed: application \"%s\" (pid: %d) has "
3979 "%d-bit long, but no consumerd for this size is available.\n",
3980 msg->name, msg->pid, msg->bits_per_long);
3981 goto error;
3982 }
3983
3984 /*
3985 * Reserve the two file descriptors of the event source pipe. The write
3986 * end will be closed once it is passed to the application, at which
3987 * point a single 'put' will be performed.
3988 */
3989 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
3990 if (ret) {
3991 ERR("Failed to reserve two file descriptors for the event source pipe while creating a new application instance: app = '%s', pid = %d",
3992 msg->name, (int) msg->pid);
3993 goto error;
3994 }
3995
3996 event_notifier_event_source_pipe = lttng_pipe_open(FD_CLOEXEC);
3997 if (!event_notifier_event_source_pipe) {
3998 PERROR("Failed to open application event source pipe: '%s' (pid = %d)",
3999 msg->name, msg->pid);
4000 goto error;
4001 }
4002
4003 lta = zmalloc<ust_app>();
4004 if (lta == NULL) {
4005 PERROR("malloc");
4006 goto error_free_pipe;
4007 }
4008
4009 lta->event_notifier_group.event_pipe = event_notifier_event_source_pipe;
4010
4011 lta->ppid = msg->ppid;
4012 lta->uid = msg->uid;
4013 lta->gid = msg->gid;
4014
4015 lta->abi = {
4016 .bits_per_long = msg->bits_per_long,
4017 .long_alignment = msg->long_alignment,
4018 .uint8_t_alignment = msg->uint8_t_alignment,
4019 .uint16_t_alignment = msg->uint16_t_alignment,
4020 .uint32_t_alignment = msg->uint32_t_alignment,
4021 .uint64_t_alignment = msg->uint64_t_alignment,
4022 .byte_order = msg->byte_order == LITTLE_ENDIAN ?
4023 lttng::sessiond::trace::byte_order::LITTLE_ENDIAN_ :
4024 lttng::sessiond::trace::byte_order::BIG_ENDIAN_,
4025 };
4026
4027 lta->v_major = msg->major;
4028 lta->v_minor = msg->minor;
4029 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4030 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4031 lta->ust_sessions_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4032 lta->notify_sock = -1;
4033 lta->token_to_event_notifier_rule_ht = lttng_ht_new(0, LTTNG_HT_TYPE_U64);
4034
4035 /* Copy name and make sure it's NULL terminated. */
4036 strncpy(lta->name, msg->name, sizeof(lta->name));
4037 lta->name[UST_APP_PROCNAME_LEN] = '\0';
4038
4039 /*
4040 * Before this can be called, when receiving the registration information,
4041 * the application compatibility is checked. So, at this point, the
4042 * application can work with this session daemon.
4043 */
4044 lta->compatible = 1;
4045
4046 lta->pid = msg->pid;
4047 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
4048 lta->sock = sock;
4049 pthread_mutex_init(&lta->sock_lock, NULL);
4050 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
4051
4052 CDS_INIT_LIST_HEAD(&lta->teardown_head);
4053 return lta;
4054
4055 error_free_pipe:
4056 lttng_pipe_destroy(event_notifier_event_source_pipe);
4057 lttng_fd_put(LTTNG_FD_APPS, 2);
4058 error:
4059 return NULL;
4060 }
4061
4062 /*
4063 * For a given application object, add it to every hash table.
4064 */
4065 void ust_app_add(struct ust_app *app)
4066 {
4067 LTTNG_ASSERT(app);
4068 LTTNG_ASSERT(app->notify_sock >= 0);
4069
4070 app->registration_time = time(NULL);
4071
4072 rcu_read_lock();
4073
4074 /*
4075 * On a re-registration, we want to kick out the previous registration of
4076 * that pid
4077 */
4078 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
4079
4080 /*
4081 * The socket _should_ be unique until _we_ call close. So, a add_unique
4082 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
4083 * already in the table.
4084 */
4085 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
4086
4087 /* Add application to the notify socket hash table. */
4088 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
4089 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
4090
4091 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock =%d name:%s "
4092 "notify_sock =%d (version %d.%d)", app->pid, app->ppid, app->uid,
4093 app->gid, app->sock, app->name, app->notify_sock, app->v_major,
4094 app->v_minor);
4095
4096 rcu_read_unlock();
4097 }
4098
4099 /*
4100 * Set the application version into the object.
4101 *
4102 * Return 0 on success else a negative value either an errno code or a
4103 * LTTng-UST error code.
4104 */
4105 int ust_app_version(struct ust_app *app)
4106 {
4107 int ret;
4108
4109 LTTNG_ASSERT(app);
4110
4111 pthread_mutex_lock(&app->sock_lock);
4112 ret = lttng_ust_ctl_tracer_version(app->sock, &app->version);
4113 pthread_mutex_unlock(&app->sock_lock);
4114 if (ret < 0) {
4115 if (ret == -LTTNG_UST_ERR_EXITING || ret == -EPIPE) {
4116 DBG3("UST app version failed. Application is dead: pid = %d, sock = %d",
4117 app->pid, app->sock);
4118 } else if (ret == -EAGAIN) {
4119 WARN("UST app version failed. Communication time out: pid = %d, sock = %d",
4120 app->pid, app->sock);
4121 } else {
4122 ERR("UST app version failed with ret %d: pid = %d, sock = %d",
4123 ret, app->pid, app->sock);
4124 }
4125 }
4126
4127 return ret;
4128 }
4129
4130 bool ust_app_supports_notifiers(const struct ust_app *app)
4131 {
4132 return app->v_major >= 9;
4133 }
4134
4135 bool ust_app_supports_counters(const struct ust_app *app)
4136 {
4137 return app->v_major >= 9;
4138 }
4139
4140 /*
4141 * Setup the base event notifier group.
4142 *
4143 * Return 0 on success else a negative value either an errno code or a
4144 * LTTng-UST error code.
4145 */
4146 int ust_app_setup_event_notifier_group(struct ust_app *app)
4147 {
4148 int ret;
4149 int event_pipe_write_fd;
4150 struct lttng_ust_abi_object_data *event_notifier_group = NULL;
4151 enum lttng_error_code lttng_ret;
4152 enum event_notifier_error_accounting_status event_notifier_error_accounting_status;
4153
4154 LTTNG_ASSERT(app);
4155
4156 if (!ust_app_supports_notifiers(app)) {
4157 ret = -ENOSYS;
4158 goto error;
4159 }
4160
4161 /* Get the write side of the pipe. */
4162 event_pipe_write_fd = lttng_pipe_get_writefd(
4163 app->event_notifier_group.event_pipe);
4164
4165 pthread_mutex_lock(&app->sock_lock);
4166 ret = lttng_ust_ctl_create_event_notifier_group(app->sock,
4167 event_pipe_write_fd, &event_notifier_group);
4168 pthread_mutex_unlock(&app->sock_lock);
4169 if (ret < 0) {
4170 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4171 ret = 0;
4172 DBG3("UST app create event notifier group failed. Application is dead: pid = %d, sock = %d",
4173 app->pid, app->sock);
4174 } else if (ret == -EAGAIN) {
4175 ret = 0;
4176 WARN("UST app create event notifier group failed. Communication time out: pid = %d, sock = %d",
4177 app->pid, app->sock);
4178 } else {
4179 ERR("UST app create event notifier group failed with ret %d: pid = %d, sock = %d, event_pipe_write_fd: %d",
4180 ret, app->pid, app->sock, event_pipe_write_fd);
4181 }
4182 goto error;
4183 }
4184
4185 ret = lttng_pipe_write_close(app->event_notifier_group.event_pipe);
4186 if (ret) {
4187 ERR("Failed to close write end of the application's event source pipe: app = '%s' (pid = %d)",
4188 app->name, app->pid);
4189 goto error;
4190 }
4191
4192 /*
4193 * Release the file descriptor that was reserved for the write-end of
4194 * the pipe.
4195 */
4196 lttng_fd_put(LTTNG_FD_APPS, 1);
4197
4198 lttng_ret = notification_thread_command_add_tracer_event_source(
4199 the_notification_thread_handle,
4200 lttng_pipe_get_readfd(
4201 app->event_notifier_group.event_pipe),
4202 LTTNG_DOMAIN_UST);
4203 if (lttng_ret != LTTNG_OK) {
4204 ERR("Failed to add tracer event source to notification thread");
4205 ret = - 1;
4206 goto error;
4207 }
4208
4209 /* Assign handle only when the complete setup is valid. */
4210 app->event_notifier_group.object = event_notifier_group;
4211
4212 event_notifier_error_accounting_status =
4213 event_notifier_error_accounting_register_app(app);
4214 switch (event_notifier_error_accounting_status) {
4215 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK:
4216 break;
4217 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_UNSUPPORTED:
4218 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",
4219 app->sock, app->name, (int) app->pid);
4220 ret = 0;
4221 goto error_accounting;
4222 case EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_APP_DEAD:
4223 DBG3("Failed to setup event notifier error accounting (application is dead): app socket fd = %d, app name = '%s', app pid = %d",
4224 app->sock, app->name, (int) app->pid);
4225 ret = 0;
4226 goto error_accounting;
4227 default:
4228 ERR("Failed to setup event notifier error accounting for app");
4229 ret = -1;
4230 goto error_accounting;
4231 }
4232
4233 return ret;
4234
4235 error_accounting:
4236 lttng_ret = notification_thread_command_remove_tracer_event_source(
4237 the_notification_thread_handle,
4238 lttng_pipe_get_readfd(
4239 app->event_notifier_group.event_pipe));
4240 if (lttng_ret != LTTNG_OK) {
4241 ERR("Failed to remove application tracer event source from notification thread");
4242 }
4243
4244 error:
4245 lttng_ust_ctl_release_object(app->sock, app->event_notifier_group.object);
4246 free(app->event_notifier_group.object);
4247 app->event_notifier_group.object = NULL;
4248 return ret;
4249 }
4250
4251 /*
4252 * Unregister app by removing it from the global traceable app list and freeing
4253 * the data struct.
4254 *
4255 * The socket is already closed at this point so no close to sock.
4256 */
4257 void ust_app_unregister(int sock)
4258 {
4259 struct ust_app *lta;
4260 struct lttng_ht_node_ulong *node;
4261 struct lttng_ht_iter ust_app_sock_iter;
4262 struct lttng_ht_iter iter;
4263 struct ust_app_session *ua_sess;
4264 int ret;
4265
4266 rcu_read_lock();
4267
4268 /* Get the node reference for a call_rcu */
4269 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter);
4270 node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter);
4271 LTTNG_ASSERT(node);
4272
4273 lta = caa_container_of(node, struct ust_app, sock_n);
4274 DBG("PID %d unregistering with sock %d", lta->pid, sock);
4275
4276 /*
4277 * For per-PID buffers, perform "push metadata" and flush all
4278 * application streams before removing app from hash tables,
4279 * ensuring proper behavior of data_pending check.
4280 * Remove sessions so they are not visible during deletion.
4281 */
4282 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
4283 node.node) {
4284 ret = lttng_ht_del(lta->sessions, &iter);
4285 if (ret) {
4286 /* The session was already removed so scheduled for teardown. */
4287 continue;
4288 }
4289
4290 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) {
4291 (void) ust_app_flush_app_session(lta, ua_sess);
4292 }
4293
4294 /*
4295 * Add session to list for teardown. This is safe since at this point we
4296 * are the only one using this list.
4297 */
4298 pthread_mutex_lock(&ua_sess->lock);
4299
4300 if (ua_sess->deleted) {
4301 pthread_mutex_unlock(&ua_sess->lock);
4302 continue;
4303 }
4304
4305 /*
4306 * Normally, this is done in the delete session process which is
4307 * executed in the call rcu below. However, upon registration we can't
4308 * afford to wait for the grace period before pushing data or else the
4309 * data pending feature can race between the unregistration and stop
4310 * command where the data pending command is sent *before* the grace
4311 * period ended.
4312 *
4313 * The close metadata below nullifies the metadata pointer in the
4314 * session so the delete session will NOT push/close a second time.
4315 */
4316 auto locked_registry = get_locked_session_registry(ua_sess);
4317 if (locked_registry) {
4318 /* Push metadata for application before freeing the application. */
4319 (void) push_metadata(locked_registry, ua_sess->consumer);
4320
4321 /*
4322 * Don't ask to close metadata for global per UID buffers. Close
4323 * metadata only on destroy trace session in this case. Also, the
4324 * previous push metadata could have flag the metadata registry to
4325 * close so don't send a close command if closed.
4326 */
4327 if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) {
4328 const auto metadata_key = locked_registry->_metadata_key;
4329 const auto consumer_bitness = locked_registry->abi.bits_per_long;
4330
4331 if (!locked_registry->_metadata_closed && metadata_key != 0) {
4332 locked_registry->_metadata_closed = true;
4333 }
4334
4335 /* Release lock before communication, see comments in close_metadata(). */
4336 locked_registry.reset();
4337 (void) close_metadata(metadata_key, consumer_bitness, ua_sess->consumer);
4338 } else {
4339 locked_registry.reset();
4340 }
4341 }
4342 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
4343
4344 pthread_mutex_unlock(&ua_sess->lock);
4345 }
4346
4347 /* Remove application from PID hash table */
4348 ret = lttng_ht_del(ust_app_ht_by_sock, &ust_app_sock_iter);
4349 LTTNG_ASSERT(!ret);
4350
4351 /*
4352 * Remove application from notify hash table. The thread handling the
4353 * notify socket could have deleted the node so ignore on error because
4354 * either way it's valid. The close of that socket is handled by the
4355 * apps_notify_thread.
4356 */
4357 iter.iter.node = &lta->notify_sock_n.node;
4358 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
4359
4360 /*
4361 * Ignore return value since the node might have been removed before by an
4362 * add replace during app registration because the PID can be reassigned by
4363 * the OS.
4364 */
4365 iter.iter.node = &lta->pid_n.node;
4366 ret = lttng_ht_del(ust_app_ht, &iter);
4367 if (ret) {
4368 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
4369 lta->pid);
4370 }
4371
4372 /* Free memory */
4373 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
4374
4375 rcu_read_unlock();
4376 return;
4377 }
4378
4379 /*
4380 * Fill events array with all events name of all registered apps.
4381 */
4382 int ust_app_list_events(struct lttng_event **events)
4383 {
4384 int ret, handle;
4385 size_t nbmem, count = 0;
4386 struct lttng_ht_iter iter;
4387 struct ust_app *app;
4388 struct lttng_event *tmp_event;
4389
4390 nbmem = UST_APP_EVENT_LIST_SIZE;
4391 tmp_event = calloc<lttng_event>(nbmem);
4392 if (tmp_event == NULL) {
4393 PERROR("zmalloc ust app events");
4394 ret = -ENOMEM;
4395 goto error;
4396 }
4397
4398 rcu_read_lock();
4399
4400 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4401 struct lttng_ust_abi_tracepoint_iter uiter;
4402
4403 health_code_update();
4404
4405 if (!app->compatible) {
4406 /*
4407 * TODO: In time, we should notice the caller of this error by
4408 * telling him that this is a version error.
4409 */
4410 continue;
4411 }
4412 pthread_mutex_lock(&app->sock_lock);
4413 handle = lttng_ust_ctl_tracepoint_list(app->sock);
4414 if (handle < 0) {
4415 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4416 ERR("UST app list events getting handle failed for app pid %d",
4417 app->pid);
4418 }
4419 pthread_mutex_unlock(&app->sock_lock);
4420 continue;
4421 }
4422
4423 while ((ret = lttng_ust_ctl_tracepoint_list_get(app->sock, handle,
4424 &uiter)) != -LTTNG_UST_ERR_NOENT) {
4425 /* Handle ustctl error. */
4426 if (ret < 0) {
4427 int release_ret;
4428
4429 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4430 ERR("UST app tp list get failed for app %d with ret %d",
4431 app->sock, ret);
4432 } else {
4433 DBG3("UST app tp list get failed. Application is dead");
4434 break;
4435 }
4436 free(tmp_event);
4437 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4438 if (release_ret < 0 &&
4439 release_ret != -LTTNG_UST_ERR_EXITING &&
4440 release_ret != -EPIPE) {
4441 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4442 }
4443 pthread_mutex_unlock(&app->sock_lock);
4444 goto rcu_error;
4445 }
4446
4447 health_code_update();
4448 if (count >= nbmem) {
4449 /* In case the realloc fails, we free the memory */
4450 struct lttng_event *new_tmp_event;
4451 size_t new_nbmem;
4452
4453 new_nbmem = nbmem << 1;
4454 DBG2("Reallocating event list from %zu to %zu entries",
4455 nbmem, new_nbmem);
4456 new_tmp_event = (lttng_event *) realloc(tmp_event,
4457 new_nbmem * sizeof(struct lttng_event));
4458 if (new_tmp_event == NULL) {
4459 int release_ret;
4460
4461 PERROR("realloc ust app events");
4462 free(tmp_event);
4463 ret = -ENOMEM;
4464 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4465 if (release_ret < 0 &&
4466 release_ret != -LTTNG_UST_ERR_EXITING &&
4467 release_ret != -EPIPE) {
4468 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4469 }
4470 pthread_mutex_unlock(&app->sock_lock);
4471 goto rcu_error;
4472 }
4473 /* Zero the new memory */
4474 memset(new_tmp_event + nbmem, 0,
4475 (new_nbmem - nbmem) * sizeof(struct lttng_event));
4476 nbmem = new_nbmem;
4477 tmp_event = new_tmp_event;
4478 }
4479 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_ABI_SYM_NAME_LEN);
4480 tmp_event[count].loglevel = uiter.loglevel;
4481 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_ABI_TRACEPOINT;
4482 tmp_event[count].pid = app->pid;
4483 tmp_event[count].enabled = -1;
4484 count++;
4485 }
4486 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4487 pthread_mutex_unlock(&app->sock_lock);
4488 if (ret < 0) {
4489 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
4490 DBG3("Error releasing app handle. Application died: pid = %d, sock = %d",
4491 app->pid, app->sock);
4492 } else if (ret == -EAGAIN) {
4493 WARN("Error releasing app handle. Communication time out: pid = %d, sock = %d",
4494 app->pid, app->sock);
4495 } else {
4496 ERR("Error releasing app handle with ret %d: pid = %d, sock = %d",
4497 ret, app->pid, app->sock);
4498 }
4499 }
4500 }
4501
4502 ret = count;
4503 *events = tmp_event;
4504
4505 DBG2("UST app list events done (%zu events)", count);
4506
4507 rcu_error:
4508 rcu_read_unlock();
4509 error:
4510 health_code_update();
4511 return ret;
4512 }
4513
4514 /*
4515 * Fill events array with all events name of all registered apps.
4516 */
4517 int ust_app_list_event_fields(struct lttng_event_field **fields)
4518 {
4519 int ret, handle;
4520 size_t nbmem, count = 0;
4521 struct lttng_ht_iter iter;
4522 struct ust_app *app;
4523 struct lttng_event_field *tmp_event;
4524
4525 nbmem = UST_APP_EVENT_LIST_SIZE;
4526 tmp_event = calloc<lttng_event_field>(nbmem);
4527 if (tmp_event == NULL) {
4528 PERROR("zmalloc ust app event fields");
4529 ret = -ENOMEM;
4530 goto error;
4531 }
4532
4533 rcu_read_lock();
4534
4535 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4536 struct lttng_ust_abi_field_iter uiter;
4537
4538 health_code_update();
4539
4540 if (!app->compatible) {
4541 /*
4542 * TODO: In time, we should notice the caller of this error by
4543 * telling him that this is a version error.
4544 */
4545 continue;
4546 }
4547 pthread_mutex_lock(&app->sock_lock);
4548 handle = lttng_ust_ctl_tracepoint_field_list(app->sock);
4549 if (handle < 0) {
4550 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
4551 ERR("UST app list field getting handle failed for app pid %d",
4552 app->pid);
4553 }
4554 pthread_mutex_unlock(&app->sock_lock);
4555 continue;
4556 }
4557
4558 while ((ret = lttng_ust_ctl_tracepoint_field_list_get(app->sock, handle,
4559 &uiter)) != -LTTNG_UST_ERR_NOENT) {
4560 /* Handle ustctl error. */
4561 if (ret < 0) {
4562 int release_ret;
4563
4564 if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) {
4565 ERR("UST app tp list field failed for app %d with ret %d",
4566 app->sock, ret);
4567 } else {
4568 DBG3("UST app tp list field failed. Application is dead");
4569 break;
4570 }
4571 free(tmp_event);
4572 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4573 pthread_mutex_unlock(&app->sock_lock);
4574 if (release_ret < 0 &&
4575 release_ret != -LTTNG_UST_ERR_EXITING &&
4576 release_ret != -EPIPE) {
4577 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4578 }
4579 goto rcu_error;
4580 }
4581
4582 health_code_update();
4583 if (count >= nbmem) {
4584 /* In case the realloc fails, we free the memory */
4585 struct lttng_event_field *new_tmp_event;
4586 size_t new_nbmem;
4587
4588 new_nbmem = nbmem << 1;
4589 DBG2("Reallocating event field list from %zu to %zu entries",
4590 nbmem, new_nbmem);
4591 new_tmp_event = (lttng_event_field *) realloc(tmp_event,
4592 new_nbmem * sizeof(struct lttng_event_field));
4593 if (new_tmp_event == NULL) {
4594 int release_ret;
4595
4596 PERROR("realloc ust app event fields");
4597 free(tmp_event);
4598 ret = -ENOMEM;
4599 release_ret = lttng_ust_ctl_release_handle(app->sock, handle);
4600 pthread_mutex_unlock(&app->sock_lock);
4601 if (release_ret &&
4602 release_ret != -LTTNG_UST_ERR_EXITING &&
4603 release_ret != -EPIPE) {
4604 ERR("Error releasing app handle for app %d with ret %d", app->sock, release_ret);
4605 }
4606 goto rcu_error;
4607 }
4608 /* Zero the new memory */
4609 memset(new_tmp_event + nbmem, 0,
4610 (new_nbmem - nbmem) * sizeof(struct lttng_event_field));
4611 nbmem = new_nbmem;
4612 tmp_event = new_tmp_event;
4613 }
4614
4615 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_ABI_SYM_NAME_LEN);
4616 /* Mapping between these enums matches 1 to 1. */
4617 tmp_event[count].type = (enum lttng_event_field_type) uiter.type;
4618 tmp_event[count].nowrite = uiter.nowrite;
4619
4620 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_ABI_SYM_NAME_LEN);
4621 tmp_event[count].event.loglevel = uiter.loglevel;
4622 tmp_event[count].event.type = LTTNG_EVENT_TRACEPOINT;
4623 tmp_event[count].event.pid = app->pid;
4624 tmp_event[count].event.enabled = -1;
4625 count++;
4626 }
4627 ret = lttng_ust_ctl_release_handle(app->sock, handle);
4628 pthread_mutex_unlock(&app->sock_lock);
4629 if (ret < 0 &&
4630 ret != -LTTNG_UST_ERR_EXITING &&
4631 ret != -EPIPE) {
4632 ERR("Error releasing app handle for app %d with ret %d", app->sock, ret);
4633 }
4634 }
4635
4636 ret = count;
4637 *fields = tmp_event;
4638
4639 DBG2("UST app list event fields done (%zu events)", count);
4640
4641 rcu_error:
4642 rcu_read_unlock();
4643 error:
4644 health_code_update();
4645 return ret;
4646 }
4647
4648 /*
4649 * Free and clean all traceable apps of the global list.
4650 */
4651 void ust_app_clean_list(void)
4652 {
4653 int ret;
4654 struct ust_app *app;
4655 struct lttng_ht_iter iter;
4656
4657 DBG2("UST app cleaning registered apps hash table");
4658
4659 rcu_read_lock();
4660
4661 /* Cleanup notify socket hash table */
4662 if (ust_app_ht_by_notify_sock) {
4663 cds_lfht_for_each_entry(ust_app_ht_by_notify_sock->ht, &iter.iter, app,
4664 notify_sock_n.node) {
4665 /*
4666 * Assert that all notifiers are gone as all triggers
4667 * are unregistered prior to this clean-up.
4668 */
4669 LTTNG_ASSERT(lttng_ht_get_count(app->token_to_event_notifier_rule_ht) == 0);
4670
4671 ust_app_notify_sock_unregister(app->notify_sock);
4672 }
4673 }
4674
4675 if (ust_app_ht) {
4676 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4677 ret = lttng_ht_del(ust_app_ht, &iter);
4678 LTTNG_ASSERT(!ret);
4679 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
4680 }
4681 }
4682
4683 /* Cleanup socket hash table */
4684 if (ust_app_ht_by_sock) {
4685 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
4686 sock_n.node) {
4687 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
4688 LTTNG_ASSERT(!ret);
4689 }
4690 }
4691
4692 rcu_read_unlock();
4693
4694 /* Destroy is done only when the ht is empty */
4695 if (ust_app_ht) {
4696 lttng_ht_destroy(ust_app_ht);
4697 }
4698 if (ust_app_ht_by_sock) {
4699 lttng_ht_destroy(ust_app_ht_by_sock);
4700 }
4701 if (ust_app_ht_by_notify_sock) {
4702 lttng_ht_destroy(ust_app_ht_by_notify_sock);
4703 }
4704 }
4705
4706 /*
4707 * Init UST app hash table.
4708 */
4709 int ust_app_ht_alloc(void)
4710 {
4711 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4712 if (!ust_app_ht) {
4713 return -1;
4714 }
4715 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4716 if (!ust_app_ht_by_sock) {
4717 return -1;
4718 }
4719 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
4720 if (!ust_app_ht_by_notify_sock) {
4721 return -1;
4722 }
4723 return 0;
4724 }
4725
4726 /*
4727 * For a specific UST session, disable the channel for all registered apps.
4728 */
4729 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
4730 struct ltt_ust_channel *uchan)
4731 {
4732 int ret = 0;
4733 struct lttng_ht_iter iter;
4734 struct lttng_ht_node_str *ua_chan_node;
4735 struct ust_app *app;
4736 struct ust_app_session *ua_sess;
4737 struct ust_app_channel *ua_chan;
4738
4739 LTTNG_ASSERT(usess->active);
4740 DBG2("UST app disabling channel %s from global domain for session id %" PRIu64,
4741 uchan->name, usess->id);
4742
4743 rcu_read_lock();
4744
4745 /* For every registered applications */
4746 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4747 struct lttng_ht_iter uiter;
4748 if (!app->compatible) {
4749 /*
4750 * TODO: In time, we should notice the caller of this error by
4751 * telling him that this is a version error.
4752 */
4753 continue;
4754 }
4755 ua_sess = lookup_session_by_app(usess, app);
4756 if (ua_sess == NULL) {
4757 continue;
4758 }
4759
4760 /* Get channel */
4761 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4762 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4763 /* If the session if found for the app, the channel must be there */
4764 LTTNG_ASSERT(ua_chan_node);
4765
4766 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4767 /* The channel must not be already disabled */
4768 LTTNG_ASSERT(ua_chan->enabled == 1);
4769
4770 /* Disable channel onto application */
4771 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
4772 if (ret < 0) {
4773 /* XXX: We might want to report this error at some point... */
4774 continue;
4775 }
4776 }
4777
4778 rcu_read_unlock();
4779 return ret;
4780 }
4781
4782 /*
4783 * For a specific UST session, enable the channel for all registered apps.
4784 */
4785 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
4786 struct ltt_ust_channel *uchan)
4787 {
4788 int ret = 0;
4789 struct lttng_ht_iter iter;
4790 struct ust_app *app;
4791 struct ust_app_session *ua_sess;
4792
4793 LTTNG_ASSERT(usess->active);
4794 DBG2("UST app enabling channel %s to global domain for session id %" PRIu64,
4795 uchan->name, usess->id);
4796
4797 rcu_read_lock();
4798
4799 /* For every registered applications */
4800 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4801 if (!app->compatible) {
4802 /*
4803 * TODO: In time, we should notice the caller of this error by
4804 * telling him that this is a version error.
4805 */
4806 continue;
4807 }
4808 ua_sess = lookup_session_by_app(usess, app);
4809 if (ua_sess == NULL) {
4810 continue;
4811 }
4812
4813 /* Enable channel onto application */
4814 ret = enable_ust_app_channel(ua_sess, uchan, app);
4815 if (ret < 0) {
4816 /* XXX: We might want to report this error at some point... */
4817 continue;
4818 }
4819 }
4820
4821 rcu_read_unlock();
4822 return ret;
4823 }
4824
4825 /*
4826 * Disable an event in a channel and for a specific session.
4827 */
4828 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
4829 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4830 {
4831 int ret = 0;
4832 struct lttng_ht_iter iter, uiter;
4833 struct lttng_ht_node_str *ua_chan_node;
4834 struct ust_app *app;
4835 struct ust_app_session *ua_sess;
4836 struct ust_app_channel *ua_chan;
4837 struct ust_app_event *ua_event;
4838
4839 LTTNG_ASSERT(usess->active);
4840 DBG("UST app disabling event %s for all apps in channel "
4841 "%s for session id %" PRIu64,
4842 uevent->attr.name, uchan->name, usess->id);
4843
4844 rcu_read_lock();
4845
4846 /* For all registered applications */
4847 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4848 if (!app->compatible) {
4849 /*
4850 * TODO: In time, we should notice the caller of this error by
4851 * telling him that this is a version error.
4852 */
4853 continue;
4854 }
4855 ua_sess = lookup_session_by_app(usess, app);
4856 if (ua_sess == NULL) {
4857 /* Next app */
4858 continue;
4859 }
4860
4861 /* Lookup channel in the ust app session */
4862 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
4863 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
4864 if (ua_chan_node == NULL) {
4865 DBG2("Channel %s not found in session id %" PRIu64 " for app pid %d."
4866 "Skipping", uchan->name, usess->id, app->pid);
4867 continue;
4868 }
4869 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
4870
4871 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
4872 uevent->filter, uevent->attr.loglevel,
4873 uevent->exclusion);
4874 if (ua_event == NULL) {
4875 DBG2("Event %s not found in channel %s for app pid %d."
4876 "Skipping", uevent->attr.name, uchan->name, app->pid);
4877 continue;
4878 }
4879
4880 ret = disable_ust_app_event(ua_event, app);
4881 if (ret < 0) {
4882 /* XXX: Report error someday... */
4883 continue;
4884 }
4885 }
4886
4887 rcu_read_unlock();
4888 return ret;
4889 }
4890
4891 /* The ua_sess lock must be held by the caller. */
4892 static
4893 int ust_app_channel_create(struct ltt_ust_session *usess,
4894 struct ust_app_session *ua_sess,
4895 struct ltt_ust_channel *uchan, struct ust_app *app,
4896 struct ust_app_channel **_ua_chan)
4897 {
4898 int ret = 0;
4899 struct ust_app_channel *ua_chan = NULL;
4900
4901 LTTNG_ASSERT(ua_sess);
4902 ASSERT_LOCKED(ua_sess->lock);
4903
4904 if (!strncmp(uchan->name, DEFAULT_METADATA_NAME,
4905 sizeof(uchan->name))) {
4906 copy_channel_attr_to_ustctl(&ua_sess->metadata_attr,
4907 &uchan->attr);
4908 ret = 0;
4909 } else {
4910 struct ltt_ust_context *uctx = NULL;
4911
4912 /*
4913 * Create channel onto application and synchronize its
4914 * configuration.
4915 */
4916 ret = ust_app_channel_allocate(ua_sess, uchan,
4917 LTTNG_UST_ABI_CHAN_PER_CPU, usess,
4918 &ua_chan);
4919 if (ret < 0) {
4920 goto error;
4921 }
4922
4923 ret = ust_app_channel_send(app, usess,
4924 ua_sess, ua_chan);
4925 if (ret) {
4926 goto error;
4927 }
4928
4929 /* Add contexts. */
4930 cds_list_for_each_entry(uctx, &uchan->ctx_list, list) {
4931 ret = create_ust_app_channel_context(ua_chan,
4932 &uctx->ctx, app);
4933 if (ret) {
4934 goto error;
4935 }
4936 }
4937 }
4938
4939 error:
4940 if (ret < 0) {
4941 switch (ret) {
4942 case -ENOTCONN:
4943 /*
4944 * The application's socket is not valid. Either a bad socket
4945 * or a timeout on it. We can't inform the caller that for a
4946 * specific app, the session failed so lets continue here.
4947 */
4948 ret = 0; /* Not an error. */
4949 break;
4950 case -ENOMEM:
4951 default:
4952 break;
4953 }
4954 }
4955
4956 if (ret == 0 && _ua_chan) {
4957 /*
4958 * Only return the application's channel on success. Note
4959 * that the channel can still be part of the application's
4960 * channel hashtable on error.
4961 */
4962 *_ua_chan = ua_chan;
4963 }
4964 return ret;
4965 }
4966
4967 /*
4968 * Enable event for a specific session and channel on the tracer.
4969 */
4970 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
4971 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
4972 {
4973 int ret = 0;
4974 struct lttng_ht_iter iter, uiter;
4975 struct lttng_ht_node_str *ua_chan_node;
4976 struct ust_app *app;
4977 struct ust_app_session *ua_sess;
4978 struct ust_app_channel *ua_chan;
4979 struct ust_app_event *ua_event;
4980
4981 LTTNG_ASSERT(usess->active);
4982 DBG("UST app enabling event %s for all apps for session id %" PRIu64,
4983 uevent->attr.name, usess->id);
4984
4985 /*
4986 * NOTE: At this point, this function is called only if the session and
4987 * channel passed are already created for all apps. and enabled on the
4988 * tracer also.
4989 */
4990
4991 rcu_read_lock();
4992
4993 /* For all registered applications */
4994 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
4995 if (!app->compatible) {
4996 /*
4997 * TODO: In time, we should notice the caller of this error by
4998 * telling him that this is a version error.
4999 */
5000 continue;
5001 }
5002 ua_sess = lookup_session_by_app(usess, app);
5003 if (!ua_sess) {
5004 /* The application has problem or is probably dead. */
5005 continue;
5006 }
5007
5008 pthread_mutex_lock(&ua_sess->lock);
5009
5010 if (ua_sess->deleted) {
5011 pthread_mutex_unlock(&ua_sess->lock);
5012 continue;
5013 }
5014
5015 /* Lookup channel in the ust app session */
5016 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5017 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5018 /*
5019 * It is possible that the channel cannot be found is
5020 * the channel/event creation occurs concurrently with
5021 * an application exit.
5022 */
5023 if (!ua_chan_node) {
5024 pthread_mutex_unlock(&ua_sess->lock);
5025 continue;
5026 }
5027
5028 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5029
5030 /* Get event node */
5031 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5032 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5033 if (ua_event == NULL) {
5034 DBG3("UST app enable event %s not found for app PID %d."
5035 "Skipping app", uevent->attr.name, app->pid);
5036 goto next_app;
5037 }
5038
5039 ret = enable_ust_app_event(ua_event, app);
5040 if (ret < 0) {
5041 pthread_mutex_unlock(&ua_sess->lock);
5042 goto error;
5043 }
5044 next_app:
5045 pthread_mutex_unlock(&ua_sess->lock);
5046 }
5047
5048 error:
5049 rcu_read_unlock();
5050 return ret;
5051 }
5052
5053 /*
5054 * For a specific existing UST session and UST channel, creates the event for
5055 * all registered apps.
5056 */
5057 int ust_app_create_event_glb(struct ltt_ust_session *usess,
5058 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
5059 {
5060 int ret = 0;
5061 struct lttng_ht_iter iter, uiter;
5062 struct lttng_ht_node_str *ua_chan_node;
5063 struct ust_app *app;
5064 struct ust_app_session *ua_sess;
5065 struct ust_app_channel *ua_chan;
5066
5067 LTTNG_ASSERT(usess->active);
5068 DBG("UST app creating event %s for all apps for session id %" PRIu64,
5069 uevent->attr.name, usess->id);
5070
5071 rcu_read_lock();
5072
5073 /* For all registered applications */
5074 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5075 if (!app->compatible) {
5076 /*
5077 * TODO: In time, we should notice the caller of this error by
5078 * telling him that this is a version error.
5079 */
5080 continue;
5081 }
5082 ua_sess = lookup_session_by_app(usess, app);
5083 if (!ua_sess) {
5084 /* The application has problem or is probably dead. */
5085 continue;
5086 }
5087
5088 pthread_mutex_lock(&ua_sess->lock);
5089
5090 if (ua_sess->deleted) {
5091 pthread_mutex_unlock(&ua_sess->lock);
5092 continue;
5093 }
5094
5095 /* Lookup channel in the ust app session */
5096 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
5097 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
5098 /* If the channel is not found, there is a code flow error */
5099 LTTNG_ASSERT(ua_chan_node);
5100
5101 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
5102
5103 ret = create_ust_app_event(ua_chan, uevent, app);
5104 pthread_mutex_unlock(&ua_sess->lock);
5105 if (ret < 0) {
5106 if (ret != -LTTNG_UST_ERR_EXIST) {
5107 /* Possible value at this point: -ENOMEM. If so, we stop! */
5108 break;
5109 }
5110 DBG2("UST app event %s already exist on app PID %d",
5111 uevent->attr.name, app->pid);
5112 continue;
5113 }
5114 }
5115
5116 rcu_read_unlock();
5117 return ret;
5118 }
5119
5120 /*
5121 * Start tracing for a specific UST session and app.
5122 *
5123 * Called with UST app session lock held.
5124 *
5125 */
5126 static
5127 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
5128 {
5129 int ret = 0;
5130 struct ust_app_session *ua_sess;
5131
5132 DBG("Starting tracing for ust app pid %d", app->pid);
5133
5134 rcu_read_lock();
5135
5136 if (!app->compatible) {
5137 goto end;
5138 }
5139
5140 ua_sess = lookup_session_by_app(usess, app);
5141 if (ua_sess == NULL) {
5142 /* The session is in teardown process. Ignore and continue. */
5143 goto end;
5144 }
5145
5146 pthread_mutex_lock(&ua_sess->lock);
5147
5148 if (ua_sess->deleted) {
5149 pthread_mutex_unlock(&ua_sess->lock);
5150 goto end;
5151 }
5152
5153 if (ua_sess->enabled) {
5154 pthread_mutex_unlock(&ua_sess->lock);
5155 goto end;
5156 }
5157
5158 /* Upon restart, we skip the setup, already done */
5159 if (ua_sess->started) {
5160 goto skip_setup;
5161 }
5162
5163 health_code_update();
5164
5165 skip_setup:
5166 /* This starts the UST tracing */
5167 pthread_mutex_lock(&app->sock_lock);
5168 ret = lttng_ust_ctl_start_session(app->sock, ua_sess->handle);
5169 pthread_mutex_unlock(&app->sock_lock);
5170 if (ret < 0) {
5171 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5172 DBG3("UST app start session failed. Application is dead: pid = %d, sock = %d",
5173 app->pid, app->sock);
5174 pthread_mutex_unlock(&ua_sess->lock);
5175 goto end;
5176 } else if (ret == -EAGAIN) {
5177 WARN("UST app start session failed. Communication time out: pid = %d, sock = %d",
5178 app->pid, app->sock);
5179 pthread_mutex_unlock(&ua_sess->lock);
5180 goto end;
5181
5182 } else {
5183 ERR("UST app start session failed with ret %d: pid = %d, sock = %d",
5184 ret, app->pid, app->sock);
5185 }
5186 goto error_unlock;
5187 }
5188
5189 /* Indicate that the session has been started once */
5190 ua_sess->started = 1;
5191 ua_sess->enabled = 1;
5192
5193 pthread_mutex_unlock(&ua_sess->lock);
5194
5195 health_code_update();
5196
5197 /* Quiescent wait after starting trace */
5198 pthread_mutex_lock(&app->sock_lock);
5199 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5200 pthread_mutex_unlock(&app->sock_lock);
5201 if (ret < 0) {
5202 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5203 DBG3("UST app wait quiescent failed. Application is dead: pid = %d, sock = %d",
5204 app->pid, app->sock);
5205 } else if (ret == -EAGAIN) {
5206 WARN("UST app wait quiescent failed. Communication time out: pid = %d, sock = %d",
5207 app->pid, app->sock);
5208 } else {
5209 ERR("UST app wait quiescent failed with ret %d: pid %d, sock = %d",
5210 ret, app->pid, app->sock);
5211 }
5212 }
5213
5214 end:
5215 rcu_read_unlock();
5216 health_code_update();
5217 return 0;
5218
5219 error_unlock:
5220 pthread_mutex_unlock(&ua_sess->lock);
5221 rcu_read_unlock();
5222 health_code_update();
5223 return -1;
5224 }
5225
5226 /*
5227 * Stop tracing for a specific UST session and app.
5228 */
5229 static
5230 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
5231 {
5232 int ret = 0;
5233 struct ust_app_session *ua_sess;
5234
5235 DBG("Stopping tracing for ust app pid %d", app->pid);
5236
5237 rcu_read_lock();
5238
5239 if (!app->compatible) {
5240 goto end_no_session;
5241 }
5242
5243 ua_sess = lookup_session_by_app(usess, app);
5244 if (ua_sess == NULL) {
5245 goto end_no_session;
5246 }
5247
5248 pthread_mutex_lock(&ua_sess->lock);
5249
5250 if (ua_sess->deleted) {
5251 pthread_mutex_unlock(&ua_sess->lock);
5252 goto end_no_session;
5253 }
5254
5255 /*
5256 * If started = 0, it means that stop trace has been called for a session
5257 * that was never started. It's possible since we can have a fail start
5258 * from either the application manager thread or the command thread. Simply
5259 * indicate that this is a stop error.
5260 */
5261 if (!ua_sess->started) {
5262 goto error_rcu_unlock;
5263 }
5264
5265 health_code_update();
5266
5267 /* This inhibits UST tracing */
5268 pthread_mutex_lock(&app->sock_lock);
5269 ret = lttng_ust_ctl_stop_session(app->sock, ua_sess->handle);
5270 pthread_mutex_unlock(&app->sock_lock);
5271 if (ret < 0) {
5272 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5273 DBG3("UST app stop session failed. Application is dead: pid = %d, sock = %d",
5274 app->pid, app->sock);
5275 goto end_unlock;
5276 } else if (ret == -EAGAIN) {
5277 WARN("UST app stop session failed. Communication time out: pid = %d, sock = %d",
5278 app->pid, app->sock);
5279 goto end_unlock;
5280
5281 } else {
5282 ERR("UST app stop session failed with ret %d: pid = %d, sock = %d",
5283 ret, app->pid, app->sock);
5284 }
5285 goto error_rcu_unlock;
5286 }
5287
5288 health_code_update();
5289 ua_sess->enabled = 0;
5290
5291 /* Quiescent wait after stopping trace */
5292 pthread_mutex_lock(&app->sock_lock);
5293 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5294 pthread_mutex_unlock(&app->sock_lock);
5295 if (ret < 0) {
5296 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5297 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5298 app->pid, app->sock);
5299 } else if (ret == -EAGAIN) {
5300 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5301 app->pid, app->sock);
5302 } else {
5303 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5304 ret, app->pid, app->sock);
5305 }
5306 }
5307
5308 health_code_update();
5309
5310 {
5311 auto locked_registry = get_locked_session_registry(ua_sess);
5312
5313 /* The UST app session is held registry shall not be null. */
5314 LTTNG_ASSERT(locked_registry);
5315
5316 /* Push metadata for application before freeing the application. */
5317 (void) push_metadata(locked_registry, ua_sess->consumer);
5318 }
5319
5320 end_unlock:
5321 pthread_mutex_unlock(&ua_sess->lock);
5322 end_no_session:
5323 rcu_read_unlock();
5324 health_code_update();
5325 return 0;
5326
5327 error_rcu_unlock:
5328 pthread_mutex_unlock(&ua_sess->lock);
5329 rcu_read_unlock();
5330 health_code_update();
5331 return -1;
5332 }
5333
5334 static
5335 int ust_app_flush_app_session(struct ust_app *app,
5336 struct ust_app_session *ua_sess)
5337 {
5338 int ret, retval = 0;
5339 struct lttng_ht_iter iter;
5340 struct ust_app_channel *ua_chan;
5341 struct consumer_socket *socket;
5342
5343 DBG("Flushing app session buffers for ust app pid %d", app->pid);
5344
5345 rcu_read_lock();
5346
5347 if (!app->compatible) {
5348 goto end_not_compatible;
5349 }
5350
5351 pthread_mutex_lock(&ua_sess->lock);
5352
5353 if (ua_sess->deleted) {
5354 goto end_deleted;
5355 }
5356
5357 health_code_update();
5358
5359 /* Flushing buffers */
5360 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
5361 ua_sess->consumer);
5362
5363 /* Flush buffers and push metadata. */
5364 switch (ua_sess->buffer_type) {
5365 case LTTNG_BUFFER_PER_PID:
5366 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
5367 node.node) {
5368 health_code_update();
5369 ret = consumer_flush_channel(socket, ua_chan->key);
5370 if (ret) {
5371 ERR("Error flushing consumer channel");
5372 retval = -1;
5373 continue;
5374 }
5375 }
5376 break;
5377 case LTTNG_BUFFER_PER_UID:
5378 default:
5379 abort();
5380 break;
5381 }
5382
5383 health_code_update();
5384
5385 end_deleted:
5386 pthread_mutex_unlock(&ua_sess->lock);
5387
5388 end_not_compatible:
5389 rcu_read_unlock();
5390 health_code_update();
5391 return retval;
5392 }
5393
5394 /*
5395 * Flush buffers for all applications for a specific UST session.
5396 * Called with UST session lock held.
5397 */
5398 static
5399 int ust_app_flush_session(struct ltt_ust_session *usess)
5400
5401 {
5402 int ret = 0;
5403
5404 DBG("Flushing session buffers for all ust apps");
5405
5406 rcu_read_lock();
5407
5408 /* Flush buffers and push metadata. */
5409 switch (usess->buffer_type) {
5410 case LTTNG_BUFFER_PER_UID:
5411 {
5412 struct buffer_reg_uid *reg;
5413 struct lttng_ht_iter iter;
5414
5415 /* Flush all per UID buffers associated to that session. */
5416 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5417 lsu::registry_session *ust_session_reg;
5418 struct buffer_reg_channel *buf_reg_chan;
5419 struct consumer_socket *socket;
5420
5421 /* Get consumer socket to use to push the metadata.*/
5422 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
5423 usess->consumer);
5424 if (!socket) {
5425 /* Ignore request if no consumer is found for the session. */
5426 continue;
5427 }
5428
5429 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
5430 buf_reg_chan, node.node) {
5431 /*
5432 * The following call will print error values so the return
5433 * code is of little importance because whatever happens, we
5434 * have to try them all.
5435 */
5436 (void) consumer_flush_channel(socket, buf_reg_chan->consumer_key);
5437 }
5438
5439 ust_session_reg = reg->registry->reg.ust;
5440 /* Push metadata. */
5441 auto locked_registry = ust_session_reg->lock();
5442 (void) push_metadata(locked_registry, usess->consumer);
5443 }
5444 break;
5445 }
5446 case LTTNG_BUFFER_PER_PID:
5447 {
5448 struct ust_app_session *ua_sess;
5449 struct lttng_ht_iter iter;
5450 struct ust_app *app;
5451
5452 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5453 ua_sess = lookup_session_by_app(usess, app);
5454 if (ua_sess == NULL) {
5455 continue;
5456 }
5457 (void) ust_app_flush_app_session(app, ua_sess);
5458 }
5459 break;
5460 }
5461 default:
5462 ret = -1;
5463 abort();
5464 break;
5465 }
5466
5467 rcu_read_unlock();
5468 health_code_update();
5469 return ret;
5470 }
5471
5472 static
5473 int ust_app_clear_quiescent_app_session(struct ust_app *app,
5474 struct ust_app_session *ua_sess)
5475 {
5476 int ret = 0;
5477 struct lttng_ht_iter iter;
5478 struct ust_app_channel *ua_chan;
5479 struct consumer_socket *socket;
5480
5481 DBG("Clearing stream quiescent state for ust app pid %d", app->pid);
5482
5483 rcu_read_lock();
5484
5485 if (!app->compatible) {
5486 goto end_not_compatible;
5487 }
5488
5489 pthread_mutex_lock(&ua_sess->lock);
5490
5491 if (ua_sess->deleted) {
5492 goto end_unlock;
5493 }
5494
5495 health_code_update();
5496
5497 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
5498 ua_sess->consumer);
5499 if (!socket) {
5500 ERR("Failed to find consumer (%" PRIu32 ") socket",
5501 app->abi.bits_per_long);
5502 ret = -1;
5503 goto end_unlock;
5504 }
5505
5506 /* Clear quiescent state. */
5507 switch (ua_sess->buffer_type) {
5508 case LTTNG_BUFFER_PER_PID:
5509 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter,
5510 ua_chan, node.node) {
5511 health_code_update();
5512 ret = consumer_clear_quiescent_channel(socket,
5513 ua_chan->key);
5514 if (ret) {
5515 ERR("Error clearing quiescent state for consumer channel");
5516 ret = -1;
5517 continue;
5518 }
5519 }
5520 break;
5521 case LTTNG_BUFFER_PER_UID:
5522 default:
5523 abort();
5524 ret = -1;
5525 break;
5526 }
5527
5528 health_code_update();
5529
5530 end_unlock:
5531 pthread_mutex_unlock(&ua_sess->lock);
5532
5533 end_not_compatible:
5534 rcu_read_unlock();
5535 health_code_update();
5536 return ret;
5537 }
5538
5539 /*
5540 * Clear quiescent state in each stream for all applications for a
5541 * specific UST session.
5542 * Called with UST session lock held.
5543 */
5544 static
5545 int ust_app_clear_quiescent_session(struct ltt_ust_session *usess)
5546
5547 {
5548 int ret = 0;
5549
5550 DBG("Clearing stream quiescent state for all ust apps");
5551
5552 rcu_read_lock();
5553
5554 switch (usess->buffer_type) {
5555 case LTTNG_BUFFER_PER_UID:
5556 {
5557 struct lttng_ht_iter iter;
5558 struct buffer_reg_uid *reg;
5559
5560 /*
5561 * Clear quiescent for all per UID buffers associated to
5562 * that session.
5563 */
5564 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
5565 struct consumer_socket *socket;
5566 struct buffer_reg_channel *buf_reg_chan;
5567
5568 /* Get associated consumer socket.*/
5569 socket = consumer_find_socket_by_bitness(
5570 reg->bits_per_long, usess->consumer);
5571 if (!socket) {
5572 /*
5573 * Ignore request if no consumer is found for
5574 * the session.
5575 */
5576 continue;
5577 }
5578
5579 cds_lfht_for_each_entry(reg->registry->channels->ht,
5580 &iter.iter, buf_reg_chan, node.node) {
5581 /*
5582 * The following call will print error values so
5583 * the return code is of little importance
5584 * because whatever happens, we have to try them
5585 * all.
5586 */
5587 (void) consumer_clear_quiescent_channel(socket,
5588 buf_reg_chan->consumer_key);
5589 }
5590 }
5591 break;
5592 }
5593 case LTTNG_BUFFER_PER_PID:
5594 {
5595 struct ust_app_session *ua_sess;
5596 struct lttng_ht_iter iter;
5597 struct ust_app *app;
5598
5599 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
5600 pid_n.node) {
5601 ua_sess = lookup_session_by_app(usess, app);
5602 if (ua_sess == NULL) {
5603 continue;
5604 }
5605 (void) ust_app_clear_quiescent_app_session(app,
5606 ua_sess);
5607 }
5608 break;
5609 }
5610 default:
5611 ret = -1;
5612 abort();
5613 break;
5614 }
5615
5616 rcu_read_unlock();
5617 health_code_update();
5618 return ret;
5619 }
5620
5621 /*
5622 * Destroy a specific UST session in apps.
5623 */
5624 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
5625 {
5626 int ret;
5627 struct ust_app_session *ua_sess;
5628 struct lttng_ht_iter iter;
5629 struct lttng_ht_node_u64 *node;
5630
5631 DBG("Destroy tracing for ust app pid %d", app->pid);
5632
5633 rcu_read_lock();
5634
5635 if (!app->compatible) {
5636 goto end;
5637 }
5638
5639 __lookup_session_by_app(usess, app, &iter);
5640 node = lttng_ht_iter_get_node_u64(&iter);
5641 if (node == NULL) {
5642 /* Session is being or is deleted. */
5643 goto end;
5644 }
5645 ua_sess = caa_container_of(node, struct ust_app_session, node);
5646
5647 health_code_update();
5648 destroy_app_session(app, ua_sess);
5649
5650 health_code_update();
5651
5652 /* Quiescent wait after stopping trace */
5653 pthread_mutex_lock(&app->sock_lock);
5654 ret = lttng_ust_ctl_wait_quiescent(app->sock);
5655 pthread_mutex_unlock(&app->sock_lock);
5656 if (ret < 0) {
5657 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
5658 DBG3("UST app wait quiescent failed. Application is dead: pid= %d, sock = %d",
5659 app->pid, app->sock);
5660 } else if (ret == -EAGAIN) {
5661 WARN("UST app wait quiescent failed. Communication time out: pid= %d, sock = %d",
5662 app->pid, app->sock);
5663 } else {
5664 ERR("UST app wait quiescent failed with ret %d: pid= %d, sock = %d",
5665 ret, app->pid, app->sock);
5666 }
5667 }
5668 end:
5669 rcu_read_unlock();
5670 health_code_update();
5671 return 0;
5672 }
5673
5674 /*
5675 * Start tracing for the UST session.
5676 */
5677 int ust_app_start_trace_all(struct ltt_ust_session *usess)
5678 {
5679 struct lttng_ht_iter iter;
5680 struct ust_app *app;
5681
5682 DBG("Starting all UST traces");
5683
5684 /*
5685 * Even though the start trace might fail, flag this session active so
5686 * other application coming in are started by default.
5687 */
5688 usess->active = 1;
5689
5690 rcu_read_lock();
5691
5692 /*
5693 * In a start-stop-start use-case, we need to clear the quiescent state
5694 * of each channel set by the prior stop command, thus ensuring that a
5695 * following stop or destroy is sure to grab a timestamp_end near those
5696 * operations, even if the packet is empty.
5697 */
5698 (void) ust_app_clear_quiescent_session(usess);
5699
5700 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5701 ust_app_global_update(usess, app);
5702 }
5703
5704 rcu_read_unlock();
5705
5706 return 0;
5707 }
5708
5709 /*
5710 * Start tracing for the UST session.
5711 * Called with UST session lock held.
5712 */
5713 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
5714 {
5715 int ret = 0;
5716 struct lttng_ht_iter iter;
5717 struct ust_app *app;
5718
5719 DBG("Stopping all UST traces");
5720
5721 /*
5722 * Even though the stop trace might fail, flag this session inactive so
5723 * other application coming in are not started by default.
5724 */
5725 usess->active = 0;
5726
5727 rcu_read_lock();
5728
5729 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5730 ret = ust_app_stop_trace(usess, app);
5731 if (ret < 0) {
5732 /* Continue to next apps even on error */
5733 continue;
5734 }
5735 }
5736
5737 (void) ust_app_flush_session(usess);
5738
5739 rcu_read_unlock();
5740
5741 return 0;
5742 }
5743
5744 /*
5745 * Destroy app UST session.
5746 */
5747 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
5748 {
5749 int ret = 0;
5750 struct lttng_ht_iter iter;
5751 struct ust_app *app;
5752
5753 DBG("Destroy all UST traces");
5754
5755 rcu_read_lock();
5756
5757 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
5758 ret = destroy_trace(usess, app);
5759 if (ret < 0) {
5760 /* Continue to next apps even on error */
5761 continue;
5762 }
5763 }
5764
5765 rcu_read_unlock();
5766
5767 return 0;
5768 }
5769
5770 /* The ua_sess lock must be held by the caller. */
5771 static
5772 int find_or_create_ust_app_channel(
5773 struct ltt_ust_session *usess,
5774 struct ust_app_session *ua_sess,
5775 struct ust_app *app,
5776 struct ltt_ust_channel *uchan,
5777 struct ust_app_channel **ua_chan)
5778 {
5779 int ret = 0;
5780 struct lttng_ht_iter iter;
5781 struct lttng_ht_node_str *ua_chan_node;
5782
5783 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &iter);
5784 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
5785 if (ua_chan_node) {
5786 *ua_chan = caa_container_of(ua_chan_node,
5787 struct ust_app_channel, node);
5788 goto end;
5789 }
5790
5791 ret = ust_app_channel_create(usess, ua_sess, uchan, app, ua_chan);
5792 if (ret) {
5793 goto end;
5794 }
5795 end:
5796 return ret;
5797 }
5798
5799 static
5800 int ust_app_channel_synchronize_event(struct ust_app_channel *ua_chan,
5801 struct ltt_ust_event *uevent,
5802 struct ust_app *app)
5803 {
5804 int ret = 0;
5805 struct ust_app_event *ua_event = NULL;
5806
5807 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
5808 uevent->filter, uevent->attr.loglevel, uevent->exclusion);
5809 if (!ua_event) {
5810 ret = create_ust_app_event(ua_chan, uevent, app);
5811 if (ret < 0) {
5812 goto end;
5813 }
5814 } else {
5815 if (ua_event->enabled != uevent->enabled) {
5816 ret = uevent->enabled ?
5817 enable_ust_app_event(ua_event, app) :
5818 disable_ust_app_event(ua_event, app);
5819 }
5820 }
5821
5822 end:
5823 return ret;
5824 }
5825
5826 /* Called with RCU read-side lock held. */
5827 static
5828 void ust_app_synchronize_event_notifier_rules(struct ust_app *app)
5829 {
5830 int ret = 0;
5831 enum lttng_error_code ret_code;
5832 enum lttng_trigger_status t_status;
5833 struct lttng_ht_iter app_trigger_iter;
5834 struct lttng_triggers *triggers = NULL;
5835 struct ust_app_event_notifier_rule *event_notifier_rule;
5836 unsigned int count, i;
5837
5838 ASSERT_RCU_READ_LOCKED();
5839
5840 if (!ust_app_supports_notifiers(app)) {
5841 goto end;
5842 }
5843
5844 /*
5845 * Currrently, registering or unregistering a trigger with an
5846 * event rule condition causes a full synchronization of the event
5847 * notifiers.
5848 *
5849 * The first step attempts to add an event notifier for all registered
5850 * triggers that apply to the user space tracers. Then, the
5851 * application's event notifiers rules are all checked against the list
5852 * of registered triggers. Any event notifier that doesn't have a
5853 * matching trigger can be assumed to have been disabled.
5854 *
5855 * All of this is inefficient, but is put in place to get the feature
5856 * rolling as it is simpler at this moment. It will be optimized Soon™
5857 * to allow the state of enabled
5858 * event notifiers to be synchronized in a piece-wise way.
5859 */
5860
5861 /* Get all triggers using uid 0 (root) */
5862 ret_code = notification_thread_command_list_triggers(
5863 the_notification_thread_handle, 0, &triggers);
5864 if (ret_code != LTTNG_OK) {
5865 goto end;
5866 }
5867
5868 LTTNG_ASSERT(triggers);
5869
5870 t_status = lttng_triggers_get_count(triggers, &count);
5871 if (t_status != LTTNG_TRIGGER_STATUS_OK) {
5872 goto end;
5873 }
5874
5875 for (i = 0; i < count; i++) {
5876 struct lttng_condition *condition;
5877 struct lttng_event_rule *event_rule;
5878 struct lttng_trigger *trigger;
5879 const struct ust_app_event_notifier_rule *looked_up_event_notifier_rule;
5880 enum lttng_condition_status condition_status;
5881 uint64_t token;
5882
5883 trigger = lttng_triggers_borrow_mutable_at_index(triggers, i);
5884 LTTNG_ASSERT(trigger);
5885
5886 token = lttng_trigger_get_tracer_token(trigger);
5887 condition = lttng_trigger_get_condition(trigger);
5888
5889 if (lttng_condition_get_type(condition) !=
5890 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES) {
5891 /* Does not apply */
5892 continue;
5893 }
5894
5895 condition_status =
5896 lttng_condition_event_rule_matches_borrow_rule_mutable(
5897 condition, &event_rule);
5898 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
5899
5900 if (lttng_event_rule_get_domain_type(event_rule) == LTTNG_DOMAIN_KERNEL) {
5901 /* Skip kernel related triggers. */
5902 continue;
5903 }
5904
5905 /*
5906 * Find or create the associated token event rule. The caller
5907 * holds the RCU read lock, so this is safe to call without
5908 * explicitly acquiring it here.
5909 */
5910 looked_up_event_notifier_rule = find_ust_app_event_notifier_rule(
5911 app->token_to_event_notifier_rule_ht, token);
5912 if (!looked_up_event_notifier_rule) {
5913 ret = create_ust_app_event_notifier_rule(trigger, app);
5914 if (ret < 0) {
5915 goto end;
5916 }
5917 }
5918 }
5919
5920 rcu_read_lock();
5921 /* Remove all unknown event sources from the app. */
5922 cds_lfht_for_each_entry (app->token_to_event_notifier_rule_ht->ht,
5923 &app_trigger_iter.iter, event_notifier_rule,
5924 node.node) {
5925 const uint64_t app_token = event_notifier_rule->token;
5926 bool found = false;
5927
5928 /*
5929 * Check if the app event trigger still exists on the
5930 * notification side.
5931 */
5932 for (i = 0; i < count; i++) {
5933 uint64_t notification_thread_token;
5934 const struct lttng_trigger *trigger =
5935 lttng_triggers_get_at_index(
5936 triggers, i);
5937
5938 LTTNG_ASSERT(trigger);
5939
5940 notification_thread_token =
5941 lttng_trigger_get_tracer_token(trigger);
5942
5943 if (notification_thread_token == app_token) {
5944 found = true;
5945 break;
5946 }
5947 }
5948
5949 if (found) {
5950 /* Still valid. */
5951 continue;
5952 }
5953
5954 /*
5955 * This trigger was unregistered, disable it on the tracer's
5956 * side.
5957 */
5958 ret = lttng_ht_del(app->token_to_event_notifier_rule_ht,
5959 &app_trigger_iter);
5960 LTTNG_ASSERT(ret == 0);
5961
5962 /* Callee logs errors. */
5963 (void) disable_ust_object(app, event_notifier_rule->obj);
5964
5965 delete_ust_app_event_notifier_rule(
5966 app->sock, event_notifier_rule, app);
5967 }
5968
5969 rcu_read_unlock();
5970
5971 end:
5972 lttng_triggers_destroy(triggers);
5973 return;
5974 }
5975
5976 /*
5977 * RCU read lock must be held by the caller.
5978 */
5979 static
5980 void ust_app_synchronize_all_channels(struct ltt_ust_session *usess,
5981 struct ust_app_session *ua_sess,
5982 struct ust_app *app)
5983 {
5984 int ret = 0;
5985 struct cds_lfht_iter uchan_iter;
5986 struct ltt_ust_channel *uchan;
5987
5988 LTTNG_ASSERT(usess);
5989 LTTNG_ASSERT(ua_sess);
5990 LTTNG_ASSERT(app);
5991 ASSERT_RCU_READ_LOCKED();
5992
5993 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &uchan_iter,
5994 uchan, node.node) {
5995 struct ust_app_channel *ua_chan;
5996 struct cds_lfht_iter uevent_iter;
5997 struct ltt_ust_event *uevent;
5998
5999 /*
6000 * Search for a matching ust_app_channel. If none is found,
6001 * create it. Creating the channel will cause the ua_chan
6002 * structure to be allocated, the channel buffers to be
6003 * allocated (if necessary) and sent to the application, and
6004 * all enabled contexts will be added to the channel.
6005 */
6006 ret = find_or_create_ust_app_channel(usess, ua_sess,
6007 app, uchan, &ua_chan);
6008 if (ret) {
6009 /* Tracer is probably gone or ENOMEM. */
6010 goto end;
6011 }
6012
6013 if (!ua_chan) {
6014 /* ua_chan will be NULL for the metadata channel */
6015 continue;
6016 }
6017
6018 cds_lfht_for_each_entry(uchan->events->ht, &uevent_iter, uevent,
6019 node.node) {
6020 ret = ust_app_channel_synchronize_event(ua_chan,
6021 uevent, app);
6022 if (ret) {
6023 goto end;
6024 }
6025 }
6026
6027 if (ua_chan->enabled != uchan->enabled) {
6028 ret = uchan->enabled ?
6029 enable_ust_app_channel(ua_sess, uchan, app) :
6030 disable_ust_app_channel(ua_sess, ua_chan, app);
6031 if (ret) {
6032 goto end;
6033 }
6034 }
6035 }
6036 end:
6037 return;
6038 }
6039
6040 /*
6041 * The caller must ensure that the application is compatible and is tracked
6042 * by the process attribute trackers.
6043 */
6044 static
6045 void ust_app_synchronize(struct ltt_ust_session *usess,
6046 struct ust_app *app)
6047 {
6048 int ret = 0;
6049 struct ust_app_session *ua_sess = NULL;
6050
6051 /*
6052 * The application's configuration should only be synchronized for
6053 * active sessions.
6054 */
6055 LTTNG_ASSERT(usess->active);
6056
6057 ret = find_or_create_ust_app_session(usess, app, &ua_sess, NULL);
6058 if (ret < 0) {
6059 /* Tracer is probably gone or ENOMEM. */
6060 if (ua_sess) {
6061 destroy_app_session(app, ua_sess);
6062 }
6063 goto end;
6064 }
6065 LTTNG_ASSERT(ua_sess);
6066
6067 pthread_mutex_lock(&ua_sess->lock);
6068 if (ua_sess->deleted) {
6069 goto deleted_session;
6070 }
6071
6072 rcu_read_lock();
6073
6074 ust_app_synchronize_all_channels(usess, ua_sess, app);
6075
6076 /*
6077 * Create the metadata for the application. This returns gracefully if a
6078 * metadata was already set for the session.
6079 *
6080 * The metadata channel must be created after the data channels as the
6081 * consumer daemon assumes this ordering. When interacting with a relay
6082 * daemon, the consumer will use this assumption to send the
6083 * "STREAMS_SENT" message to the relay daemon.
6084 */
6085 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
6086 if (ret < 0) {
6087 ERR("Metadata creation failed for app sock %d for session id %" PRIu64,
6088 app->sock, usess->id);
6089 }
6090
6091 rcu_read_unlock();
6092
6093 deleted_session:
6094 pthread_mutex_unlock(&ua_sess->lock);
6095 end:
6096 return;
6097 }
6098
6099 static
6100 void ust_app_global_destroy(struct ltt_ust_session *usess, struct ust_app *app)
6101 {
6102 struct ust_app_session *ua_sess;
6103
6104 ua_sess = lookup_session_by_app(usess, app);
6105 if (ua_sess == NULL) {
6106 return;
6107 }
6108 destroy_app_session(app, ua_sess);
6109 }
6110
6111 /*
6112 * Add channels/events from UST global domain to registered apps at sock.
6113 *
6114 * Called with session lock held.
6115 * Called with RCU read-side lock held.
6116 */
6117 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
6118 {
6119 LTTNG_ASSERT(usess);
6120 LTTNG_ASSERT(usess->active);
6121 ASSERT_RCU_READ_LOCKED();
6122
6123 DBG2("UST app global update for app sock %d for session id %" PRIu64,
6124 app->sock, usess->id);
6125
6126 if (!app->compatible) {
6127 return;
6128 }
6129 if (trace_ust_id_tracker_lookup(LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID,
6130 usess, app->pid) &&
6131 trace_ust_id_tracker_lookup(
6132 LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID,
6133 usess, app->uid) &&
6134 trace_ust_id_tracker_lookup(
6135 LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID,
6136 usess, app->gid)) {
6137 /*
6138 * Synchronize the application's internal tracing configuration
6139 * and start tracing.
6140 */
6141 ust_app_synchronize(usess, app);
6142 ust_app_start_trace(usess, app);
6143 } else {
6144 ust_app_global_destroy(usess, app);
6145 }
6146 }
6147
6148 /*
6149 * Add all event notifiers to an application.
6150 *
6151 * Called with session lock held.
6152 * Called with RCU read-side lock held.
6153 */
6154 void ust_app_global_update_event_notifier_rules(struct ust_app *app)
6155 {
6156 ASSERT_RCU_READ_LOCKED();
6157
6158 DBG2("UST application global event notifier rules update: app = '%s', pid = %d",
6159 app->name, app->pid);
6160
6161 if (!app->compatible || !ust_app_supports_notifiers(app)) {
6162 return;
6163 }
6164
6165 if (app->event_notifier_group.object == NULL) {
6166 WARN("UST app global update of event notifiers for app skipped since communication handle is null: app = '%s', pid = %d",
6167 app->name, app->pid);
6168 return;
6169 }
6170
6171 ust_app_synchronize_event_notifier_rules(app);
6172 }
6173
6174 /*
6175 * Called with session lock held.
6176 */
6177 void ust_app_global_update_all(struct ltt_ust_session *usess)
6178 {
6179 struct lttng_ht_iter iter;
6180 struct ust_app *app;
6181
6182 rcu_read_lock();
6183 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6184 ust_app_global_update(usess, app);
6185 }
6186 rcu_read_unlock();
6187 }
6188
6189 void ust_app_global_update_all_event_notifier_rules(void)
6190 {
6191 struct lttng_ht_iter iter;
6192 struct ust_app *app;
6193
6194 rcu_read_lock();
6195 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6196 ust_app_global_update_event_notifier_rules(app);
6197 }
6198
6199 rcu_read_unlock();
6200 }
6201
6202 /*
6203 * Add context to a specific channel for global UST domain.
6204 */
6205 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
6206 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
6207 {
6208 int ret = 0;
6209 struct lttng_ht_node_str *ua_chan_node;
6210 struct lttng_ht_iter iter, uiter;
6211 struct ust_app_channel *ua_chan = NULL;
6212 struct ust_app_session *ua_sess;
6213 struct ust_app *app;
6214
6215 LTTNG_ASSERT(usess->active);
6216
6217 rcu_read_lock();
6218 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
6219 if (!app->compatible) {
6220 /*
6221 * TODO: In time, we should notice the caller of this error by
6222 * telling him that this is a version error.
6223 */
6224 continue;
6225 }
6226 ua_sess = lookup_session_by_app(usess, app);
6227 if (ua_sess == NULL) {
6228 continue;
6229 }
6230
6231 pthread_mutex_lock(&ua_sess->lock);
6232
6233 if (ua_sess->deleted) {
6234 pthread_mutex_unlock(&ua_sess->lock);
6235 continue;
6236 }
6237
6238 /* Lookup channel in the ust app session */
6239 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
6240 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
6241 if (ua_chan_node == NULL) {
6242 goto next_app;
6243 }
6244 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
6245 node);
6246 ret = create_ust_app_channel_context(ua_chan, &uctx->ctx, app);
6247 if (ret < 0) {
6248 goto next_app;
6249 }
6250 next_app:
6251 pthread_mutex_unlock(&ua_sess->lock);
6252 }
6253
6254 rcu_read_unlock();
6255 return ret;
6256 }
6257
6258 /*
6259 * Receive registration and populate the given msg structure.
6260 *
6261 * On success return 0 else a negative value returned by the ustctl call.
6262 */
6263 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
6264 {
6265 int ret;
6266 uint32_t pid, ppid, uid, gid;
6267
6268 LTTNG_ASSERT(msg);
6269
6270 ret = lttng_ust_ctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
6271 &pid, &ppid, &uid, &gid,
6272 &msg->bits_per_long,
6273 &msg->uint8_t_alignment,
6274 &msg->uint16_t_alignment,
6275 &msg->uint32_t_alignment,
6276 &msg->uint64_t_alignment,
6277 &msg->long_alignment,
6278 &msg->byte_order,
6279 msg->name);
6280 if (ret < 0) {
6281 switch (-ret) {
6282 case EPIPE:
6283 case ECONNRESET:
6284 case LTTNG_UST_ERR_EXITING:
6285 DBG3("UST app recv reg message failed. Application died");
6286 break;
6287 case LTTNG_UST_ERR_UNSUP_MAJOR:
6288 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
6289 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
6290 LTTNG_UST_ABI_MINOR_VERSION);
6291 break;
6292 default:
6293 ERR("UST app recv reg message failed with ret %d", ret);
6294 break;
6295 }
6296 goto error;
6297 }
6298 msg->pid = (pid_t) pid;
6299 msg->ppid = (pid_t) ppid;
6300 msg->uid = (uid_t) uid;
6301 msg->gid = (gid_t) gid;
6302
6303 error:
6304 return ret;
6305 }
6306
6307 /*
6308 * Return a ust app session object using the application object and the
6309 * session object descriptor has a key. If not found, NULL is returned.
6310 * A RCU read side lock MUST be acquired when calling this function.
6311 */
6312 static struct ust_app_session *find_session_by_objd(struct ust_app *app,
6313 int objd)
6314 {
6315 struct lttng_ht_node_ulong *node;
6316 struct lttng_ht_iter iter;
6317 struct ust_app_session *ua_sess = NULL;
6318
6319 LTTNG_ASSERT(app);
6320 ASSERT_RCU_READ_LOCKED();
6321
6322 lttng_ht_lookup(app->ust_sessions_objd, (void *)((unsigned long) objd), &iter);
6323 node = lttng_ht_iter_get_node_ulong(&iter);
6324 if (node == NULL) {
6325 DBG2("UST app session find by objd %d not found", objd);
6326 goto error;
6327 }
6328
6329 ua_sess = caa_container_of(node, struct ust_app_session, ust_objd_node);
6330
6331 error:
6332 return ua_sess;
6333 }
6334
6335 /*
6336 * Return a ust app channel object using the application object and the channel
6337 * object descriptor has a key. If not found, NULL is returned. A RCU read side
6338 * lock MUST be acquired before calling this function.
6339 */
6340 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
6341 int objd)
6342 {
6343 struct lttng_ht_node_ulong *node;
6344 struct lttng_ht_iter iter;
6345 struct ust_app_channel *ua_chan = NULL;
6346
6347 LTTNG_ASSERT(app);
6348 ASSERT_RCU_READ_LOCKED();
6349
6350 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
6351 node = lttng_ht_iter_get_node_ulong(&iter);
6352 if (node == NULL) {
6353 DBG2("UST app channel find by objd %d not found", objd);
6354 goto error;
6355 }
6356
6357 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
6358
6359 error:
6360 return ua_chan;
6361 }
6362
6363 /*
6364 * Reply to a register channel notification from an application on the notify
6365 * socket. The channel metadata is also created.
6366 *
6367 * The session UST registry lock is acquired in this function.
6368 *
6369 * On success 0 is returned else a negative value.
6370 */
6371 static int handle_app_register_channel_notification(int sock,
6372 int cobjd,
6373 struct lttng_ust_ctl_field *raw_context_fields,
6374 size_t context_field_count)
6375 {
6376 int ret, ret_code = 0;
6377 uint32_t chan_id;
6378 uint64_t chan_reg_key;
6379 struct ust_app *app;
6380 struct ust_app_channel *ua_chan;
6381 struct ust_app_session *ua_sess;
6382 auto ust_ctl_context_fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(
6383 raw_context_fields);
6384
6385 lttng::urcu::read_lock_guard read_lock_guard;
6386
6387 /* Lookup application. If not found, there is a code flow error. */
6388 app = find_app_by_notify_sock(sock);
6389 if (!app) {
6390 DBG("Application socket %d is being torn down. Abort event notify",
6391 sock);
6392 return -1;
6393 }
6394
6395 /* Lookup channel by UST object descriptor. */
6396 ua_chan = find_channel_by_objd(app, cobjd);
6397 if (!ua_chan) {
6398 DBG("Application channel is being torn down. Abort event notify");
6399 return 0;
6400 }
6401
6402 LTTNG_ASSERT(ua_chan->session);
6403 ua_sess = ua_chan->session;
6404
6405 /* Get right session registry depending on the session buffer type. */
6406 auto locked_registry_session = get_locked_session_registry(ua_sess);
6407 if (!locked_registry_session) {
6408 DBG("Application session is being torn down. Abort event notify");
6409 return 0;
6410 };
6411
6412 /* Depending on the buffer type, a different channel key is used. */
6413 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6414 chan_reg_key = ua_chan->tracing_channel_id;
6415 } else {
6416 chan_reg_key = ua_chan->key;
6417 }
6418
6419 auto& ust_reg_chan = locked_registry_session->get_channel(chan_reg_key);
6420
6421 /* Channel id is set during the object creation. */
6422 chan_id = ust_reg_chan.id;
6423
6424 /*
6425 * The application returns the typing information of the channel's
6426 * context fields. In per-PID buffering mode, this is the first and only
6427 * time we get this information. It is our chance to finalize the
6428 * initialiation of the channel and serialize it's layout's description
6429 * to the trace's metadata.
6430 *
6431 * However, in per-UID buffering mode, every application will provide
6432 * this information (redundantly). The first time will allow us to
6433 * complete the initialization. The following times, we simply validate
6434 * that all apps provide the same typing for the context fields as a
6435 * sanity check.
6436 */
6437 lst::type::cuptr context_fields = lttng::make_unique<lst::structure_type>(0,
6438 lsu::create_trace_fields_from_ust_ctl_fields(*locked_registry_session,
6439 ust_ctl_context_fields.get(), context_field_count));
6440
6441 if (!ust_reg_chan.is_registered()) {
6442 ust_reg_chan.set_context(std::move(context_fields));
6443 } else {
6444 /*
6445 * Validate that the context fields match between
6446 * registry and newcoming application.
6447 */
6448 if (ust_reg_chan.get_context() != *context_fields) {
6449 ERR("Registering application channel due to context field mismatch: pid = %d, sock = %d",
6450 app->pid, app->sock);
6451 ret_code = -EINVAL;
6452 goto reply;
6453 }
6454 }
6455
6456 /* Append to metadata */
6457 if (!ust_reg_chan._metadata_dumped) {
6458 /*ret_code = ust_metadata_channel_statedump(registry, ust_reg_chan);*/
6459 if (ret_code) {
6460 ERR("Error appending channel metadata (errno = %d)", ret_code);
6461 goto reply;
6462 }
6463 }
6464
6465 reply:
6466 DBG3("UST app replying to register channel key %" PRIu64
6467 " with id %u, ret = %d", chan_reg_key, chan_id,
6468 ret_code);
6469
6470 ret = lttng_ust_ctl_reply_register_channel(sock, chan_id,
6471 ust_reg_chan.header_type == lst::stream_class::header_type::COMPACT ?
6472 LTTNG_UST_CTL_CHANNEL_HEADER_COMPACT :
6473 LTTNG_UST_CTL_CHANNEL_HEADER_LARGE,
6474 ret_code);
6475 if (ret < 0) {
6476 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6477 DBG3("UST app reply channel failed. Application died: pid = %d, sock = %d",
6478 app->pid, app->sock);
6479 } else if (ret == -EAGAIN) {
6480 WARN("UST app reply channel failed. Communication time out: pid = %d, sock = %d",
6481 app->pid, app->sock);
6482 } else {
6483 ERR("UST app reply channel failed with ret %d: pid = %d, sock = %d",
6484 ret, app->pid, app->sock);
6485 }
6486
6487 return ret;
6488 }
6489
6490 /* This channel registry's registration is completed. */
6491 ust_reg_chan.set_as_registered();
6492
6493 return ret;
6494 }
6495
6496 /*
6497 * Add event to the UST channel registry. When the event is added to the
6498 * registry, the metadata is also created. Once done, this replies to the
6499 * application with the appropriate error code.
6500 *
6501 * The session UST registry lock is acquired in the function.
6502 *
6503 * On success 0 is returned else a negative value.
6504 */
6505 static int add_event_ust_registry(int sock, int sobjd, int cobjd, const char *name,
6506 char *raw_signature, size_t nr_fields, struct lttng_ust_ctl_field *raw_fields,
6507 int loglevel_value, char *raw_model_emf_uri)
6508 {
6509 int ret, ret_code;
6510 uint32_t event_id = 0;
6511 uint64_t chan_reg_key;
6512 struct ust_app *app;
6513 struct ust_app_channel *ua_chan;
6514 struct ust_app_session *ua_sess;
6515 lttng::urcu::read_lock_guard rcu_lock;
6516 auto signature = lttng::make_unique_wrapper<char, lttng::free>(raw_signature);
6517 auto fields = lttng::make_unique_wrapper<lttng_ust_ctl_field, lttng::free>(raw_fields);
6518 auto model_emf_uri = lttng::make_unique_wrapper<char, lttng::free>(raw_model_emf_uri);
6519
6520 /* Lookup application. If not found, there is a code flow error. */
6521 app = find_app_by_notify_sock(sock);
6522 if (!app) {
6523 DBG("Application socket %d is being torn down. Abort event notify",
6524 sock);
6525 return -1;
6526 }
6527
6528 /* Lookup channel by UST object descriptor. */
6529 ua_chan = find_channel_by_objd(app, cobjd);
6530 if (!ua_chan) {
6531 DBG("Application channel is being torn down. Abort event notify");
6532 return 0;
6533 }
6534
6535 LTTNG_ASSERT(ua_chan->session);
6536 ua_sess = ua_chan->session;
6537
6538 if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) {
6539 chan_reg_key = ua_chan->tracing_channel_id;
6540 } else {
6541 chan_reg_key = ua_chan->key;
6542 }
6543
6544 {
6545 auto locked_registry = get_locked_session_registry(ua_sess);
6546 if (locked_registry) {
6547 /*
6548 * From this point on, this call acquires the ownership of the signature,
6549 * fields and model_emf_uri meaning any free are done inside it if needed.
6550 * These three variables MUST NOT be read/write after this.
6551 */
6552 try {
6553 auto& channel = locked_registry->get_channel(chan_reg_key);
6554
6555 /* event_id is set on success. */
6556 channel.add_event(sobjd, cobjd, name, signature.get(),
6557 lsu::create_trace_fields_from_ust_ctl_fields(
6558 *locked_registry, fields.get(),
6559 nr_fields),
6560 loglevel_value,
6561 model_emf_uri.get() ?
6562 nonstd::optional<std::string>(
6563 model_emf_uri.get()) :
6564 nonstd::nullopt,
6565 ua_sess->buffer_type, *app, event_id);
6566 ret_code = 0;
6567 } catch (const std::exception& ex) {
6568 ERR("Failed to add event `%s` to registry session: %s", name,
6569 ex.what());
6570 /* Inform the application of the error; don't return directly. */
6571 ret_code = -EINVAL;
6572 }
6573 } else {
6574 DBG("Application session is being torn down. Abort event notify");
6575 return 0;
6576 }
6577 }
6578
6579 /*
6580 * The return value is returned to ustctl so in case of an error, the
6581 * application can be notified. In case of an error, it's important not to
6582 * return a negative error or else the application will get closed.
6583 */
6584 ret = lttng_ust_ctl_reply_register_event(sock, event_id, ret_code);
6585 if (ret < 0) {
6586 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6587 DBG3("UST app reply event failed. Application died: pid = %d, sock = %d.",
6588 app->pid, app->sock);
6589 } else if (ret == -EAGAIN) {
6590 WARN("UST app reply event failed. Communication time out: pid = %d, sock = %d",
6591 app->pid, app->sock);
6592 } else {
6593 ERR("UST app reply event failed with ret %d: pid = %d, sock = %d",
6594 ret, app->pid, app->sock);
6595 }
6596 /*
6597 * No need to wipe the create event since the application socket will
6598 * get close on error hence cleaning up everything by itself.
6599 */
6600 return ret;
6601 }
6602
6603 DBG3("UST registry event %s with id %" PRId32 " added successfully",
6604 name, event_id);
6605 return ret;
6606 }
6607
6608 /*
6609 * Add enum to the UST session registry. Once done, this replies to the
6610 * application with the appropriate error code.
6611 *
6612 * The session UST registry lock is acquired within this function.
6613 *
6614 * On success 0 is returned else a negative value.
6615 */
6616 static int add_enum_ust_registry(int sock, int sobjd, const char *name,
6617 struct lttng_ust_ctl_enum_entry *raw_entries, size_t nr_entries)
6618 {
6619 int ret = 0;
6620 struct ust_app *app;
6621 struct ust_app_session *ua_sess;
6622 uint64_t enum_id = -1ULL;
6623 lttng::urcu::read_lock_guard read_lock_guard;
6624 auto entries = lttng::make_unique_wrapper<struct lttng_ust_ctl_enum_entry, lttng::free>(
6625 raw_entries);
6626
6627 /* Lookup application. If not found, there is a code flow error. */
6628 app = find_app_by_notify_sock(sock);
6629 if (!app) {
6630 /* Return an error since this is not an error */
6631 DBG("Application socket %d is being torn down. Aborting enum registration",
6632 sock);
6633 return -1;
6634 }
6635
6636 /* Lookup session by UST object descriptor. */
6637 ua_sess = find_session_by_objd(app, sobjd);
6638 if (!ua_sess) {
6639 /* Return an error since this is not an error */
6640 DBG("Application session is being torn down (session not found). Aborting enum registration.");
6641 return 0;
6642 }
6643
6644 auto locked_registry = get_locked_session_registry(ua_sess);
6645 if (!locked_registry) {
6646 DBG("Application session is being torn down (registry not found). Aborting enum registration.");
6647 return 0;
6648 }
6649
6650 /*
6651 * From this point on, the callee acquires the ownership of
6652 * entries. The variable entries MUST NOT be read/written after
6653 * call.
6654 */
6655 int application_reply_code;
6656 try {
6657 locked_registry->create_or_find_enum(
6658 sobjd, name, entries.release(), nr_entries, &enum_id);
6659 application_reply_code = 0;
6660 } catch (const std::exception& ex) {
6661 ERR("%s: %s", fmt::format("Failed to create or find enumeration provided by application: app = {}, enumeration name = {}",
6662 *app, name).c_str(), ex.what());
6663 application_reply_code = -1;
6664 }
6665
6666 /*
6667 * The return value is returned to ustctl so in case of an error, the
6668 * application can be notified. In case of an error, it's important not to
6669 * return a negative error or else the application will get closed.
6670 */
6671 ret = lttng_ust_ctl_reply_register_enum(sock, enum_id, application_reply_code);
6672 if (ret < 0) {
6673 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6674 DBG3("UST app reply enum failed. Application died: pid = %d, sock = %d",
6675 app->pid, app->sock);
6676 } else if (ret == -EAGAIN) {
6677 WARN("UST app reply enum failed. Communication time out: pid = %d, sock = %d",
6678 app->pid, app->sock);
6679 } else {
6680 ERR("UST app reply enum failed with ret %d: pid = %d, sock = %d",
6681 ret, app->pid, app->sock);
6682 }
6683 /*
6684 * No need to wipe the create enum since the application socket will
6685 * get close on error hence cleaning up everything by itself.
6686 */
6687 return ret;
6688 }
6689
6690 DBG3("UST registry enum %s added successfully or already found", name);
6691 return 0;
6692 }
6693
6694 /*
6695 * Handle application notification through the given notify socket.
6696 *
6697 * Return 0 on success or else a negative value.
6698 */
6699 int ust_app_recv_notify(int sock)
6700 {
6701 int ret;
6702 enum lttng_ust_ctl_notify_cmd cmd;
6703
6704 DBG3("UST app receiving notify from sock %d", sock);
6705
6706 ret = lttng_ust_ctl_recv_notify(sock, &cmd);
6707 if (ret < 0) {
6708 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6709 DBG3("UST app recv notify failed. Application died: sock = %d",
6710 sock);
6711 } else if (ret == -EAGAIN) {
6712 WARN("UST app recv notify failed. Communication time out: sock = %d",
6713 sock);
6714 } else {
6715 ERR("UST app recv notify failed with ret %d: sock = %d",
6716 ret, sock);
6717 }
6718 goto error;
6719 }
6720
6721 switch (cmd) {
6722 case LTTNG_UST_CTL_NOTIFY_CMD_EVENT:
6723 {
6724 int sobjd, cobjd, loglevel_value;
6725 char name[LTTNG_UST_ABI_SYM_NAME_LEN], *sig, *model_emf_uri;
6726 size_t nr_fields;
6727 struct lttng_ust_ctl_field *fields;
6728
6729 DBG2("UST app ustctl register event received");
6730
6731 ret = lttng_ust_ctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel_value,
6732 &sig, &nr_fields, &fields, &model_emf_uri);
6733 if (ret < 0) {
6734 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6735 DBG3("UST app recv event failed. Application died: sock = %d",
6736 sock);
6737 } else if (ret == -EAGAIN) {
6738 WARN("UST app recv event failed. Communication time out: sock = %d",
6739 sock);
6740 } else {
6741 ERR("UST app recv event failed with ret %d: sock = %d",
6742 ret, sock);
6743 }
6744 goto error;
6745 }
6746
6747 {
6748 lttng::urcu::read_lock_guard rcu_lock;
6749 const struct ust_app *app = find_app_by_notify_sock(sock);
6750 if (!app) {
6751 DBG("Application socket %d is being torn down. Abort event notify", sock);
6752 ret = -1;
6753 goto error;
6754 }
6755 }
6756
6757 if ((!fields && nr_fields > 0) || (fields && nr_fields == 0)) {
6758 ERR("Invalid return value from lttng_ust_ctl_recv_register_event: fields = %p, nr_fields = %zu",
6759 fields, nr_fields);
6760 ret = -1;
6761 free(fields);
6762 goto error;
6763 }
6764
6765 /*
6766 * Add event to the UST registry coming from the notify socket. This
6767 * call will free if needed the sig, fields and model_emf_uri. This
6768 * code path loses the ownsership of these variables and transfer them
6769 * to the this function.
6770 */
6771 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
6772 fields, loglevel_value, model_emf_uri);
6773 if (ret < 0) {
6774 goto error;
6775 }
6776
6777 break;
6778 }
6779 case LTTNG_UST_CTL_NOTIFY_CMD_CHANNEL:
6780 {
6781 int sobjd, cobjd;
6782 size_t field_count;
6783 struct lttng_ust_ctl_field *context_fields;
6784
6785 DBG2("UST app ustctl register channel received");
6786
6787 ret = lttng_ust_ctl_recv_register_channel(
6788 sock, &sobjd, &cobjd, &field_count, &context_fields);
6789 if (ret < 0) {
6790 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6791 DBG3("UST app recv channel failed. Application died: sock = %d",
6792 sock);
6793 } else if (ret == -EAGAIN) {
6794 WARN("UST app recv channel failed. Communication time out: sock = %d",
6795 sock);
6796 } else {
6797 ERR("UST app recv channel failed with ret %d: sock = %d", ret,
6798 sock);
6799 }
6800 goto error;
6801 }
6802
6803 /*
6804 * The fields ownership are transfered to this function call meaning
6805 * that if needed it will be freed. After this, it's invalid to access
6806 * fields or clean them up.
6807 */
6808 ret = handle_app_register_channel_notification(sock, cobjd, context_fields, field_count);
6809 if (ret < 0) {
6810 goto error;
6811 }
6812
6813 break;
6814 }
6815 case LTTNG_UST_CTL_NOTIFY_CMD_ENUM:
6816 {
6817 int sobjd;
6818 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
6819 size_t nr_entries;
6820 struct lttng_ust_ctl_enum_entry *entries;
6821
6822 DBG2("UST app ustctl register enum received");
6823
6824 ret = lttng_ust_ctl_recv_register_enum(sock, &sobjd, name,
6825 &entries, &nr_entries);
6826 if (ret < 0) {
6827 if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) {
6828 DBG3("UST app recv enum failed. Application died: sock = %d",
6829 sock);
6830 } else if (ret == -EAGAIN) {
6831 WARN("UST app recv enum failed. Communication time out: sock = %d",
6832 sock);
6833 } else {
6834 ERR("UST app recv enum failed with ret %d: sock = %d",
6835 ret, sock);
6836 }
6837 goto error;
6838 }
6839
6840 /* Callee assumes ownership of entries. */
6841 ret = add_enum_ust_registry(sock, sobjd, name,
6842 entries, nr_entries);
6843 if (ret < 0) {
6844 goto error;
6845 }
6846
6847 break;
6848 }
6849 default:
6850 /* Should NEVER happen. */
6851 abort();
6852 }
6853
6854 error:
6855 return ret;
6856 }
6857
6858 /*
6859 * Once the notify socket hangs up, this is called. First, it tries to find the
6860 * corresponding application. On failure, the call_rcu to close the socket is
6861 * executed. If an application is found, it tries to delete it from the notify
6862 * socket hash table. Whathever the result, it proceeds to the call_rcu.
6863 *
6864 * Note that an object needs to be allocated here so on ENOMEM failure, the
6865 * call RCU is not done but the rest of the cleanup is.
6866 */
6867 void ust_app_notify_sock_unregister(int sock)
6868 {
6869 int err_enomem = 0;
6870 struct lttng_ht_iter iter;
6871 struct ust_app *app;
6872 struct ust_app_notify_sock_obj *obj;
6873
6874 LTTNG_ASSERT(sock >= 0);
6875
6876 rcu_read_lock();
6877
6878 obj = zmalloc<ust_app_notify_sock_obj>();
6879 if (!obj) {
6880 /*
6881 * An ENOMEM is kind of uncool. If this strikes we continue the
6882 * procedure but the call_rcu will not be called. In this case, we
6883 * accept the fd leak rather than possibly creating an unsynchronized
6884 * state between threads.
6885 *
6886 * TODO: The notify object should be created once the notify socket is
6887 * registered and stored independantely from the ust app object. The
6888 * tricky part is to synchronize the teardown of the application and
6889 * this notify object. Let's keep that in mind so we can avoid this
6890 * kind of shenanigans with ENOMEM in the teardown path.
6891 */
6892 err_enomem = 1;
6893 } else {
6894 obj->fd = sock;
6895 }
6896
6897 DBG("UST app notify socket unregister %d", sock);
6898
6899 /*
6900 * Lookup application by notify socket. If this fails, this means that the
6901 * hash table delete has already been done by the application
6902 * unregistration process so we can safely close the notify socket in a
6903 * call RCU.
6904 */
6905 app = find_app_by_notify_sock(sock);
6906 if (!app) {
6907 goto close_socket;
6908 }
6909
6910 iter.iter.node = &app->notify_sock_n.node;
6911
6912 /*
6913 * Whatever happens here either we fail or succeed, in both cases we have
6914 * to close the socket after a grace period to continue to the call RCU
6915 * here. If the deletion is successful, the application is not visible
6916 * anymore by other threads and is it fails it means that it was already
6917 * deleted from the hash table so either way we just have to close the
6918 * socket.
6919 */
6920 (void) lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
6921
6922 close_socket:
6923 rcu_read_unlock();
6924
6925 /*
6926 * Close socket after a grace period to avoid for the socket to be reused
6927 * before the application object is freed creating potential race between
6928 * threads trying to add unique in the global hash table.
6929 */
6930 if (!err_enomem) {
6931 call_rcu(&obj->head, close_notify_sock_rcu);
6932 }
6933 }
6934
6935 /*
6936 * Destroy a ust app data structure and free its memory.
6937 */
6938 void ust_app_destroy(struct ust_app *app)
6939 {
6940 if (!app) {
6941 return;
6942 }
6943
6944 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
6945 }
6946
6947 /*
6948 * Take a snapshot for a given UST session. The snapshot is sent to the given
6949 * output.
6950 *
6951 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
6952 */
6953 enum lttng_error_code ust_app_snapshot_record(
6954 const struct ltt_ust_session *usess,
6955 const struct consumer_output *output,
6956 uint64_t nb_packets_per_stream)
6957 {
6958 int ret = 0;
6959 enum lttng_error_code status = LTTNG_OK;
6960 struct lttng_ht_iter iter;
6961 struct ust_app *app;
6962 char *trace_path = NULL;
6963
6964 LTTNG_ASSERT(usess);
6965 LTTNG_ASSERT(output);
6966
6967 rcu_read_lock();
6968
6969 switch (usess->buffer_type) {
6970 case LTTNG_BUFFER_PER_UID:
6971 {
6972 struct buffer_reg_uid *reg;
6973
6974 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
6975 struct buffer_reg_channel *buf_reg_chan;
6976 struct consumer_socket *socket;
6977 char pathname[PATH_MAX];
6978 size_t consumer_path_offset = 0;
6979
6980 if (!reg->registry->reg.ust->_metadata_key) {
6981 /* Skip since no metadata is present */
6982 continue;
6983 }
6984
6985 /* Get consumer socket to use to push the metadata.*/
6986 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
6987 usess->consumer);
6988 if (!socket) {
6989 status = LTTNG_ERR_INVALID;
6990 goto error;
6991 }
6992
6993 memset(pathname, 0, sizeof(pathname));
6994 ret = snprintf(pathname, sizeof(pathname),
6995 DEFAULT_UST_TRACE_UID_PATH,
6996 reg->uid, reg->bits_per_long);
6997 if (ret < 0) {
6998 PERROR("snprintf snapshot path");
6999 status = LTTNG_ERR_INVALID;
7000 goto error;
7001 }
7002 /* Free path allowed on previous iteration. */
7003 free(trace_path);
7004 trace_path = setup_channel_trace_path(usess->consumer, pathname,
7005 &consumer_path_offset);
7006 if (!trace_path) {
7007 status = LTTNG_ERR_INVALID;
7008 goto error;
7009 }
7010 /* Add the UST default trace dir to path. */
7011 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7012 buf_reg_chan, node.node) {
7013 status = consumer_snapshot_channel(socket,
7014 buf_reg_chan->consumer_key,
7015 output, 0, &trace_path[consumer_path_offset],
7016 nb_packets_per_stream);
7017 if (status != LTTNG_OK) {
7018 goto error;
7019 }
7020 }
7021 status = consumer_snapshot_channel(socket,
7022 reg->registry->reg.ust->_metadata_key, output, 1,
7023 &trace_path[consumer_path_offset], 0);
7024 if (status != LTTNG_OK) {
7025 goto error;
7026 }
7027 }
7028 break;
7029 }
7030 case LTTNG_BUFFER_PER_PID:
7031 {
7032 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7033 struct consumer_socket *socket;
7034 struct lttng_ht_iter chan_iter;
7035 struct ust_app_channel *ua_chan;
7036 struct ust_app_session *ua_sess;
7037 lsu::registry_session *registry;
7038 char pathname[PATH_MAX];
7039 size_t consumer_path_offset = 0;
7040
7041 ua_sess = lookup_session_by_app(usess, app);
7042 if (!ua_sess) {
7043 /* Session not associated with this app. */
7044 continue;
7045 }
7046
7047 /* Get the right consumer socket for the application. */
7048 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7049 output);
7050 if (!socket) {
7051 status = LTTNG_ERR_INVALID;
7052 goto error;
7053 }
7054
7055 /* Add the UST default trace dir to path. */
7056 memset(pathname, 0, sizeof(pathname));
7057 ret = snprintf(pathname, sizeof(pathname), "%s",
7058 ua_sess->path);
7059 if (ret < 0) {
7060 status = LTTNG_ERR_INVALID;
7061 PERROR("snprintf snapshot path");
7062 goto error;
7063 }
7064 /* Free path allowed on previous iteration. */
7065 free(trace_path);
7066 trace_path = setup_channel_trace_path(usess->consumer, pathname,
7067 &consumer_path_offset);
7068 if (!trace_path) {
7069 status = LTTNG_ERR_INVALID;
7070 goto error;
7071 }
7072 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7073 ua_chan, node.node) {
7074 status = consumer_snapshot_channel(socket,
7075 ua_chan->key, output, 0,
7076 &trace_path[consumer_path_offset],
7077 nb_packets_per_stream);
7078 switch (status) {
7079 case LTTNG_OK:
7080 break;
7081 case LTTNG_ERR_CHAN_NOT_FOUND:
7082 continue;
7083 default:
7084 goto error;
7085 }
7086 }
7087
7088 registry = get_session_registry(ua_sess);
7089 if (!registry) {
7090 DBG("Application session is being torn down. Skip application.");
7091 continue;
7092 }
7093 status = consumer_snapshot_channel(socket,
7094 registry->_metadata_key, output, 1,
7095 &trace_path[consumer_path_offset], 0);
7096 switch (status) {
7097 case LTTNG_OK:
7098 break;
7099 case LTTNG_ERR_CHAN_NOT_FOUND:
7100 continue;
7101 default:
7102 goto error;
7103 }
7104 }
7105 break;
7106 }
7107 default:
7108 abort();
7109 break;
7110 }
7111
7112 error:
7113 free(trace_path);
7114 rcu_read_unlock();
7115 return status;
7116 }
7117
7118 /*
7119 * Return the size taken by one more packet per stream.
7120 */
7121 uint64_t ust_app_get_size_one_more_packet_per_stream(
7122 const struct ltt_ust_session *usess, uint64_t cur_nr_packets)
7123 {
7124 uint64_t tot_size = 0;
7125 struct ust_app *app;
7126 struct lttng_ht_iter iter;
7127
7128 LTTNG_ASSERT(usess);
7129
7130 switch (usess->buffer_type) {
7131 case LTTNG_BUFFER_PER_UID:
7132 {
7133 struct buffer_reg_uid *reg;
7134
7135 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7136 struct buffer_reg_channel *buf_reg_chan;
7137
7138 rcu_read_lock();
7139 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7140 buf_reg_chan, node.node) {
7141 if (cur_nr_packets >= buf_reg_chan->num_subbuf) {
7142 /*
7143 * Don't take channel into account if we
7144 * already grab all its packets.
7145 */
7146 continue;
7147 }
7148 tot_size += buf_reg_chan->subbuf_size * buf_reg_chan->stream_count;
7149 }
7150 rcu_read_unlock();
7151 }
7152 break;
7153 }
7154 case LTTNG_BUFFER_PER_PID:
7155 {
7156 rcu_read_lock();
7157 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7158 struct ust_app_channel *ua_chan;
7159 struct ust_app_session *ua_sess;
7160 struct lttng_ht_iter chan_iter;
7161
7162 ua_sess = lookup_session_by_app(usess, app);
7163 if (!ua_sess) {
7164 /* Session not associated with this app. */
7165 continue;
7166 }
7167
7168 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7169 ua_chan, node.node) {
7170 if (cur_nr_packets >= ua_chan->attr.num_subbuf) {
7171 /*
7172 * Don't take channel into account if we
7173 * already grab all its packets.
7174 */
7175 continue;
7176 }
7177 tot_size += ua_chan->attr.subbuf_size * ua_chan->streams.count;
7178 }
7179 }
7180 rcu_read_unlock();
7181 break;
7182 }
7183 default:
7184 abort();
7185 break;
7186 }
7187
7188 return tot_size;
7189 }
7190
7191 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
7192 struct cds_list_head *buffer_reg_uid_list,
7193 struct consumer_output *consumer, uint64_t uchan_id,
7194 int overwrite, uint64_t *discarded, uint64_t *lost)
7195 {
7196 int ret;
7197 uint64_t consumer_chan_key;
7198
7199 *discarded = 0;
7200 *lost = 0;
7201
7202 ret = buffer_reg_uid_consumer_channel_key(
7203 buffer_reg_uid_list, uchan_id, &consumer_chan_key);
7204 if (ret < 0) {
7205 /* Not found */
7206 ret = 0;
7207 goto end;
7208 }
7209
7210 if (overwrite) {
7211 ret = consumer_get_lost_packets(ust_session_id,
7212 consumer_chan_key, consumer, lost);
7213 } else {
7214 ret = consumer_get_discarded_events(ust_session_id,
7215 consumer_chan_key, consumer, discarded);
7216 }
7217
7218 end:
7219 return ret;
7220 }
7221
7222 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
7223 struct ltt_ust_channel *uchan,
7224 struct consumer_output *consumer, int overwrite,
7225 uint64_t *discarded, uint64_t *lost)
7226 {
7227 int ret = 0;
7228 struct lttng_ht_iter iter;
7229 struct lttng_ht_node_str *ua_chan_node;
7230 struct ust_app *app;
7231 struct ust_app_session *ua_sess;
7232 struct ust_app_channel *ua_chan;
7233
7234 *discarded = 0;
7235 *lost = 0;
7236
7237 rcu_read_lock();
7238 /*
7239 * Iterate over every registered applications. Sum counters for
7240 * all applications containing requested session and channel.
7241 */
7242 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7243 struct lttng_ht_iter uiter;
7244
7245 ua_sess = lookup_session_by_app(usess, app);
7246 if (ua_sess == NULL) {
7247 continue;
7248 }
7249
7250 /* Get channel */
7251 lttng_ht_lookup(ua_sess->channels, (void *) uchan->name, &uiter);
7252 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
7253 /* If the session is found for the app, the channel must be there */
7254 LTTNG_ASSERT(ua_chan_node);
7255
7256 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
7257
7258 if (overwrite) {
7259 uint64_t _lost;
7260
7261 ret = consumer_get_lost_packets(usess->id, ua_chan->key,
7262 consumer, &_lost);
7263 if (ret < 0) {
7264 break;
7265 }
7266 (*lost) += _lost;
7267 } else {
7268 uint64_t _discarded;
7269
7270 ret = consumer_get_discarded_events(usess->id,
7271 ua_chan->key, consumer, &_discarded);
7272 if (ret < 0) {
7273 break;
7274 }
7275 (*discarded) += _discarded;
7276 }
7277 }
7278
7279 rcu_read_unlock();
7280 return ret;
7281 }
7282
7283 static
7284 int ust_app_regenerate_statedump(struct ltt_ust_session *usess,
7285 struct ust_app *app)
7286 {
7287 int ret = 0;
7288 struct ust_app_session *ua_sess;
7289
7290 DBG("Regenerating the metadata for ust app pid %d", app->pid);
7291
7292 rcu_read_lock();
7293
7294 ua_sess = lookup_session_by_app(usess, app);
7295 if (ua_sess == NULL) {
7296 /* The session is in teardown process. Ignore and continue. */
7297 goto end;
7298 }
7299
7300 pthread_mutex_lock(&ua_sess->lock);
7301
7302 if (ua_sess->deleted) {
7303 goto end_unlock;
7304 }
7305
7306 pthread_mutex_lock(&app->sock_lock);
7307 ret = lttng_ust_ctl_regenerate_statedump(app->sock, ua_sess->handle);
7308 pthread_mutex_unlock(&app->sock_lock);
7309
7310 end_unlock:
7311 pthread_mutex_unlock(&ua_sess->lock);
7312
7313 end:
7314 rcu_read_unlock();
7315 health_code_update();
7316 return ret;
7317 }
7318
7319 /*
7320 * Regenerate the statedump for each app in the session.
7321 */
7322 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
7323 {
7324 int ret = 0;
7325 struct lttng_ht_iter iter;
7326 struct ust_app *app;
7327
7328 DBG("Regenerating the metadata for all UST apps");
7329
7330 rcu_read_lock();
7331
7332 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7333 if (!app->compatible) {
7334 continue;
7335 }
7336
7337 ret = ust_app_regenerate_statedump(usess, app);
7338 if (ret < 0) {
7339 /* Continue to the next app even on error */
7340 continue;
7341 }
7342 }
7343
7344 rcu_read_unlock();
7345
7346 return 0;
7347 }
7348
7349 /*
7350 * Rotate all the channels of a session.
7351 *
7352 * Return LTTNG_OK on success or else an LTTng error code.
7353 */
7354 enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
7355 {
7356 int ret;
7357 enum lttng_error_code cmd_ret = LTTNG_OK;
7358 struct lttng_ht_iter iter;
7359 struct ust_app *app;
7360 struct ltt_ust_session *usess = session->ust_session;
7361
7362 LTTNG_ASSERT(usess);
7363
7364 rcu_read_lock();
7365
7366 switch (usess->buffer_type) {
7367 case LTTNG_BUFFER_PER_UID:
7368 {
7369 struct buffer_reg_uid *reg;
7370
7371 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7372 struct buffer_reg_channel *buf_reg_chan;
7373 struct consumer_socket *socket;
7374
7375 /* Get consumer socket to use to push the metadata.*/
7376 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7377 usess->consumer);
7378 if (!socket) {
7379 cmd_ret = LTTNG_ERR_INVALID;
7380 goto error;
7381 }
7382
7383 /* Rotate the data channels. */
7384 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7385 buf_reg_chan, node.node) {
7386 ret = consumer_rotate_channel(socket,
7387 buf_reg_chan->consumer_key,
7388 usess->consumer,
7389 /* is_metadata_channel */ false);
7390 if (ret < 0) {
7391 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7392 goto error;
7393 }
7394 }
7395
7396 /*
7397 * The metadata channel might not be present.
7398 *
7399 * Consumer stream allocation can be done
7400 * asynchronously and can fail on intermediary
7401 * operations (i.e add context) and lead to data
7402 * channels created with no metadata channel.
7403 */
7404 if (!reg->registry->reg.ust->_metadata_key) {
7405 /* Skip since no metadata is present. */
7406 continue;
7407 }
7408
7409 {
7410 auto locked_registry = reg->registry->reg.ust->lock();
7411 (void) push_metadata(locked_registry, usess->consumer);
7412 }
7413
7414 ret = consumer_rotate_channel(socket,
7415 reg->registry->reg.ust->_metadata_key,
7416 usess->consumer,
7417 /* is_metadata_channel */ true);
7418 if (ret < 0) {
7419 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7420 goto error;
7421 }
7422 }
7423 break;
7424 }
7425 case LTTNG_BUFFER_PER_PID:
7426 {
7427 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7428 struct consumer_socket *socket;
7429 struct lttng_ht_iter chan_iter;
7430 struct ust_app_channel *ua_chan;
7431 struct ust_app_session *ua_sess;
7432 lsu::registry_session *registry;
7433
7434 ua_sess = lookup_session_by_app(usess, app);
7435 if (!ua_sess) {
7436 /* Session not associated with this app. */
7437 continue;
7438 }
7439
7440 /* Get the right consumer socket for the application. */
7441 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7442 usess->consumer);
7443 if (!socket) {
7444 cmd_ret = LTTNG_ERR_INVALID;
7445 goto error;
7446 }
7447
7448 registry = get_session_registry(ua_sess);
7449 if (!registry) {
7450 DBG("Application session is being torn down. Skip application.");
7451 continue;
7452 }
7453
7454 /* Rotate the data channels. */
7455 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7456 ua_chan, node.node) {
7457 ret = consumer_rotate_channel(socket,
7458 ua_chan->key,
7459 ua_sess->consumer,
7460 /* is_metadata_channel */ false);
7461 if (ret < 0) {
7462 /* Per-PID buffer and application going away. */
7463 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7464 continue;
7465 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7466 goto error;
7467 }
7468 }
7469
7470 /* Rotate the metadata channel. */
7471 {
7472 auto locked_registry = registry->lock();
7473
7474 (void) push_metadata(locked_registry, usess->consumer);
7475 }
7476 ret = consumer_rotate_channel(socket,
7477 registry->_metadata_key,
7478 ua_sess->consumer,
7479 /* is_metadata_channel */ true);
7480 if (ret < 0) {
7481 /* Per-PID buffer and application going away. */
7482 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND)
7483 continue;
7484 cmd_ret = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
7485 goto error;
7486 }
7487 }
7488 break;
7489 }
7490 default:
7491 abort();
7492 break;
7493 }
7494
7495 cmd_ret = LTTNG_OK;
7496
7497 error:
7498 rcu_read_unlock();
7499 return cmd_ret;
7500 }
7501
7502 enum lttng_error_code ust_app_create_channel_subdirectories(
7503 const struct ltt_ust_session *usess)
7504 {
7505 enum lttng_error_code ret = LTTNG_OK;
7506 struct lttng_ht_iter iter;
7507 enum lttng_trace_chunk_status chunk_status;
7508 char *pathname_index;
7509 int fmt_ret;
7510
7511 LTTNG_ASSERT(usess->current_trace_chunk);
7512 rcu_read_lock();
7513
7514 switch (usess->buffer_type) {
7515 case LTTNG_BUFFER_PER_UID:
7516 {
7517 struct buffer_reg_uid *reg;
7518
7519 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7520 fmt_ret = asprintf(&pathname_index,
7521 DEFAULT_UST_TRACE_DIR "/" DEFAULT_UST_TRACE_UID_PATH "/" DEFAULT_INDEX_DIR,
7522 reg->uid, reg->bits_per_long);
7523 if (fmt_ret < 0) {
7524 ERR("Failed to format channel index directory");
7525 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7526 goto error;
7527 }
7528
7529 /*
7530 * Create the index subdirectory which will take care
7531 * of implicitly creating the channel's path.
7532 */
7533 chunk_status = lttng_trace_chunk_create_subdirectory(
7534 usess->current_trace_chunk,
7535 pathname_index);
7536 free(pathname_index);
7537 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7538 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7539 goto error;
7540 }
7541 }
7542 break;
7543 }
7544 case LTTNG_BUFFER_PER_PID:
7545 {
7546 struct ust_app *app;
7547
7548 /*
7549 * Create the toplevel ust/ directory in case no apps are running.
7550 */
7551 chunk_status = lttng_trace_chunk_create_subdirectory(
7552 usess->current_trace_chunk,
7553 DEFAULT_UST_TRACE_DIR);
7554 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7555 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7556 goto error;
7557 }
7558
7559 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app,
7560 pid_n.node) {
7561 struct ust_app_session *ua_sess;
7562 lsu::registry_session *registry;
7563
7564 ua_sess = lookup_session_by_app(usess, app);
7565 if (!ua_sess) {
7566 /* Session not associated with this app. */
7567 continue;
7568 }
7569
7570 registry = get_session_registry(ua_sess);
7571 if (!registry) {
7572 DBG("Application session is being torn down. Skip application.");
7573 continue;
7574 }
7575
7576 fmt_ret = asprintf(&pathname_index,
7577 DEFAULT_UST_TRACE_DIR "/%s/" DEFAULT_INDEX_DIR,
7578 ua_sess->path);
7579 if (fmt_ret < 0) {
7580 ERR("Failed to format channel index directory");
7581 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7582 goto error;
7583 }
7584 /*
7585 * Create the index subdirectory which will take care
7586 * of implicitly creating the channel's path.
7587 */
7588 chunk_status = lttng_trace_chunk_create_subdirectory(
7589 usess->current_trace_chunk,
7590 pathname_index);
7591 free(pathname_index);
7592 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
7593 ret = LTTNG_ERR_CREATE_DIR_FAIL;
7594 goto error;
7595 }
7596 }
7597 break;
7598 }
7599 default:
7600 abort();
7601 }
7602
7603 ret = LTTNG_OK;
7604 error:
7605 rcu_read_unlock();
7606 return ret;
7607 }
7608
7609 /*
7610 * Clear all the channels of a session.
7611 *
7612 * Return LTTNG_OK on success or else an LTTng error code.
7613 */
7614 enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
7615 {
7616 int ret;
7617 enum lttng_error_code cmd_ret = LTTNG_OK;
7618 struct lttng_ht_iter iter;
7619 struct ust_app *app;
7620 struct ltt_ust_session *usess = session->ust_session;
7621
7622 LTTNG_ASSERT(usess);
7623
7624 rcu_read_lock();
7625
7626 if (usess->active) {
7627 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
7628 cmd_ret = LTTNG_ERR_FATAL;
7629 goto end;
7630 }
7631
7632 switch (usess->buffer_type) {
7633 case LTTNG_BUFFER_PER_UID:
7634 {
7635 struct buffer_reg_uid *reg;
7636
7637 cds_list_for_each_entry(reg, &usess->buffer_reg_uid_list, lnode) {
7638 struct buffer_reg_channel *buf_reg_chan;
7639 struct consumer_socket *socket;
7640
7641 /* Get consumer socket to use to push the metadata.*/
7642 socket = consumer_find_socket_by_bitness(reg->bits_per_long,
7643 usess->consumer);
7644 if (!socket) {
7645 cmd_ret = LTTNG_ERR_INVALID;
7646 goto error_socket;
7647 }
7648
7649 /* Clear the data channels. */
7650 cds_lfht_for_each_entry(reg->registry->channels->ht, &iter.iter,
7651 buf_reg_chan, node.node) {
7652 ret = consumer_clear_channel(socket,
7653 buf_reg_chan->consumer_key);
7654 if (ret < 0) {
7655 goto error;
7656 }
7657 }
7658
7659 {
7660 auto locked_registry = reg->registry->reg.ust->lock();
7661 (void) push_metadata(locked_registry, usess->consumer);
7662 }
7663
7664 /*
7665 * Clear the metadata channel.
7666 * Metadata channel is not cleared per se but we still need to
7667 * perform a rotation operation on it behind the scene.
7668 */
7669 ret = consumer_clear_channel(socket,
7670 reg->registry->reg.ust->_metadata_key);
7671 if (ret < 0) {
7672 goto error;
7673 }
7674 }
7675 break;
7676 }
7677 case LTTNG_BUFFER_PER_PID:
7678 {
7679 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7680 struct consumer_socket *socket;
7681 struct lttng_ht_iter chan_iter;
7682 struct ust_app_channel *ua_chan;
7683 struct ust_app_session *ua_sess;
7684 lsu::registry_session *registry;
7685
7686 ua_sess = lookup_session_by_app(usess, app);
7687 if (!ua_sess) {
7688 /* Session not associated with this app. */
7689 continue;
7690 }
7691
7692 /* Get the right consumer socket for the application. */
7693 socket = consumer_find_socket_by_bitness(app->abi.bits_per_long,
7694 usess->consumer);
7695 if (!socket) {
7696 cmd_ret = LTTNG_ERR_INVALID;
7697 goto error_socket;
7698 }
7699
7700 registry = get_session_registry(ua_sess);
7701 if (!registry) {
7702 DBG("Application session is being torn down. Skip application.");
7703 continue;
7704 }
7705
7706 /* Clear the data channels. */
7707 cds_lfht_for_each_entry(ua_sess->channels->ht, &chan_iter.iter,
7708 ua_chan, node.node) {
7709 ret = consumer_clear_channel(socket, ua_chan->key);
7710 if (ret < 0) {
7711 /* Per-PID buffer and application going away. */
7712 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7713 continue;
7714 }
7715 goto error;
7716 }
7717 }
7718
7719 {
7720 auto locked_registry = registry->lock();
7721 (void) push_metadata(locked_registry, usess->consumer);
7722 }
7723
7724 /*
7725 * Clear the metadata channel.
7726 * Metadata channel is not cleared per se but we still need to
7727 * perform rotation operation on it behind the scene.
7728 */
7729 ret = consumer_clear_channel(socket, registry->_metadata_key);
7730 if (ret < 0) {
7731 /* Per-PID buffer and application going away. */
7732 if (ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7733 continue;
7734 }
7735 goto error;
7736 }
7737 }
7738 break;
7739 }
7740 default:
7741 abort();
7742 break;
7743 }
7744
7745 cmd_ret = LTTNG_OK;
7746 goto end;
7747
7748 error:
7749 switch (-ret) {
7750 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
7751 cmd_ret = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
7752 break;
7753 default:
7754 cmd_ret = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
7755 }
7756
7757 error_socket:
7758 end:
7759 rcu_read_unlock();
7760 return cmd_ret;
7761 }
7762
7763 /*
7764 * This function skips the metadata channel as the begin/end timestamps of a
7765 * metadata packet are useless.
7766 *
7767 * Moreover, opening a packet after a "clear" will cause problems for live
7768 * sessions as it will introduce padding that was not part of the first trace
7769 * chunk. The relay daemon expects the content of the metadata stream of
7770 * successive metadata trace chunks to be strict supersets of one another.
7771 *
7772 * For example, flushing a packet at the beginning of the metadata stream of
7773 * a trace chunk resulting from a "clear" session command will cause the
7774 * size of the metadata stream of the new trace chunk to not match the size of
7775 * the metadata stream of the original chunk. This will confuse the relay
7776 * daemon as the same "offset" in a metadata stream will no longer point
7777 * to the same content.
7778 */
7779 enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
7780 {
7781 enum lttng_error_code ret = LTTNG_OK;
7782 struct lttng_ht_iter iter;
7783 struct ltt_ust_session *usess = session->ust_session;
7784
7785 LTTNG_ASSERT(usess);
7786
7787 rcu_read_lock();
7788
7789 switch (usess->buffer_type) {
7790 case LTTNG_BUFFER_PER_UID:
7791 {
7792 struct buffer_reg_uid *reg;
7793
7794 cds_list_for_each_entry (
7795 reg, &usess->buffer_reg_uid_list, lnode) {
7796 struct buffer_reg_channel *buf_reg_chan;
7797 struct consumer_socket *socket;
7798
7799 socket = consumer_find_socket_by_bitness(
7800 reg->bits_per_long, usess->consumer);
7801 if (!socket) {
7802 ret = LTTNG_ERR_FATAL;
7803 goto error;
7804 }
7805
7806 cds_lfht_for_each_entry(reg->registry->channels->ht,
7807 &iter.iter, buf_reg_chan, node.node) {
7808 const int open_ret =
7809 consumer_open_channel_packets(
7810 socket,
7811 buf_reg_chan->consumer_key);
7812
7813 if (open_ret < 0) {
7814 ret = LTTNG_ERR_UNK;
7815 goto error;
7816 }
7817 }
7818 }
7819 break;
7820 }
7821 case LTTNG_BUFFER_PER_PID:
7822 {
7823 struct ust_app *app;
7824
7825 cds_lfht_for_each_entry (
7826 ust_app_ht->ht, &iter.iter, app, pid_n.node) {
7827 struct consumer_socket *socket;
7828 struct lttng_ht_iter chan_iter;
7829 struct ust_app_channel *ua_chan;
7830 struct ust_app_session *ua_sess;
7831 lsu::registry_session *registry;
7832
7833 ua_sess = lookup_session_by_app(usess, app);
7834 if (!ua_sess) {
7835 /* Session not associated with this app. */
7836 continue;
7837 }
7838
7839 /* Get the right consumer socket for the application. */
7840 socket = consumer_find_socket_by_bitness(
7841 app->abi.bits_per_long, usess->consumer);
7842 if (!socket) {
7843 ret = LTTNG_ERR_FATAL;
7844 goto error;
7845 }
7846
7847 registry = get_session_registry(ua_sess);
7848 if (!registry) {
7849 DBG("Application session is being torn down. Skip application.");
7850 continue;
7851 }
7852
7853 cds_lfht_for_each_entry(ua_sess->channels->ht,
7854 &chan_iter.iter, ua_chan, node.node) {
7855 const int open_ret =
7856 consumer_open_channel_packets(
7857 socket,
7858 ua_chan->key);
7859
7860 if (open_ret < 0) {
7861 /*
7862 * Per-PID buffer and application going
7863 * away.
7864 */
7865 if (open_ret == -LTTNG_ERR_CHAN_NOT_FOUND) {
7866 continue;
7867 }
7868
7869 ret = LTTNG_ERR_UNK;
7870 goto error;
7871 }
7872 }
7873 }
7874 break;
7875 }
7876 default:
7877 abort();
7878 break;
7879 }
7880
7881 error:
7882 rcu_read_unlock();
7883 return ret;
7884 }
This page took 0.241635 seconds and 4 git commands to generate.