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