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