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