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