Move UST registry into sessiond and implement notifiers
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <errno.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <urcu/compiler.h>
28 #include <lttng/ust-error.h>
29
30 #include <common/common.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
32
33 #include "fd-limit.h"
34 #include "health.h"
35 #include "ust-app.h"
36 #include "ust-consumer.h"
37 #include "ust-ctl.h"
38
39 /* Next available channel key. */
40 static unsigned long next_channel_key;
41
42 /*
43 * Return the atomically incremented value of next_channel_key.
44 */
45 static inline unsigned long get_next_channel_key(void)
46 {
47 return uatomic_add_return(&next_channel_key, 1);
48 }
49
50 /*
51 * Return the consumer socket from the given consumer output with the right
52 * bitness. On error, returns NULL.
53 *
54 * The caller MUST acquire a rcu read side lock and keep it until the socket
55 * object reference is not needed anymore.
56 */
57 static struct consumer_socket *find_consumer_socket_by_bitness(int bits,
58 struct consumer_output *consumer)
59 {
60 int consumer_fd;
61 struct consumer_socket *socket = NULL;
62
63 switch (bits) {
64 case 64:
65 consumer_fd = uatomic_read(&ust_consumerd64_fd);
66 break;
67 case 32:
68 consumer_fd = uatomic_read(&ust_consumerd32_fd);
69 break;
70 default:
71 assert(0);
72 goto end;
73 }
74
75 socket = consumer_find_socket(consumer_fd, consumer);
76
77 end:
78 return socket;
79 }
80
81 /*
82 * Match function for the hash table lookup.
83 *
84 * It matches an ust app event based on three attributes which are the event
85 * name, the filter bytecode and the loglevel.
86 */
87 static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key)
88 {
89 struct ust_app_event *event;
90 const struct ust_app_ht_key *key;
91
92 assert(node);
93 assert(_key);
94
95 event = caa_container_of(node, struct ust_app_event, node.node);
96 key = _key;
97
98 /* Match the 3 elements of the key: name, filter and loglevel. */
99
100 /* Event name */
101 if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) {
102 goto no_match;
103 }
104
105 /* Event loglevel. */
106 if (event->attr.loglevel != key->loglevel) {
107 if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL
108 && key->loglevel == 0 && event->attr.loglevel == -1) {
109 /*
110 * Match is accepted. This is because on event creation, the
111 * loglevel is set to -1 if the event loglevel type is ALL so 0 and
112 * -1 are accepted for this loglevel type since 0 is the one set by
113 * the API when receiving an enable event.
114 */
115 } else {
116 goto no_match;
117 }
118 }
119
120 /* One of the filters is NULL, fail. */
121 if ((key->filter && !event->filter) || (!key->filter && event->filter)) {
122 goto no_match;
123 }
124
125 if (key->filter && event->filter) {
126 /* Both filters exists, check length followed by the bytecode. */
127 if (event->filter->len != key->filter->len ||
128 memcmp(event->filter->data, key->filter->data,
129 event->filter->len) != 0) {
130 goto no_match;
131 }
132 }
133
134 /* Match. */
135 return 1;
136
137 no_match:
138 return 0;
139 }
140
141 /*
142 * Unique add of an ust app event in the given ht. This uses the custom
143 * ht_match_ust_app_event match function and the event name as hash.
144 */
145 static void add_unique_ust_app_event(struct ust_app_channel *ua_chan,
146 struct ust_app_event *event)
147 {
148 struct cds_lfht_node *node_ptr;
149 struct ust_app_ht_key key;
150 struct lttng_ht *ht;
151
152 assert(ua_chan);
153 assert(ua_chan->events);
154 assert(event);
155
156 ht = ua_chan->events;
157 key.name = event->attr.name;
158 key.filter = event->filter;
159 key.loglevel = event->attr.loglevel;
160
161 node_ptr = cds_lfht_add_unique(ht->ht,
162 ht->hash_fct(event->node.key, lttng_ht_seed),
163 ht_match_ust_app_event, &key, &event->node.node);
164 assert(node_ptr == &event->node.node);
165 }
166
167 /*
168 * Delete ust context safely. RCU read lock must be held before calling
169 * this function.
170 */
171 static
172 void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx)
173 {
174 int ret;
175
176 assert(ua_ctx);
177
178 if (ua_ctx->obj) {
179 ret = ustctl_release_object(sock, ua_ctx->obj);
180 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
181 ERR("UST app sock %d release ctx obj handle %d failed with ret %d",
182 sock, ua_ctx->obj->handle, ret);
183 }
184 free(ua_ctx->obj);
185 }
186 free(ua_ctx);
187 }
188
189 /*
190 * Delete ust app event safely. RCU read lock must be held before calling
191 * this function.
192 */
193 static
194 void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
195 {
196 int ret;
197
198 assert(ua_event);
199
200 free(ua_event->filter);
201
202 if (ua_event->obj != NULL) {
203 ret = ustctl_release_object(sock, ua_event->obj);
204 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
205 ERR("UST app sock %d release event obj failed with ret %d",
206 sock, ret);
207 }
208 free(ua_event->obj);
209 }
210 free(ua_event);
211 }
212
213 /*
214 * Delete ust app stream safely. RCU read lock must be held before calling
215 * this function.
216 */
217 static
218 void delete_ust_app_stream(int sock, struct ust_app_stream *stream)
219 {
220 int ret;
221
222 assert(stream);
223
224 if (stream->obj) {
225 ret = ustctl_release_object(sock, stream->obj);
226 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
227 ERR("UST app sock %d release stream obj failed with ret %d",
228 sock, ret);
229 }
230 lttng_fd_put(LTTNG_FD_APPS, 2);
231 free(stream->obj);
232 }
233 free(stream);
234 }
235
236 /*
237 * Delete ust app channel safely. RCU read lock must be held before calling
238 * this function.
239 */
240 static
241 void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan,
242 struct ust_app *app)
243 {
244 int ret;
245 struct lttng_ht_iter iter;
246 struct ust_app_event *ua_event;
247 struct ust_app_ctx *ua_ctx;
248 struct ust_app_stream *stream, *stmp;
249
250 assert(ua_chan);
251
252 DBG3("UST app deleting channel %s", ua_chan->name);
253
254 /* Wipe stream */
255 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
256 cds_list_del(&stream->list);
257 delete_ust_app_stream(sock, stream);
258 }
259
260 /* Wipe context */
261 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) {
262 ret = lttng_ht_del(ua_chan->ctx, &iter);
263 assert(!ret);
264 delete_ust_app_ctx(sock, ua_ctx);
265 }
266 lttng_ht_destroy(ua_chan->ctx);
267
268 /* Wipe events */
269 cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event,
270 node.node) {
271 ret = lttng_ht_del(ua_chan->events, &iter);
272 assert(!ret);
273 delete_ust_app_event(sock, ua_event);
274 }
275 lttng_ht_destroy(ua_chan->events);
276
277 /* Wipe and free registry. */
278 ust_registry_channel_destroy(&ua_chan->session->registry, &ua_chan->registry);
279
280 if (ua_chan->obj != NULL) {
281 /* Remove channel from application UST object descriptor. */
282 iter.iter.node = &ua_chan->ust_objd_node.node;
283 lttng_ht_del(app->ust_objd, &iter);
284 ret = ustctl_release_object(sock, ua_chan->obj);
285 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
286 ERR("UST app sock %d release channel obj failed with ret %d",
287 sock, ret);
288 }
289 lttng_fd_put(LTTNG_FD_APPS, 2);
290 free(ua_chan->obj);
291 }
292 free(ua_chan);
293 }
294
295 /*
296 * Delete ust app session safely. RCU read lock must be held before calling
297 * this function.
298 */
299 static
300 void delete_ust_app_session(int sock, struct ust_app_session *ua_sess,
301 struct ust_app *app)
302 {
303 int ret;
304 struct lttng_ht_iter iter;
305 struct ust_app_channel *ua_chan;
306
307 if (ua_sess->metadata) {
308 delete_ust_app_channel(sock, ua_sess->metadata, app);
309 }
310
311 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
312 node.node) {
313 ret = lttng_ht_del(ua_sess->channels, &iter);
314 assert(!ret);
315 delete_ust_app_channel(sock, ua_chan, app);
316 }
317 lttng_ht_destroy(ua_sess->channels);
318
319 ust_registry_session_destroy(&ua_sess->registry);
320
321 if (ua_sess->handle != -1) {
322 ret = ustctl_release_handle(sock, ua_sess->handle);
323 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
324 ERR("UST app sock %d release session handle failed with ret %d",
325 sock, ret);
326 }
327 }
328 free(ua_sess);
329 }
330
331 /*
332 * Delete a traceable application structure from the global list. Never call
333 * this function outside of a call_rcu call.
334 */
335 static
336 void delete_ust_app(struct ust_app *app)
337 {
338 int ret, sock;
339 struct ust_app_session *ua_sess, *tmp_ua_sess;
340
341 rcu_read_lock();
342
343 /* Delete ust app sessions info */
344 sock = app->sock;
345 app->sock = -1;
346
347 lttng_ht_destroy(app->sessions);
348
349 /* Wipe sessions */
350 cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head,
351 teardown_node) {
352 /* Free every object in the session and the session. */
353 delete_ust_app_session(sock, ua_sess, app);
354 }
355
356 /*
357 * Wait until we have deleted the application from the sock hash table
358 * before closing this socket, otherwise an application could re-use the
359 * socket ID and race with the teardown, using the same hash table entry.
360 *
361 * It's OK to leave the close in call_rcu. We want it to stay unique for
362 * all RCU readers that could run concurrently with unregister app,
363 * therefore we _need_ to only close that socket after a grace period. So
364 * it should stay in this RCU callback.
365 *
366 * This close() is a very important step of the synchronization model so
367 * every modification to this function must be carefully reviewed.
368 */
369 ret = close(sock);
370 if (ret) {
371 PERROR("close");
372 }
373 lttng_fd_put(LTTNG_FD_APPS, 1);
374
375 DBG2("UST app pid %d deleted", app->pid);
376 free(app);
377
378 rcu_read_unlock();
379 }
380
381 /*
382 * URCU intermediate call to delete an UST app.
383 */
384 static
385 void delete_ust_app_rcu(struct rcu_head *head)
386 {
387 struct lttng_ht_node_ulong *node =
388 caa_container_of(head, struct lttng_ht_node_ulong, head);
389 struct ust_app *app =
390 caa_container_of(node, struct ust_app, pid_n);
391
392 DBG3("Call RCU deleting app PID %d", app->pid);
393 delete_ust_app(app);
394 }
395
396 /*
397 * Delete the session from the application ht and delete the data structure by
398 * freeing every object inside and releasing them.
399 */
400 static void destroy_app_session(struct ust_app *app,
401 struct ust_app_session *ua_sess)
402 {
403 int ret;
404 struct lttng_ht_iter iter;
405
406 assert(app);
407 assert(ua_sess);
408
409 iter.iter.node = &ua_sess->node.node;
410 ret = lttng_ht_del(app->sessions, &iter);
411 if (ret) {
412 /* Already scheduled for teardown. */
413 goto end;
414 }
415
416 /* Once deleted, free the data structure. */
417 delete_ust_app_session(app->sock, ua_sess, app);
418
419 end:
420 return;
421 }
422
423 /*
424 * Alloc new UST app session.
425 */
426 static
427 struct ust_app_session *alloc_ust_app_session(struct ust_app *app)
428 {
429 struct ust_app_session *ua_sess;
430
431 /* Init most of the default value by allocating and zeroing */
432 ua_sess = zmalloc(sizeof(struct ust_app_session));
433 if (ua_sess == NULL) {
434 PERROR("malloc");
435 goto error_free;
436 }
437
438 ua_sess->handle = -1;
439 ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
440 pthread_mutex_init(&ua_sess->lock, NULL);
441 if (ust_registry_session_init(&ua_sess->registry, app,
442 app->bits_per_long,
443 app->uint8_t_alignment,
444 app->uint16_t_alignment,
445 app->uint32_t_alignment,
446 app->uint64_t_alignment,
447 app->long_alignment,
448 app->byte_order)) {
449 goto error;
450 }
451
452 return ua_sess;
453
454 error:
455 free(ua_sess);
456 error_free:
457 return NULL;
458 }
459
460 /*
461 * Alloc new UST app channel.
462 */
463 static
464 struct ust_app_channel *alloc_ust_app_channel(char *name,
465 struct ust_app_session *ua_sess,
466 struct lttng_ust_channel_attr *attr)
467 {
468 struct ust_app_channel *ua_chan;
469
470 /* Init most of the default value by allocating and zeroing */
471 ua_chan = zmalloc(sizeof(struct ust_app_channel));
472 if (ua_chan == NULL) {
473 PERROR("malloc");
474 goto error;
475 }
476
477 /* Setup channel name */
478 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
479 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
480
481 ua_chan->enabled = 1;
482 ua_chan->handle = -1;
483 ua_chan->key = get_next_channel_key();
484 ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
485 ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING);
486 lttng_ht_node_init_str(&ua_chan->node, ua_chan->name);
487
488 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
489
490 /* Initialize UST registry. */
491 ust_registry_channel_init(&ua_sess->registry, &ua_chan->registry);
492
493 /* Copy attributes */
494 if (attr) {
495 /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */
496 ua_chan->attr.subbuf_size = attr->subbuf_size;
497 ua_chan->attr.num_subbuf = attr->num_subbuf;
498 ua_chan->attr.overwrite = attr->overwrite;
499 ua_chan->attr.switch_timer_interval = attr->switch_timer_interval;
500 ua_chan->attr.read_timer_interval = attr->read_timer_interval;
501 ua_chan->attr.output = attr->output;
502 }
503 /* By default, the channel is a per cpu channel. */
504 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
505
506 DBG3("UST app channel %s allocated", ua_chan->name);
507
508 return ua_chan;
509
510 error:
511 return NULL;
512 }
513
514 /*
515 * Allocate and initialize a UST app stream.
516 *
517 * Return newly allocated stream pointer or NULL on error.
518 */
519 struct ust_app_stream *ust_app_alloc_stream(void)
520 {
521 struct ust_app_stream *stream = NULL;
522
523 stream = zmalloc(sizeof(*stream));
524 if (stream == NULL) {
525 PERROR("zmalloc ust app stream");
526 goto error;
527 }
528
529 /* Zero could be a valid value for a handle so flag it to -1. */
530 stream->handle = -1;
531
532 error:
533 return stream;
534 }
535
536 /*
537 * Alloc new UST app event.
538 */
539 static
540 struct ust_app_event *alloc_ust_app_event(char *name,
541 struct lttng_ust_event *attr)
542 {
543 struct ust_app_event *ua_event;
544
545 /* Init most of the default value by allocating and zeroing */
546 ua_event = zmalloc(sizeof(struct ust_app_event));
547 if (ua_event == NULL) {
548 PERROR("malloc");
549 goto error;
550 }
551
552 ua_event->enabled = 1;
553 strncpy(ua_event->name, name, sizeof(ua_event->name));
554 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
555 lttng_ht_node_init_str(&ua_event->node, ua_event->name);
556
557 /* Copy attributes */
558 if (attr) {
559 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
560 }
561
562 DBG3("UST app event %s allocated", ua_event->name);
563
564 return ua_event;
565
566 error:
567 return NULL;
568 }
569
570 /*
571 * Alloc new UST app context.
572 */
573 static
574 struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx)
575 {
576 struct ust_app_ctx *ua_ctx;
577
578 ua_ctx = zmalloc(sizeof(struct ust_app_ctx));
579 if (ua_ctx == NULL) {
580 goto error;
581 }
582
583 if (uctx) {
584 memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx));
585 }
586
587 DBG3("UST app context %d allocated", ua_ctx->ctx.ctx);
588
589 error:
590 return ua_ctx;
591 }
592
593 /*
594 * Allocate a filter and copy the given original filter.
595 *
596 * Return allocated filter or NULL on error.
597 */
598 static struct lttng_ust_filter_bytecode *alloc_copy_ust_app_filter(
599 struct lttng_ust_filter_bytecode *orig_f)
600 {
601 struct lttng_ust_filter_bytecode *filter = NULL;
602
603 /* Copy filter bytecode */
604 filter = zmalloc(sizeof(*filter) + orig_f->len);
605 if (!filter) {
606 PERROR("zmalloc alloc ust app filter");
607 goto error;
608 }
609
610 memcpy(filter, orig_f, sizeof(*filter) + orig_f->len);
611
612 error:
613 return filter;
614 }
615
616 /*
617 * Find an ust_app using the sock and return it. RCU read side lock must be
618 * held before calling this helper function.
619 */
620 static
621 struct ust_app *find_app_by_sock(int sock)
622 {
623 struct lttng_ht_node_ulong *node;
624 struct lttng_ht_iter iter;
625
626 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
627 node = lttng_ht_iter_get_node_ulong(&iter);
628 if (node == NULL) {
629 DBG2("UST app find by sock %d not found", sock);
630 goto error;
631 }
632
633 return caa_container_of(node, struct ust_app, sock_n);
634
635 error:
636 return NULL;
637 }
638
639 /*
640 * Find an ust_app using the notify sock and return it. RCU read side lock must
641 * be held before calling this helper function.
642 */
643 static struct ust_app *find_app_by_notify_sock(int sock)
644 {
645 struct lttng_ht_node_ulong *node;
646 struct lttng_ht_iter iter;
647
648 lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock),
649 &iter);
650 node = lttng_ht_iter_get_node_ulong(&iter);
651 if (node == NULL) {
652 DBG2("UST app find by notify sock %d not found", sock);
653 goto error;
654 }
655
656 return caa_container_of(node, struct ust_app, notify_sock_n);
657
658 error:
659 return NULL;
660 }
661
662 /*
663 * Lookup for an ust app event based on event name, filter bytecode and the
664 * event loglevel.
665 *
666 * Return an ust_app_event object or NULL on error.
667 */
668 static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht,
669 char *name, struct lttng_ust_filter_bytecode *filter, int loglevel)
670 {
671 struct lttng_ht_iter iter;
672 struct lttng_ht_node_str *node;
673 struct ust_app_event *event = NULL;
674 struct ust_app_ht_key key;
675
676 assert(name);
677 assert(ht);
678
679 /* Setup key for event lookup. */
680 key.name = name;
681 key.filter = filter;
682 key.loglevel = loglevel;
683
684 /* Lookup using the event name as hash and a custom match fct. */
685 cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed),
686 ht_match_ust_app_event, &key, &iter.iter);
687 node = lttng_ht_iter_get_node_str(&iter);
688 if (node == NULL) {
689 goto end;
690 }
691
692 event = caa_container_of(node, struct ust_app_event, node);
693
694 end:
695 return event;
696 }
697
698 /*
699 * Create the channel context on the tracer.
700 *
701 * Called with UST app session lock held.
702 */
703 static
704 int create_ust_channel_context(struct ust_app_channel *ua_chan,
705 struct ust_app_ctx *ua_ctx, struct ust_app *app)
706 {
707 int ret;
708
709 health_code_update();
710
711 ret = ustctl_add_context(app->sock, &ua_ctx->ctx,
712 ua_chan->obj, &ua_ctx->obj);
713 if (ret < 0) {
714 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
715 ERR("UST app create channel context failed for app (pid: %d) "
716 "with ret %d", app->pid, ret);
717 } else {
718 DBG3("UST app disable event failed. Application is dead.");
719 }
720 goto error;
721 }
722
723 ua_ctx->handle = ua_ctx->obj->handle;
724
725 DBG2("UST app context handle %d created successfully for channel %s",
726 ua_ctx->handle, ua_chan->name);
727
728 error:
729 health_code_update();
730 return ret;
731 }
732
733 /*
734 * Set the filter on the tracer.
735 */
736 static
737 int set_ust_event_filter(struct ust_app_event *ua_event,
738 struct ust_app *app)
739 {
740 int ret;
741
742 health_code_update();
743
744 if (!ua_event->filter) {
745 ret = 0;
746 goto error;
747 }
748
749 ret = ustctl_set_filter(app->sock, ua_event->filter,
750 ua_event->obj);
751 if (ret < 0) {
752 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
753 ERR("UST app event %s filter failed for app (pid: %d) "
754 "with ret %d", ua_event->attr.name, app->pid, ret);
755 } else {
756 DBG3("UST app filter event failed. Application is dead.");
757 }
758 goto error;
759 }
760
761 DBG2("UST filter set successfully for event %s", ua_event->name);
762
763 error:
764 health_code_update();
765 return ret;
766 }
767
768 /*
769 * Disable the specified event on to UST tracer for the UST session.
770 */
771 static int disable_ust_event(struct ust_app *app,
772 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
773 {
774 int ret;
775
776 health_code_update();
777
778 ret = ustctl_disable(app->sock, ua_event->obj);
779 if (ret < 0) {
780 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
781 ERR("UST app event %s disable failed for app (pid: %d) "
782 "and session handle %d with ret %d",
783 ua_event->attr.name, app->pid, ua_sess->handle, ret);
784 } else {
785 DBG3("UST app disable event failed. Application is dead.");
786 }
787 goto error;
788 }
789
790 DBG2("UST app event %s disabled successfully for app (pid: %d)",
791 ua_event->attr.name, app->pid);
792
793 error:
794 health_code_update();
795 return ret;
796 }
797
798 /*
799 * Disable the specified channel on to UST tracer for the UST session.
800 */
801 static int disable_ust_channel(struct ust_app *app,
802 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
803 {
804 int ret;
805
806 health_code_update();
807
808 ret = ustctl_disable(app->sock, ua_chan->obj);
809 if (ret < 0) {
810 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
811 ERR("UST app channel %s disable failed for app (pid: %d) "
812 "and session handle %d with ret %d",
813 ua_chan->name, app->pid, ua_sess->handle, ret);
814 } else {
815 DBG3("UST app disable channel failed. Application is dead.");
816 }
817 goto error;
818 }
819
820 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
821 ua_chan->name, app->pid);
822
823 error:
824 health_code_update();
825 return ret;
826 }
827
828 /*
829 * Enable the specified channel on to UST tracer for the UST session.
830 */
831 static int enable_ust_channel(struct ust_app *app,
832 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
833 {
834 int ret;
835
836 health_code_update();
837
838 ret = ustctl_enable(app->sock, ua_chan->obj);
839 if (ret < 0) {
840 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
841 ERR("UST app channel %s enable failed for app (pid: %d) "
842 "and session handle %d with ret %d",
843 ua_chan->name, app->pid, ua_sess->handle, ret);
844 } else {
845 DBG3("UST app enable channel failed. Application is dead.");
846 }
847 goto error;
848 }
849
850 ua_chan->enabled = 1;
851
852 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
853 ua_chan->name, app->pid);
854
855 error:
856 health_code_update();
857 return ret;
858 }
859
860 /*
861 * Enable the specified event on to UST tracer for the UST session.
862 */
863 static int enable_ust_event(struct ust_app *app,
864 struct ust_app_session *ua_sess, struct ust_app_event *ua_event)
865 {
866 int ret;
867
868 health_code_update();
869
870 ret = ustctl_enable(app->sock, ua_event->obj);
871 if (ret < 0) {
872 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
873 ERR("UST app event %s enable failed for app (pid: %d) "
874 "and session handle %d with ret %d",
875 ua_event->attr.name, app->pid, ua_sess->handle, ret);
876 } else {
877 DBG3("UST app enable event failed. Application is dead.");
878 }
879 goto error;
880 }
881
882 DBG2("UST app event %s enabled successfully for app (pid: %d)",
883 ua_event->attr.name, app->pid);
884
885 error:
886 health_code_update();
887 return ret;
888 }
889
890 /*
891 * Create the specified channel onto the UST tracer for a UST session.
892 * Called with UST app session lock held.
893 *
894 * Return 0 on success. On error, a negative value is returned.
895 */
896 static int create_ust_channel(struct ust_app *app,
897 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
898 struct consumer_output *consumer)
899 {
900 int ret;
901 unsigned int nb_fd = 0;
902 struct consumer_socket *socket;
903 struct ust_app_stream *stream, *stmp;
904
905 assert(app);
906 assert(ua_sess);
907 assert(ua_chan);
908 assert(consumer);
909
910 health_code_update();
911
912 /* Get the right consumer socket for the application. */
913 socket = find_consumer_socket_by_bitness(app->bits_per_long, consumer);
914 if (!socket) {
915 ret = -1;
916 goto error;
917 }
918
919 health_code_update();
920
921 /*
922 * Ask consumer to create channel. The consumer will return the number of
923 * stream we have to expect.
924 */
925 ret = ust_consumer_ask_channel(ua_sess, ua_chan, consumer, socket);
926 if (ret < 0) {
927 goto error;
928 }
929
930 /*
931 * Compute the number of fd needed before receiving them. It must be 2 per
932 * stream.
933 */
934 nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count;
935
936 /* Reserve the amount of file descriptor we need. */
937 ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd);
938 if (ret < 0) {
939 ERR("Exhausted number of available FD upon create channel");
940 goto error_fd_get;
941 }
942
943 health_code_update();
944
945 /*
946 * Now get the channel from the consumer. This call wil populate the stream
947 * list of that channel and set the ust object.
948 */
949 ret = ust_consumer_get_channel(socket, ua_chan);
950 if (ret < 0) {
951 goto error_destroy;
952 }
953
954 /* Send channel to the application. */
955 ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan);
956 if (ret < 0) {
957 goto error;
958 }
959
960 /* Send all streams to application. */
961 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
962 ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream);
963 if (ret < 0) {
964 goto error;
965 }
966 /* We don't need the stream anymore once sent to the tracer. */
967 cds_list_del(&stream->list);
968 delete_ust_app_stream(-1, stream);
969 }
970
971 /* Flag the channel that it is sent to the application. */
972 ua_chan->is_sent = 1;
973 /* Assign session to channel. */
974 ua_chan->session = ua_sess;
975 /* Initialize ust objd object using the received handle and add it. */
976 lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle);
977 lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node);
978
979 health_code_update();
980
981 /* If channel is not enabled, disable it on the tracer */
982 if (!ua_chan->enabled) {
983 ret = disable_ust_channel(app, ua_sess, ua_chan);
984 if (ret < 0) {
985 goto error;
986 }
987 }
988
989 return 0;
990
991 error_destroy:
992 lttng_fd_put(LTTNG_FD_APPS, nb_fd);
993 error_fd_get:
994 /*
995 * Initiate a destroy channel on the consumer since we had an error
996 * handling it on our side. The return value is of no importance since we
997 * already have a ret value set by the previous error that we need to
998 * return.
999 */
1000 (void) ust_consumer_destroy_channel(socket, ua_chan);
1001 error:
1002 health_code_update();
1003 return ret;
1004 }
1005
1006 /*
1007 * Create the specified event onto the UST tracer for a UST session.
1008 *
1009 * Should be called with session mutex held.
1010 */
1011 static
1012 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
1013 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
1014 {
1015 int ret = 0;
1016
1017 health_code_update();
1018
1019 /* Create UST event on tracer */
1020 ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj,
1021 &ua_event->obj);
1022 if (ret < 0) {
1023 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1024 ERR("Error ustctl create event %s for app pid: %d with ret %d",
1025 ua_event->attr.name, app->pid, ret);
1026 } else {
1027 DBG3("UST app create event failed. Application is dead.");
1028 }
1029 goto error;
1030 }
1031
1032 ua_event->handle = ua_event->obj->handle;
1033
1034 DBG2("UST app event %s created successfully for pid:%d",
1035 ua_event->attr.name, app->pid);
1036
1037 health_code_update();
1038
1039 /* Set filter if one is present. */
1040 if (ua_event->filter) {
1041 ret = set_ust_event_filter(ua_event, app);
1042 if (ret < 0) {
1043 goto error;
1044 }
1045 }
1046
1047 /* If event not enabled, disable it on the tracer */
1048 if (ua_event->enabled == 0) {
1049 ret = disable_ust_event(app, ua_sess, ua_event);
1050 if (ret < 0) {
1051 /*
1052 * If we hit an EPERM, something is wrong with our disable call. If
1053 * we get an EEXIST, there is a problem on the tracer side since we
1054 * just created it.
1055 */
1056 switch (ret) {
1057 case -LTTNG_UST_ERR_PERM:
1058 /* Code flow problem */
1059 assert(0);
1060 case -LTTNG_UST_ERR_EXIST:
1061 /* It's OK for our use case. */
1062 ret = 0;
1063 break;
1064 default:
1065 break;
1066 }
1067 goto error;
1068 }
1069 }
1070
1071 error:
1072 health_code_update();
1073 return ret;
1074 }
1075
1076 /*
1077 * Copy data between an UST app event and a LTT event.
1078 */
1079 static void shadow_copy_event(struct ust_app_event *ua_event,
1080 struct ltt_ust_event *uevent)
1081 {
1082 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
1083 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
1084
1085 ua_event->enabled = uevent->enabled;
1086
1087 /* Copy event attributes */
1088 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
1089
1090 /* Copy filter bytecode */
1091 if (uevent->filter) {
1092 ua_event->filter = alloc_copy_ust_app_filter(uevent->filter);
1093 /* Filter might be NULL here in case of ENONEM. */
1094 }
1095 }
1096
1097 /*
1098 * Copy data between an UST app channel and a LTT channel.
1099 */
1100 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
1101 struct ltt_ust_channel *uchan)
1102 {
1103 struct lttng_ht_iter iter;
1104 struct ltt_ust_event *uevent;
1105 struct ltt_ust_context *uctx;
1106 struct ust_app_event *ua_event;
1107 struct ust_app_ctx *ua_ctx;
1108
1109 DBG2("UST app shadow copy of channel %s started", ua_chan->name);
1110
1111 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
1112 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
1113
1114 /* Copy event attributes since the layout is different. */
1115 ua_chan->attr.subbuf_size = uchan->attr.subbuf_size;
1116 ua_chan->attr.num_subbuf = uchan->attr.num_subbuf;
1117 ua_chan->attr.overwrite = uchan->attr.overwrite;
1118 ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval;
1119 ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval;
1120 ua_chan->attr.output = uchan->attr.output;
1121 /*
1122 * Note that the attribute channel type is not set since the channel on the
1123 * tracing registry side does not have this information.
1124 */
1125
1126 ua_chan->enabled = uchan->enabled;
1127
1128 cds_lfht_for_each_entry(uchan->ctx->ht, &iter.iter, uctx, node.node) {
1129 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
1130 if (ua_ctx == NULL) {
1131 continue;
1132 }
1133 lttng_ht_node_init_ulong(&ua_ctx->node,
1134 (unsigned long) ua_ctx->ctx.ctx);
1135 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
1136 }
1137
1138 /* Copy all events from ltt ust channel to ust app channel */
1139 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
1140 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1141 uevent->filter, uevent->attr.loglevel);
1142 if (ua_event == NULL) {
1143 DBG2("UST event %s not found on shadow copy channel",
1144 uevent->attr.name);
1145 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1146 if (ua_event == NULL) {
1147 continue;
1148 }
1149 shadow_copy_event(ua_event, uevent);
1150 add_unique_ust_app_event(ua_chan, ua_event);
1151 }
1152 }
1153
1154 DBG3("UST app shadow copy of channel %s done", ua_chan->name);
1155 }
1156
1157 /*
1158 * Copy data between a UST app session and a regular LTT session.
1159 */
1160 static void shadow_copy_session(struct ust_app_session *ua_sess,
1161 struct ltt_ust_session *usess, struct ust_app *app)
1162 {
1163 struct lttng_ht_node_str *ua_chan_node;
1164 struct lttng_ht_iter iter;
1165 struct ltt_ust_channel *uchan;
1166 struct ust_app_channel *ua_chan;
1167 time_t rawtime;
1168 struct tm *timeinfo;
1169 char datetime[16];
1170 int ret;
1171
1172 /* Get date and time for unique app path */
1173 time(&rawtime);
1174 timeinfo = localtime(&rawtime);
1175 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
1176
1177 DBG2("Shadow copy of session handle %d", ua_sess->handle);
1178
1179 ua_sess->id = usess->id;
1180 ua_sess->uid = usess->uid;
1181 ua_sess->gid = usess->gid;
1182
1183 ret = snprintf(ua_sess->path, PATH_MAX, "%s-%d-%s/", app->name, app->pid,
1184 datetime);
1185 if (ret < 0) {
1186 PERROR("asprintf UST shadow copy session");
1187 /* TODO: We cannot return an error from here.. */
1188 assert(0);
1189 }
1190
1191 /* TODO: support all UST domain */
1192
1193 /* Iterate over all channels in global domain. */
1194 cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter,
1195 uchan, node.node) {
1196 struct lttng_ht_iter uiter;
1197
1198 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1199 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1200 if (ua_chan_node != NULL) {
1201 /* Session exist. Contiuing. */
1202 continue;
1203 }
1204
1205 DBG2("Channel %s not found on shadow session copy, creating it",
1206 uchan->name);
1207 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1208 if (ua_chan == NULL) {
1209 /* malloc failed FIXME: Might want to do handle ENOMEM .. */
1210 continue;
1211 }
1212 shadow_copy_channel(ua_chan, uchan);
1213 /*
1214 * The concept of metadata channel does not exist on the tracing
1215 * registry side of the session daemon so this can only be a per CPU
1216 * channel and not metadata.
1217 */
1218 ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU;
1219
1220 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1221 }
1222 }
1223
1224 /*
1225 * Lookup sesison wrapper.
1226 */
1227 static
1228 void __lookup_session_by_app(struct ltt_ust_session *usess,
1229 struct ust_app *app, struct lttng_ht_iter *iter)
1230 {
1231 /* Get right UST app session from app */
1232 lttng_ht_lookup(app->sessions, (void *)((unsigned long) usess->id), iter);
1233 }
1234
1235 /*
1236 * Return ust app session from the app session hashtable using the UST session
1237 * id.
1238 */
1239 static struct ust_app_session *lookup_session_by_app(
1240 struct ltt_ust_session *usess, struct ust_app *app)
1241 {
1242 struct lttng_ht_iter iter;
1243 struct lttng_ht_node_ulong *node;
1244
1245 __lookup_session_by_app(usess, app, &iter);
1246 node = lttng_ht_iter_get_node_ulong(&iter);
1247 if (node == NULL) {
1248 goto error;
1249 }
1250
1251 return caa_container_of(node, struct ust_app_session, node);
1252
1253 error:
1254 return NULL;
1255 }
1256
1257 /*
1258 * Create a session on the tracer side for the given app.
1259 *
1260 * On success, ua_sess_ptr is populated with the session pointer or else left
1261 * untouched. If the session was created, is_created is set to 1. On error,
1262 * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can
1263 * be NULL.
1264 *
1265 * Returns 0 on success or else a negative code which is either -ENOMEM or
1266 * -ENOTCONN which is the default code if the ustctl_create_session fails.
1267 */
1268 static int create_ust_app_session(struct ltt_ust_session *usess,
1269 struct ust_app *app, struct ust_app_session **ua_sess_ptr,
1270 int *is_created)
1271 {
1272 int ret, created = 0;
1273 struct ust_app_session *ua_sess;
1274
1275 assert(usess);
1276 assert(app);
1277 assert(ua_sess_ptr);
1278
1279 health_code_update();
1280
1281 ua_sess = lookup_session_by_app(usess, app);
1282 if (ua_sess == NULL) {
1283 DBG2("UST app pid: %d session id %d not found, creating it",
1284 app->pid, usess->id);
1285 ua_sess = alloc_ust_app_session(app);
1286 if (ua_sess == NULL) {
1287 /* Only malloc can failed so something is really wrong */
1288 ret = -ENOMEM;
1289 goto error;
1290 }
1291 shadow_copy_session(ua_sess, usess, app);
1292 created = 1;
1293 }
1294
1295 health_code_update();
1296
1297 if (ua_sess->handle == -1) {
1298 ret = ustctl_create_session(app->sock);
1299 if (ret < 0) {
1300 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
1301 ERR("Creating session for app pid %d with ret %d",
1302 app->pid, ret);
1303 } else {
1304 DBG("UST app creating session failed. Application is dead");
1305 }
1306 delete_ust_app_session(-1, ua_sess, app);
1307 if (ret != -ENOMEM) {
1308 /*
1309 * Tracer is probably gone or got an internal error so let's
1310 * behave like it will soon unregister or not usable.
1311 */
1312 ret = -ENOTCONN;
1313 }
1314 goto error;
1315 }
1316
1317 ua_sess->handle = ret;
1318
1319 /* Add ust app session to app's HT */
1320 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
1321 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
1322
1323 DBG2("UST app session created successfully with handle %d", ret);
1324 }
1325
1326 *ua_sess_ptr = ua_sess;
1327 if (is_created) {
1328 *is_created = created;
1329 }
1330 /* Everything went well. */
1331 ret = 0;
1332
1333 error:
1334 health_code_update();
1335 return ret;
1336 }
1337
1338 /*
1339 * Create a context for the channel on the tracer.
1340 *
1341 * Called with UST app session lock held.
1342 */
1343 static
1344 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
1345 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
1346 struct ust_app *app)
1347 {
1348 int ret = 0;
1349 struct lttng_ht_iter iter;
1350 struct lttng_ht_node_ulong *node;
1351 struct ust_app_ctx *ua_ctx;
1352
1353 DBG2("UST app adding context to channel %s", ua_chan->name);
1354
1355 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
1356 node = lttng_ht_iter_get_node_ulong(&iter);
1357 if (node != NULL) {
1358 ret = -EEXIST;
1359 goto error;
1360 }
1361
1362 ua_ctx = alloc_ust_app_ctx(uctx);
1363 if (ua_ctx == NULL) {
1364 /* malloc failed */
1365 ret = -1;
1366 goto error;
1367 }
1368
1369 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
1370 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
1371
1372 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
1373 if (ret < 0) {
1374 goto error;
1375 }
1376
1377 error:
1378 return ret;
1379 }
1380
1381 /*
1382 * Enable on the tracer side a ust app event for the session and channel.
1383 *
1384 * Called with UST app session lock held.
1385 */
1386 static
1387 int enable_ust_app_event(struct ust_app_session *ua_sess,
1388 struct ust_app_event *ua_event, struct ust_app *app)
1389 {
1390 int ret;
1391
1392 ret = enable_ust_event(app, ua_sess, ua_event);
1393 if (ret < 0) {
1394 goto error;
1395 }
1396
1397 ua_event->enabled = 1;
1398
1399 error:
1400 return ret;
1401 }
1402
1403 /*
1404 * Disable on the tracer side a ust app event for the session and channel.
1405 */
1406 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1407 struct ust_app_event *ua_event, struct ust_app *app)
1408 {
1409 int ret;
1410
1411 ret = disable_ust_event(app, ua_sess, ua_event);
1412 if (ret < 0) {
1413 goto error;
1414 }
1415
1416 ua_event->enabled = 0;
1417
1418 error:
1419 return ret;
1420 }
1421
1422 /*
1423 * Lookup ust app channel for session and disable it on the tracer side.
1424 */
1425 static
1426 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1427 struct ust_app_channel *ua_chan, struct ust_app *app)
1428 {
1429 int ret;
1430
1431 ret = disable_ust_channel(app, ua_sess, ua_chan);
1432 if (ret < 0) {
1433 goto error;
1434 }
1435
1436 ua_chan->enabled = 0;
1437
1438 error:
1439 return ret;
1440 }
1441
1442 /*
1443 * Lookup ust app channel for session and enable it on the tracer side.
1444 */
1445 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1446 struct ltt_ust_channel *uchan, struct ust_app *app)
1447 {
1448 int ret = 0;
1449 struct lttng_ht_iter iter;
1450 struct lttng_ht_node_str *ua_chan_node;
1451 struct ust_app_channel *ua_chan;
1452
1453 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1454 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1455 if (ua_chan_node == NULL) {
1456 DBG2("Unable to find channel %s in ust session id %u",
1457 uchan->name, ua_sess->id);
1458 goto error;
1459 }
1460
1461 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1462
1463 ret = enable_ust_channel(app, ua_sess, ua_chan);
1464 if (ret < 0) {
1465 goto error;
1466 }
1467
1468 error:
1469 return ret;
1470 }
1471
1472 /*
1473 * Create UST app channel and create it on the tracer. Set ua_chanp of the
1474 * newly created channel if not NULL.
1475 *
1476 * Called with UST app session lock held.
1477 */
1478 static int create_ust_app_channel(struct ust_app_session *ua_sess,
1479 struct ltt_ust_channel *uchan, struct ust_app *app,
1480 struct consumer_output *consumer, enum lttng_ust_chan_type type,
1481 struct ust_app_channel **ua_chanp)
1482 {
1483 int ret = 0;
1484 struct lttng_ht_iter iter;
1485 struct lttng_ht_node_str *ua_chan_node;
1486 struct ust_app_channel *ua_chan;
1487
1488 /* Lookup channel in the ust app session */
1489 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1490 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1491 if (ua_chan_node != NULL) {
1492 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1493 goto end;
1494 }
1495
1496 ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr);
1497 if (ua_chan == NULL) {
1498 /* Only malloc can fail here */
1499 ret = -ENOMEM;
1500 goto error;
1501 }
1502 shadow_copy_channel(ua_chan, uchan);
1503
1504 /* Set channel type. */
1505 ua_chan->attr.type = type;
1506
1507 ret = create_ust_channel(app, ua_sess, ua_chan, consumer);
1508 if (ret < 0) {
1509 goto error;
1510 }
1511
1512 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
1513 app->pid);
1514
1515 /* Only add the channel if successful on the tracer side. */
1516 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1517
1518 end:
1519 if (ua_chanp) {
1520 *ua_chanp = ua_chan;
1521 }
1522
1523 /* Everything went well. */
1524 return 0;
1525
1526 error:
1527 delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app);
1528 return ret;
1529 }
1530
1531 /*
1532 * Create UST app event and create it on the tracer side.
1533 *
1534 * Called with ust app session mutex held.
1535 */
1536 static
1537 int create_ust_app_event(struct ust_app_session *ua_sess,
1538 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1539 struct ust_app *app)
1540 {
1541 int ret = 0;
1542 struct ust_app_event *ua_event;
1543
1544 /* Get event node */
1545 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
1546 uevent->filter, uevent->attr.loglevel);
1547 if (ua_event != NULL) {
1548 ret = -EEXIST;
1549 goto end;
1550 }
1551
1552 /* Does not exist so create one */
1553 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1554 if (ua_event == NULL) {
1555 /* Only malloc can failed so something is really wrong */
1556 ret = -ENOMEM;
1557 goto end;
1558 }
1559 shadow_copy_event(ua_event, uevent);
1560
1561 /* Create it on the tracer side */
1562 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1563 if (ret < 0) {
1564 /* Not found previously means that it does not exist on the tracer */
1565 assert(ret != -LTTNG_UST_ERR_EXIST);
1566 goto error;
1567 }
1568
1569 add_unique_ust_app_event(ua_chan, ua_event);
1570
1571 DBG2("UST app create event %s for PID %d completed", ua_event->name,
1572 app->pid);
1573
1574 end:
1575 return ret;
1576
1577 error:
1578 /* Valid. Calling here is already in a read side lock */
1579 delete_ust_app_event(-1, ua_event);
1580 return ret;
1581 }
1582
1583 /*
1584 * Create UST metadata and open it on the tracer side.
1585 *
1586 * Called with UST app session lock held.
1587 */
1588 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1589 struct ust_app *app, struct consumer_output *consumer)
1590 {
1591 int ret = 0;
1592 struct ust_app_channel *metadata;
1593
1594 assert(ua_sess);
1595 assert(app);
1596
1597 if (ua_sess->metadata) {
1598 /* Already exist. Return success. */
1599 goto end;
1600 }
1601
1602 /* Allocate UST metadata */
1603 metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL);
1604 if (!metadata) {
1605 /* malloc() failed */
1606 ret = -ENOMEM;
1607 goto error;
1608 }
1609
1610 /* Set default attributes for metadata. */
1611 metadata->attr.overwrite = DEFAULT_CHANNEL_OVERWRITE;
1612 metadata->attr.subbuf_size = default_get_metadata_subbuf_size();
1613 metadata->attr.num_subbuf = DEFAULT_METADATA_SUBBUF_NUM;
1614 metadata->attr.switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1615 metadata->attr.read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1616 metadata->attr.output = LTTNG_UST_MMAP;
1617 metadata->attr.type = LTTNG_UST_CHAN_METADATA;
1618
1619 ret = create_ust_channel(app, ua_sess, metadata, consumer);
1620 if (ret < 0) {
1621 goto error_create;
1622 }
1623
1624 ua_sess->metadata = metadata;
1625
1626 DBG2("UST metadata opened for app pid %d", app->pid);
1627
1628 end:
1629 return 0;
1630 error_create:
1631 delete_ust_app_channel(metadata->is_sent ? app->sock : -1, metadata, app);
1632 error:
1633 return ret;
1634 }
1635
1636 /*
1637 * Return pointer to traceable apps list.
1638 */
1639 struct lttng_ht *ust_app_get_ht(void)
1640 {
1641 return ust_app_ht;
1642 }
1643
1644 /*
1645 * Return ust app pointer or NULL if not found.
1646 */
1647 struct ust_app *ust_app_find_by_pid(pid_t pid)
1648 {
1649 struct lttng_ht_node_ulong *node;
1650 struct lttng_ht_iter iter;
1651
1652 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1653 node = lttng_ht_iter_get_node_ulong(&iter);
1654 if (node == NULL) {
1655 DBG2("UST app no found with pid %d", pid);
1656 goto error;
1657 }
1658
1659 DBG2("Found UST app by pid %d", pid);
1660
1661 return caa_container_of(node, struct ust_app, pid_n);
1662
1663 error:
1664 rcu_read_unlock();
1665 return NULL;
1666 }
1667
1668 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
1669 {
1670 struct ust_app *lta = NULL;
1671
1672 assert(msg);
1673 assert(sock >= 0);
1674
1675 DBG3("UST app creating application for socket %d", sock);
1676
1677 if ((msg->bits_per_long == 64 &&
1678 (uatomic_read(&ust_consumerd64_fd) == -EINVAL))
1679 || (msg->bits_per_long == 32 &&
1680 (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) {
1681 ERR("Registration failed: application \"%s\" (pid: %d) has "
1682 "%d-bit long, but no consumerd for this size is available.\n",
1683 msg->name, msg->pid, msg->bits_per_long);
1684 goto error;
1685 }
1686
1687 lta = zmalloc(sizeof(struct ust_app));
1688 if (lta == NULL) {
1689 PERROR("malloc");
1690 goto error;
1691 }
1692
1693 lta->ppid = msg->ppid;
1694 lta->uid = msg->uid;
1695 lta->gid = msg->gid;
1696 lta->compatible = 0; /* Not compatible until proven */
1697
1698 lta->bits_per_long = msg->bits_per_long;
1699 lta->uint8_t_alignment = msg->uint8_t_alignment;
1700 lta->uint16_t_alignment = msg->uint16_t_alignment;
1701 lta->uint32_t_alignment = msg->uint32_t_alignment;
1702 lta->uint64_t_alignment = msg->uint64_t_alignment;
1703 lta->long_alignment = msg->long_alignment;
1704 lta->byte_order = msg->byte_order;
1705
1706 lta->v_major = msg->major;
1707 lta->v_minor = msg->minor;
1708 strncpy(lta->name, msg->name, sizeof(lta->name));
1709 lta->name[LTTNG_UST_ABI_PROCNAME_LEN] = '\0';
1710 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1711 lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1712 lta->notify_sock = -1;
1713 lta->compatible = 1;
1714
1715 lta->pid = msg->pid;
1716 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long) lta->pid);
1717 lta->sock = sock;
1718 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long) lta->sock);
1719
1720 CDS_INIT_LIST_HEAD(&lta->teardown_head);
1721
1722 error:
1723 return lta;
1724 }
1725
1726 void ust_app_add(struct ust_app *app)
1727 {
1728 assert(app);
1729 assert(app->notify_sock >= 0);
1730
1731 rcu_read_lock();
1732
1733 /*
1734 * On a re-registration, we want to kick out the previous registration of
1735 * that pid
1736 */
1737 lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n);
1738
1739 /*
1740 * The socket _should_ be unique until _we_ call close. So, a add_unique
1741 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1742 * already in the table.
1743 */
1744 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n);
1745
1746 /* Add application to the notify socket hash table. */
1747 lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock);
1748 lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n);
1749
1750 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s "
1751 "(version %d.%d)", app->pid, app->ppid, app->uid, app->gid,
1752 app->sock, app->name, app->v_major, app->v_minor);
1753
1754 rcu_read_unlock();
1755 }
1756
1757 int ust_app_version(struct ust_app *app)
1758 {
1759 assert(app);
1760 return ustctl_tracer_version(app->sock, &app->version);
1761 }
1762
1763 /*
1764 * Unregister app by removing it from the global traceable app list and freeing
1765 * the data struct.
1766 *
1767 * The socket is already closed at this point so no close to sock.
1768 */
1769 void ust_app_unregister(int sock)
1770 {
1771 struct ust_app *lta;
1772 struct lttng_ht_node_ulong *node;
1773 struct lttng_ht_iter iter;
1774 struct ust_app_session *ua_sess;
1775 int ret;
1776
1777 rcu_read_lock();
1778
1779 /* Get the node reference for a call_rcu */
1780 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1781 node = lttng_ht_iter_get_node_ulong(&iter);
1782 assert(node);
1783
1784 lta = caa_container_of(node, struct ust_app, sock_n);
1785 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1786
1787 /* Remove application from PID hash table */
1788 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1789 assert(!ret);
1790
1791 /* Remove application from notify hash table */
1792 iter.iter.node = &lta->notify_sock_n.node;
1793 ret = lttng_ht_del(ust_app_ht_by_notify_sock, &iter);
1794
1795 /*
1796 * Ignore return value since the node might have been removed before by an
1797 * add replace during app registration because the PID can be reassigned by
1798 * the OS.
1799 */
1800 iter.iter.node = &lta->pid_n.node;
1801 ret = lttng_ht_del(ust_app_ht, &iter);
1802 if (ret) {
1803 DBG3("Unregister app by PID %d failed. This can happen on pid reuse",
1804 lta->pid);
1805 }
1806
1807 /* Remove sessions so they are not visible during deletion.*/
1808 cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess,
1809 node.node) {
1810 ret = lttng_ht_del(lta->sessions, &iter);
1811 if (ret) {
1812 /* The session was already removed so scheduled for teardown. */
1813 continue;
1814 }
1815
1816 /*
1817 * Add session to list for teardown. This is safe since at this point we
1818 * are the only one using this list.
1819 */
1820 cds_list_add(&ua_sess->teardown_node, &lta->teardown_head);
1821 }
1822
1823 /* Free memory */
1824 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1825
1826 rcu_read_unlock();
1827 return;
1828 }
1829
1830 /*
1831 * Return traceable_app_count
1832 */
1833 unsigned long ust_app_list_count(void)
1834 {
1835 unsigned long count;
1836
1837 rcu_read_lock();
1838 count = lttng_ht_get_count(ust_app_ht);
1839 rcu_read_unlock();
1840
1841 return count;
1842 }
1843
1844 /*
1845 * Fill events array with all events name of all registered apps.
1846 */
1847 int ust_app_list_events(struct lttng_event **events)
1848 {
1849 int ret, handle;
1850 size_t nbmem, count = 0;
1851 struct lttng_ht_iter iter;
1852 struct ust_app *app;
1853 struct lttng_event *tmp_event;
1854
1855 nbmem = UST_APP_EVENT_LIST_SIZE;
1856 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event));
1857 if (tmp_event == NULL) {
1858 PERROR("zmalloc ust app events");
1859 ret = -ENOMEM;
1860 goto error;
1861 }
1862
1863 rcu_read_lock();
1864
1865 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1866 struct lttng_ust_tracepoint_iter uiter;
1867
1868 health_code_update();
1869
1870 if (!app->compatible) {
1871 /*
1872 * TODO: In time, we should notice the caller of this error by
1873 * telling him that this is a version error.
1874 */
1875 continue;
1876 }
1877 handle = ustctl_tracepoint_list(app->sock);
1878 if (handle < 0) {
1879 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
1880 ERR("UST app list events getting handle failed for app pid %d",
1881 app->pid);
1882 }
1883 continue;
1884 }
1885
1886 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
1887 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1888 /* Handle ustctl error. */
1889 if (ret < 0) {
1890 free(tmp_event);
1891 if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) {
1892 ERR("UST app tp list get failed for app %d with ret %d",
1893 app->sock, ret);
1894 } else {
1895 DBG3("UST app tp list get failed. Application is dead");
1896 }
1897 goto rcu_error;
1898 }
1899
1900 health_code_update();
1901 if (count >= nbmem) {
1902 /* In case the realloc fails, we free the memory */
1903 void *ptr;
1904
1905 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1906 2 * nbmem);
1907 nbmem *= 2;
1908 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event));
1909 if (ptr == NULL) {
1910 PERROR("realloc ust app events");
1911 free(tmp_event);
1912 ret = -ENOMEM;
1913 goto rcu_error;
1914 }
1915 tmp_event = ptr;
1916 }
1917 memcpy(tmp_event[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1918 tmp_event[count].loglevel = uiter.loglevel;
1919 tmp_event[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1920 tmp_event[count].pid = app->pid;
1921 tmp_event[count].enabled = -1;
1922 count++;
1923 }
1924 }
1925
1926 ret = count;
1927 *events = tmp_event;
1928
1929 DBG2("UST app list events done (%zu events)", count);
1930
1931 rcu_error:
1932 rcu_read_unlock();
1933 error:
1934 health_code_update();
1935 return ret;
1936 }
1937
1938 /*
1939 * Fill events array with all events name of all registered apps.
1940 */
1941 int ust_app_list_event_fields(struct lttng_event_field **fields)
1942 {
1943 int ret, handle;
1944 size_t nbmem, count = 0;
1945 struct lttng_ht_iter iter;
1946 struct ust_app *app;
1947 struct lttng_event_field *tmp_event;
1948
1949 nbmem = UST_APP_EVENT_LIST_SIZE;
1950 tmp_event = zmalloc(nbmem * sizeof(struct lttng_event_field));
1951 if (tmp_event == NULL) {
1952 PERROR("zmalloc ust app event fields");
1953 ret = -ENOMEM;
1954 goto error;
1955 }
1956
1957 rcu_read_lock();
1958
1959 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1960 struct lttng_ust_field_iter uiter;
1961
1962 health_code_update();
1963
1964 if (!app->compatible) {
1965 /*
1966 * TODO: In time, we should notice the caller of this error by
1967 * telling him that this is a version error.
1968 */
1969 continue;
1970 }
1971 handle = ustctl_tracepoint_field_list(app->sock);
1972 if (handle < 0) {
1973 if (handle != -EPIPE && handle != -LTTNG_UST_ERR_EXITING) {
1974 ERR("UST app list field getting handle failed for app pid %d",
1975 app->pid);
1976 }
1977 continue;
1978 }
1979
1980 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
1981 &uiter)) != -LTTNG_UST_ERR_NOENT) {
1982 /* Handle ustctl error. */
1983 if (ret < 0) {
1984 free(tmp_event);
1985 if (ret != -LTTNG_UST_ERR_EXITING || ret != -EPIPE) {
1986 ERR("UST app tp list field failed for app %d with ret %d",
1987 app->sock, ret);
1988 } else {
1989 DBG3("UST app tp list field failed. Application is dead");
1990 }
1991 goto rcu_error;
1992 }
1993
1994 health_code_update();
1995 if (count >= nbmem) {
1996 /* In case the realloc fails, we free the memory */
1997 void *ptr;
1998
1999 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
2000 2 * nbmem);
2001 nbmem *= 2;
2002 ptr = realloc(tmp_event, nbmem * sizeof(struct lttng_event_field));
2003 if (ptr == NULL) {
2004 PERROR("realloc ust app event fields");
2005 free(tmp_event);
2006 ret = -ENOMEM;
2007 goto rcu_error;
2008 }
2009 tmp_event = ptr;
2010 }
2011
2012 memcpy(tmp_event[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
2013 tmp_event[count].type = uiter.type;
2014 tmp_event[count].nowrite = uiter.nowrite;
2015
2016 memcpy(tmp_event[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
2017 tmp_event[count].event.loglevel = uiter.loglevel;
2018 tmp_event[count].event.type = LTTNG_UST_TRACEPOINT;
2019 tmp_event[count].event.pid = app->pid;
2020 tmp_event[count].event.enabled = -1;
2021 count++;
2022 }
2023 }
2024
2025 ret = count;
2026 *fields = tmp_event;
2027
2028 DBG2("UST app list event fields done (%zu events)", count);
2029
2030 rcu_error:
2031 rcu_read_unlock();
2032 error:
2033 health_code_update();
2034 return ret;
2035 }
2036
2037 /*
2038 * Free and clean all traceable apps of the global list.
2039 */
2040 void ust_app_clean_list(void)
2041 {
2042 int ret;
2043 struct ust_app *app;
2044 struct lttng_ht_iter iter;
2045
2046 DBG2("UST app cleaning registered apps hash table");
2047
2048 rcu_read_lock();
2049
2050 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2051 ret = lttng_ht_del(ust_app_ht, &iter);
2052 assert(!ret);
2053 call_rcu(&app->pid_n.head, delete_ust_app_rcu);
2054 }
2055
2056 /* Cleanup socket hash table */
2057 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, app,
2058 sock_n.node) {
2059 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
2060 assert(!ret);
2061 }
2062
2063 /* Destroy is done only when the ht is empty */
2064 lttng_ht_destroy(ust_app_ht);
2065 lttng_ht_destroy(ust_app_ht_by_sock);
2066
2067 rcu_read_unlock();
2068 }
2069
2070 /*
2071 * Init UST app hash table.
2072 */
2073 void ust_app_ht_alloc(void)
2074 {
2075 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2076 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2077 ust_app_ht_by_notify_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
2078 }
2079
2080 /*
2081 * For a specific UST session, disable the channel for all registered apps.
2082 */
2083 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
2084 struct ltt_ust_channel *uchan)
2085 {
2086 int ret = 0;
2087 struct lttng_ht_iter iter;
2088 struct lttng_ht_node_str *ua_chan_node;
2089 struct ust_app *app;
2090 struct ust_app_session *ua_sess;
2091 struct ust_app_channel *ua_chan;
2092
2093 if (usess == NULL || uchan == NULL) {
2094 ERR("Disabling UST global channel with NULL values");
2095 ret = -1;
2096 goto error;
2097 }
2098
2099 DBG2("UST app disabling channel %s from global domain for session id %d",
2100 uchan->name, usess->id);
2101
2102 rcu_read_lock();
2103
2104 /* For every registered applications */
2105 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2106 struct lttng_ht_iter uiter;
2107 if (!app->compatible) {
2108 /*
2109 * TODO: In time, we should notice the caller of this error by
2110 * telling him that this is a version error.
2111 */
2112 continue;
2113 }
2114 ua_sess = lookup_session_by_app(usess, app);
2115 if (ua_sess == NULL) {
2116 continue;
2117 }
2118
2119 /* Get channel */
2120 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2121 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2122 /* If the session if found for the app, the channel must be there */
2123 assert(ua_chan_node);
2124
2125 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2126 /* The channel must not be already disabled */
2127 assert(ua_chan->enabled == 1);
2128
2129 /* Disable channel onto application */
2130 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
2131 if (ret < 0) {
2132 /* XXX: We might want to report this error at some point... */
2133 continue;
2134 }
2135 }
2136
2137 rcu_read_unlock();
2138
2139 error:
2140 return ret;
2141 }
2142
2143 /*
2144 * For a specific UST session, enable the channel for all registered apps.
2145 */
2146 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
2147 struct ltt_ust_channel *uchan)
2148 {
2149 int ret = 0;
2150 struct lttng_ht_iter iter;
2151 struct ust_app *app;
2152 struct ust_app_session *ua_sess;
2153
2154 if (usess == NULL || uchan == NULL) {
2155 ERR("Adding UST global channel to NULL values");
2156 ret = -1;
2157 goto error;
2158 }
2159
2160 DBG2("UST app enabling channel %s to global domain for session id %d",
2161 uchan->name, usess->id);
2162
2163 rcu_read_lock();
2164
2165 /* For every registered applications */
2166 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2167 if (!app->compatible) {
2168 /*
2169 * TODO: In time, we should notice the caller of this error by
2170 * telling him that this is a version error.
2171 */
2172 continue;
2173 }
2174 ua_sess = lookup_session_by_app(usess, app);
2175 if (ua_sess == NULL) {
2176 continue;
2177 }
2178
2179 /* Enable channel onto application */
2180 ret = enable_ust_app_channel(ua_sess, uchan, app);
2181 if (ret < 0) {
2182 /* XXX: We might want to report this error at some point... */
2183 continue;
2184 }
2185 }
2186
2187 rcu_read_unlock();
2188
2189 error:
2190 return ret;
2191 }
2192
2193 /*
2194 * Disable an event in a channel and for a specific session.
2195 */
2196 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
2197 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2198 {
2199 int ret = 0;
2200 struct lttng_ht_iter iter, uiter;
2201 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2202 struct ust_app *app;
2203 struct ust_app_session *ua_sess;
2204 struct ust_app_channel *ua_chan;
2205 struct ust_app_event *ua_event;
2206
2207 DBG("UST app disabling event %s for all apps in channel "
2208 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
2209
2210 rcu_read_lock();
2211
2212 /* For all registered applications */
2213 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2214 if (!app->compatible) {
2215 /*
2216 * TODO: In time, we should notice the caller of this error by
2217 * telling him that this is a version error.
2218 */
2219 continue;
2220 }
2221 ua_sess = lookup_session_by_app(usess, app);
2222 if (ua_sess == NULL) {
2223 /* Next app */
2224 continue;
2225 }
2226
2227 /* Lookup channel in the ust app session */
2228 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2229 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2230 if (ua_chan_node == NULL) {
2231 DBG2("Channel %s not found in session id %d for app pid %d."
2232 "Skipping", uchan->name, usess->id, app->pid);
2233 continue;
2234 }
2235 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2236
2237 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2238 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
2239 if (ua_event_node == NULL) {
2240 DBG2("Event %s not found in channel %s for app pid %d."
2241 "Skipping", uevent->attr.name, uchan->name, app->pid);
2242 continue;
2243 }
2244 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2245
2246 ret = disable_ust_app_event(ua_sess, ua_event, app);
2247 if (ret < 0) {
2248 /* XXX: Report error someday... */
2249 continue;
2250 }
2251 }
2252
2253 rcu_read_unlock();
2254
2255 return ret;
2256 }
2257
2258 /*
2259 * For a specific UST session and UST channel, the event for all
2260 * registered apps.
2261 */
2262 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
2263 struct ltt_ust_channel *uchan)
2264 {
2265 int ret = 0;
2266 struct lttng_ht_iter iter, uiter;
2267 struct lttng_ht_node_str *ua_chan_node;
2268 struct ust_app *app;
2269 struct ust_app_session *ua_sess;
2270 struct ust_app_channel *ua_chan;
2271 struct ust_app_event *ua_event;
2272
2273 DBG("UST app disabling all event for all apps in channel "
2274 "%s for session id %d", uchan->name, usess->id);
2275
2276 rcu_read_lock();
2277
2278 /* For all registered applications */
2279 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2280 if (!app->compatible) {
2281 /*
2282 * TODO: In time, we should notice the caller of this error by
2283 * telling him that this is a version error.
2284 */
2285 continue;
2286 }
2287 ua_sess = lookup_session_by_app(usess, app);
2288 if (!ua_sess) {
2289 /* The application has problem or is probably dead. */
2290 continue;
2291 }
2292
2293 /* Lookup channel in the ust app session */
2294 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2295 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2296 /* If the channel is not found, there is a code flow error */
2297 assert(ua_chan_node);
2298
2299 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2300
2301 /* Disable each events of channel */
2302 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2303 node.node) {
2304 ret = disable_ust_app_event(ua_sess, ua_event, app);
2305 if (ret < 0) {
2306 /* XXX: Report error someday... */
2307 continue;
2308 }
2309 }
2310 }
2311
2312 rcu_read_unlock();
2313
2314 return ret;
2315 }
2316
2317 /*
2318 * For a specific UST session, create the channel for all registered apps.
2319 */
2320 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
2321 struct ltt_ust_channel *uchan)
2322 {
2323 int ret = 0, created;
2324 struct lttng_ht_iter iter;
2325 struct ust_app *app;
2326 struct ust_app_session *ua_sess = NULL;
2327
2328 /* Very wrong code flow */
2329 assert(usess);
2330 assert(uchan);
2331
2332 DBG2("UST app adding channel %s to global domain for session id %d",
2333 uchan->name, usess->id);
2334
2335 rcu_read_lock();
2336
2337 /* For every registered applications */
2338 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2339 if (!app->compatible) {
2340 /*
2341 * TODO: In time, we should notice the caller of this error by
2342 * telling him that this is a version error.
2343 */
2344 continue;
2345 }
2346 /*
2347 * Create session on the tracer side and add it to app session HT. Note
2348 * that if session exist, it will simply return a pointer to the ust
2349 * app session.
2350 */
2351 ret = create_ust_app_session(usess, app, &ua_sess, &created);
2352 if (ret < 0) {
2353 switch (ret) {
2354 case -ENOTCONN:
2355 /*
2356 * The application's socket is not valid. Either a bad socket
2357 * or a timeout on it. We can't inform the caller that for a
2358 * specific app, the session failed so lets continue here.
2359 */
2360 continue;
2361 case -ENOMEM:
2362 default:
2363 goto error_rcu_unlock;
2364 }
2365 }
2366 assert(ua_sess);
2367
2368 pthread_mutex_lock(&ua_sess->lock);
2369 /* Create channel onto application. We don't need the chan ref. */
2370 ret = create_ust_app_channel(ua_sess, uchan, app, usess->consumer,
2371 LTTNG_UST_CHAN_PER_CPU, NULL);
2372 pthread_mutex_unlock(&ua_sess->lock);
2373 if (ret < 0) {
2374 if (ret == -ENOMEM) {
2375 /* No more memory is a fatal error. Stop right now. */
2376 goto error_rcu_unlock;
2377 }
2378 /* Cleanup the created session if it's the case. */
2379 if (created) {
2380 destroy_app_session(app, ua_sess);
2381 }
2382 }
2383 }
2384
2385 error_rcu_unlock:
2386 rcu_read_unlock();
2387 return ret;
2388 }
2389
2390 /*
2391 * Enable event for a specific session and channel on the tracer.
2392 */
2393 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
2394 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2395 {
2396 int ret = 0;
2397 struct lttng_ht_iter iter, uiter;
2398 struct lttng_ht_node_str *ua_chan_node;
2399 struct ust_app *app;
2400 struct ust_app_session *ua_sess;
2401 struct ust_app_channel *ua_chan;
2402 struct ust_app_event *ua_event;
2403
2404 DBG("UST app enabling event %s for all apps for session id %d",
2405 uevent->attr.name, usess->id);
2406
2407 /*
2408 * NOTE: At this point, this function is called only if the session and
2409 * channel passed are already created for all apps. and enabled on the
2410 * tracer also.
2411 */
2412
2413 rcu_read_lock();
2414
2415 /* For all registered applications */
2416 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2417 if (!app->compatible) {
2418 /*
2419 * TODO: In time, we should notice the caller of this error by
2420 * telling him that this is a version error.
2421 */
2422 continue;
2423 }
2424 ua_sess = lookup_session_by_app(usess, app);
2425 if (!ua_sess) {
2426 /* The application has problem or is probably dead. */
2427 continue;
2428 }
2429
2430 pthread_mutex_lock(&ua_sess->lock);
2431
2432 /* Lookup channel in the ust app session */
2433 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2434 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2435 /* If the channel is not found, there is a code flow error */
2436 assert(ua_chan_node);
2437
2438 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2439
2440 /* Get event node */
2441 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
2442 uevent->filter, uevent->attr.loglevel);
2443 if (ua_event == NULL) {
2444 DBG3("UST app enable event %s not found for app PID %d."
2445 "Skipping app", uevent->attr.name, app->pid);
2446 goto next_app;
2447 }
2448
2449 ret = enable_ust_app_event(ua_sess, ua_event, app);
2450 if (ret < 0) {
2451 pthread_mutex_unlock(&ua_sess->lock);
2452 goto error;
2453 }
2454 next_app:
2455 pthread_mutex_unlock(&ua_sess->lock);
2456 }
2457
2458 error:
2459 rcu_read_unlock();
2460 return ret;
2461 }
2462
2463 /*
2464 * For a specific existing UST session and UST channel, creates the event for
2465 * all registered apps.
2466 */
2467 int ust_app_create_event_glb(struct ltt_ust_session *usess,
2468 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
2469 {
2470 int ret = 0;
2471 struct lttng_ht_iter iter, uiter;
2472 struct lttng_ht_node_str *ua_chan_node;
2473 struct ust_app *app;
2474 struct ust_app_session *ua_sess;
2475 struct ust_app_channel *ua_chan;
2476
2477 DBG("UST app creating event %s for all apps for session id %d",
2478 uevent->attr.name, usess->id);
2479
2480 rcu_read_lock();
2481
2482 /* For all registered applications */
2483 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2484 if (!app->compatible) {
2485 /*
2486 * TODO: In time, we should notice the caller of this error by
2487 * telling him that this is a version error.
2488 */
2489 continue;
2490 }
2491 ua_sess = lookup_session_by_app(usess, app);
2492 if (!ua_sess) {
2493 /* The application has problem or is probably dead. */
2494 continue;
2495 }
2496
2497 pthread_mutex_lock(&ua_sess->lock);
2498 /* Lookup channel in the ust app session */
2499 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2500 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2501 /* If the channel is not found, there is a code flow error */
2502 assert(ua_chan_node);
2503
2504 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2505
2506 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2507 pthread_mutex_unlock(&ua_sess->lock);
2508 if (ret < 0) {
2509 if (ret != -LTTNG_UST_ERR_EXIST) {
2510 /* Possible value at this point: -ENOMEM. If so, we stop! */
2511 break;
2512 }
2513 DBG2("UST app event %s already exist on app PID %d",
2514 uevent->attr.name, app->pid);
2515 continue;
2516 }
2517 }
2518
2519 rcu_read_unlock();
2520
2521 return ret;
2522 }
2523
2524 /*
2525 * Start tracing for a specific UST session and app.
2526 */
2527 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
2528 {
2529 int ret = 0;
2530 struct ust_app_session *ua_sess;
2531
2532 DBG("Starting tracing for ust app pid %d", app->pid);
2533
2534 rcu_read_lock();
2535
2536 if (!app->compatible) {
2537 goto end;
2538 }
2539
2540 ua_sess = lookup_session_by_app(usess, app);
2541 if (ua_sess == NULL) {
2542 /* The session is in teardown process. Ignore and continue. */
2543 goto end;
2544 }
2545
2546 pthread_mutex_lock(&ua_sess->lock);
2547
2548 /* Upon restart, we skip the setup, already done */
2549 if (ua_sess->started) {
2550 goto skip_setup;
2551 }
2552
2553 /* Create directories if consumer is LOCAL and has a path defined. */
2554 if (usess->consumer->type == CONSUMER_DST_LOCAL &&
2555 strlen(usess->consumer->dst.trace_path) > 0) {
2556 ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path,
2557 S_IRWXU | S_IRWXG, usess->uid, usess->gid);
2558 if (ret < 0) {
2559 if (ret != -EEXIST) {
2560 ERR("Trace directory creation error");
2561 goto error_unlock;
2562 }
2563 }
2564 }
2565
2566 /* Create the metadata for the application. */
2567 ret = create_ust_app_metadata(ua_sess, app, usess->consumer);
2568 if (ret < 0) {
2569 goto error_unlock;
2570 }
2571
2572 health_code_update();
2573
2574 skip_setup:
2575 /* This start the UST tracing */
2576 ret = ustctl_start_session(app->sock, ua_sess->handle);
2577 if (ret < 0) {
2578 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2579 ERR("Error starting tracing for app pid: %d (ret: %d)",
2580 app->pid, ret);
2581 } else {
2582 DBG("UST app start session failed. Application is dead.");
2583 }
2584 goto error_unlock;
2585 }
2586
2587 /* Indicate that the session has been started once */
2588 ua_sess->started = 1;
2589
2590 pthread_mutex_unlock(&ua_sess->lock);
2591
2592 health_code_update();
2593
2594 /* Quiescent wait after starting trace */
2595 ret = ustctl_wait_quiescent(app->sock);
2596 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2597 ERR("UST app wait quiescent failed for app pid %d ret %d",
2598 app->pid, ret);
2599 }
2600
2601 end:
2602 rcu_read_unlock();
2603 health_code_update();
2604 return 0;
2605
2606 error_unlock:
2607 pthread_mutex_unlock(&ua_sess->lock);
2608 rcu_read_unlock();
2609 health_code_update();
2610 return -1;
2611 }
2612
2613 /*
2614 * Stop tracing for a specific UST session and app.
2615 */
2616 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
2617 {
2618 int ret = 0;
2619 struct lttng_ht_iter iter;
2620 struct ust_app_session *ua_sess;
2621 struct ust_app_channel *ua_chan;
2622
2623 DBG("Stopping tracing for ust app pid %d", app->pid);
2624
2625 rcu_read_lock();
2626
2627 if (!app->compatible) {
2628 goto end;
2629 }
2630
2631 ua_sess = lookup_session_by_app(usess, app);
2632 if (ua_sess == NULL) {
2633 goto end;
2634 }
2635
2636 /*
2637 * If started = 0, it means that stop trace has been called for a session
2638 * that was never started. It's possible since we can have a fail start
2639 * from either the application manager thread or the command thread. Simply
2640 * indicate that this is a stop error.
2641 */
2642 if (!ua_sess->started) {
2643 goto error_rcu_unlock;
2644 }
2645
2646 health_code_update();
2647
2648 /* This inhibits UST tracing */
2649 ret = ustctl_stop_session(app->sock, ua_sess->handle);
2650 if (ret < 0) {
2651 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2652 ERR("Error stopping tracing for app pid: %d (ret: %d)",
2653 app->pid, ret);
2654 } else {
2655 DBG("UST app stop session failed. Application is dead.");
2656 }
2657 goto error_rcu_unlock;
2658 }
2659
2660 health_code_update();
2661
2662 /* Quiescent wait after stopping trace */
2663 ret = ustctl_wait_quiescent(app->sock);
2664 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2665 ERR("UST app wait quiescent failed for app pid %d ret %d",
2666 app->pid, ret);
2667 }
2668
2669 health_code_update();
2670
2671 /* Flushing buffers */
2672 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2673 node.node) {
2674 health_code_update();
2675 assert(ua_chan->is_sent);
2676 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
2677 if (ret < 0) {
2678 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2679 ERR("UST app PID %d channel %s flush failed with ret %d",
2680 app->pid, ua_chan->name, ret);
2681 } else {
2682 DBG3("UST app failed to flush %s. Application is dead.",
2683 ua_chan->name);
2684 /* No need to continue. */
2685 goto end;
2686 }
2687 /* Continuing flushing all buffers */
2688 continue;
2689 }
2690 }
2691
2692 health_code_update();
2693
2694 assert(ua_sess->metadata->is_sent);
2695 /* Flush all buffers before stopping */
2696 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
2697 if (ret < 0) {
2698 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2699 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
2700 ret);
2701 goto error_rcu_unlock;
2702 } else {
2703 DBG3("UST app failed to flush metadata. Application is dead.");
2704 }
2705 }
2706
2707 end:
2708 rcu_read_unlock();
2709 health_code_update();
2710 return 0;
2711
2712 error_rcu_unlock:
2713 rcu_read_unlock();
2714 health_code_update();
2715 return -1;
2716 }
2717
2718 /*
2719 * Destroy a specific UST session in apps.
2720 */
2721 static int destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2722 {
2723 int ret;
2724 struct ust_app_session *ua_sess;
2725 struct lttng_ht_iter iter;
2726 struct lttng_ht_node_ulong *node;
2727
2728 DBG("Destroy tracing for ust app pid %d", app->pid);
2729
2730 rcu_read_lock();
2731
2732 if (!app->compatible) {
2733 goto end;
2734 }
2735
2736 __lookup_session_by_app(usess, app, &iter);
2737 node = lttng_ht_iter_get_node_ulong(&iter);
2738 if (node == NULL) {
2739 /* Session is being or is deleted. */
2740 goto end;
2741 }
2742 ua_sess = caa_container_of(node, struct ust_app_session, node);
2743
2744 health_code_update();
2745 destroy_app_session(app, ua_sess);
2746
2747 health_code_update();
2748
2749 /* Quiescent wait after stopping trace */
2750 ret = ustctl_wait_quiescent(app->sock);
2751 if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
2752 ERR("UST app wait quiescent failed for app pid %d ret %d",
2753 app->pid, ret);
2754 }
2755
2756 end:
2757 rcu_read_unlock();
2758 health_code_update();
2759 return 0;
2760 }
2761
2762 /*
2763 * Start tracing for the UST session.
2764 */
2765 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2766 {
2767 int ret = 0;
2768 struct lttng_ht_iter iter;
2769 struct ust_app *app;
2770
2771 DBG("Starting all UST traces");
2772
2773 rcu_read_lock();
2774
2775 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2776 ret = ust_app_start_trace(usess, app);
2777 if (ret < 0) {
2778 /* Continue to next apps even on error */
2779 continue;
2780 }
2781 }
2782
2783 rcu_read_unlock();
2784
2785 return 0;
2786 }
2787
2788 /*
2789 * Start tracing for the UST session.
2790 */
2791 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2792 {
2793 int ret = 0;
2794 struct lttng_ht_iter iter;
2795 struct ust_app *app;
2796
2797 DBG("Stopping all UST traces");
2798
2799 rcu_read_lock();
2800
2801 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2802 ret = ust_app_stop_trace(usess, app);
2803 if (ret < 0) {
2804 /* Continue to next apps even on error */
2805 continue;
2806 }
2807 }
2808
2809 rcu_read_unlock();
2810
2811 return 0;
2812 }
2813
2814 /*
2815 * Destroy app UST session.
2816 */
2817 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2818 {
2819 int ret = 0;
2820 struct lttng_ht_iter iter;
2821 struct ust_app *app;
2822
2823 DBG("Destroy all UST traces");
2824
2825 rcu_read_lock();
2826
2827 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2828 ret = destroy_trace(usess, app);
2829 if (ret < 0) {
2830 /* Continue to next apps even on error */
2831 continue;
2832 }
2833 }
2834
2835 rcu_read_unlock();
2836
2837 return 0;
2838 }
2839
2840 /*
2841 * Add channels/events from UST global domain to registered apps at sock.
2842 */
2843 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2844 {
2845 int ret = 0;
2846 struct lttng_ht_iter iter, uiter, iter_ctx;
2847 struct ust_app *app;
2848 struct ust_app_session *ua_sess = NULL;
2849 struct ust_app_channel *ua_chan;
2850 struct ust_app_event *ua_event;
2851 struct ust_app_ctx *ua_ctx;
2852
2853 assert(usess);
2854 assert(sock >= 0);
2855
2856 DBG2("UST app global update for app sock %d for session id %d", sock,
2857 usess->id);
2858
2859 rcu_read_lock();
2860
2861 app = find_app_by_sock(sock);
2862 if (app == NULL) {
2863 ERR("Failed to find app sock %d", sock);
2864 goto error;
2865 }
2866
2867 if (!app->compatible) {
2868 goto error;
2869 }
2870
2871 ret = create_ust_app_session(usess, app, &ua_sess, NULL);
2872 if (ret < 0) {
2873 /* Tracer is probably gone or ENOMEM. */
2874 goto error;
2875 }
2876 assert(ua_sess);
2877
2878 pthread_mutex_lock(&ua_sess->lock);
2879
2880 /*
2881 * We can iterate safely here over all UST app session since the create ust
2882 * app session above made a shadow copy of the UST global domain from the
2883 * ltt ust session.
2884 */
2885 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2886 node.node) {
2887 ret = create_ust_channel(app, ua_sess, ua_chan, usess->consumer);
2888 if (ret < 0) {
2889 /*
2890 * Stop everything. On error, the application failed, no more file
2891 * descriptor are available or ENOMEM so stopping here is the only
2892 * thing we can do for now.
2893 */
2894 goto error_unlock;
2895 }
2896
2897 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2898 node.node) {
2899 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2900 if (ret < 0) {
2901 goto error_unlock;
2902 }
2903 }
2904
2905
2906 /* For each events */
2907 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2908 node.node) {
2909 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2910 if (ret < 0) {
2911 goto error_unlock;
2912 }
2913 }
2914 }
2915
2916 pthread_mutex_unlock(&ua_sess->lock);
2917
2918 if (usess->start_trace) {
2919 ret = ust_app_start_trace(usess, app);
2920 if (ret < 0) {
2921 goto error;
2922 }
2923
2924 DBG2("UST trace started for app pid %d", app->pid);
2925 }
2926
2927 /* Everything went well at this point. */
2928 rcu_read_unlock();
2929 return;
2930
2931 error_unlock:
2932 pthread_mutex_unlock(&ua_sess->lock);
2933 error:
2934 if (ua_sess) {
2935 destroy_app_session(app, ua_sess);
2936 }
2937 rcu_read_unlock();
2938 return;
2939 }
2940
2941 /*
2942 * Add context to a specific channel for global UST domain.
2943 */
2944 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2945 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2946 {
2947 int ret = 0;
2948 struct lttng_ht_node_str *ua_chan_node;
2949 struct lttng_ht_iter iter, uiter;
2950 struct ust_app_channel *ua_chan = NULL;
2951 struct ust_app_session *ua_sess;
2952 struct ust_app *app;
2953
2954 rcu_read_lock();
2955
2956 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2957 if (!app->compatible) {
2958 /*
2959 * TODO: In time, we should notice the caller of this error by
2960 * telling him that this is a version error.
2961 */
2962 continue;
2963 }
2964 ua_sess = lookup_session_by_app(usess, app);
2965 if (ua_sess == NULL) {
2966 continue;
2967 }
2968
2969 pthread_mutex_lock(&ua_sess->lock);
2970 /* Lookup channel in the ust app session */
2971 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2972 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2973 if (ua_chan_node == NULL) {
2974 goto next_app;
2975 }
2976 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2977 node);
2978 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2979 if (ret < 0) {
2980 goto next_app;
2981 }
2982 next_app:
2983 pthread_mutex_unlock(&ua_sess->lock);
2984 }
2985
2986 rcu_read_unlock();
2987 return ret;
2988 }
2989
2990 /*
2991 * Enable event for a channel from a UST session for a specific PID.
2992 */
2993 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2994 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2995 {
2996 int ret = 0;
2997 struct lttng_ht_iter iter;
2998 struct lttng_ht_node_str *ua_chan_node;
2999 struct ust_app *app;
3000 struct ust_app_session *ua_sess;
3001 struct ust_app_channel *ua_chan;
3002 struct ust_app_event *ua_event;
3003
3004 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
3005
3006 rcu_read_lock();
3007
3008 app = ust_app_find_by_pid(pid);
3009 if (app == NULL) {
3010 ERR("UST app enable event per PID %d not found", pid);
3011 ret = -1;
3012 goto end;
3013 }
3014
3015 if (!app->compatible) {
3016 ret = 0;
3017 goto end;
3018 }
3019
3020 ua_sess = lookup_session_by_app(usess, app);
3021 if (!ua_sess) {
3022 /* The application has problem or is probably dead. */
3023 ret = 0;
3024 goto end;
3025 }
3026
3027 pthread_mutex_lock(&ua_sess->lock);
3028 /* Lookup channel in the ust app session */
3029 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3030 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3031 /* If the channel is not found, there is a code flow error */
3032 assert(ua_chan_node);
3033
3034 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3035
3036 ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name,
3037 uevent->filter, uevent->attr.loglevel);
3038 if (ua_event == NULL) {
3039 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
3040 if (ret < 0) {
3041 goto end_unlock;
3042 }
3043 } else {
3044 ret = enable_ust_app_event(ua_sess, ua_event, app);
3045 if (ret < 0) {
3046 goto end_unlock;
3047 }
3048 }
3049
3050 end_unlock:
3051 pthread_mutex_unlock(&ua_sess->lock);
3052 end:
3053 rcu_read_unlock();
3054 return ret;
3055 }
3056
3057 /*
3058 * Disable event for a channel from a UST session for a specific PID.
3059 */
3060 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
3061 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
3062 {
3063 int ret = 0;
3064 struct lttng_ht_iter iter;
3065 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
3066 struct ust_app *app;
3067 struct ust_app_session *ua_sess;
3068 struct ust_app_channel *ua_chan;
3069 struct ust_app_event *ua_event;
3070
3071 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
3072
3073 rcu_read_lock();
3074
3075 app = ust_app_find_by_pid(pid);
3076 if (app == NULL) {
3077 ERR("UST app disable event per PID %d not found", pid);
3078 ret = -1;
3079 goto error;
3080 }
3081
3082 if (!app->compatible) {
3083 ret = 0;
3084 goto error;
3085 }
3086
3087 ua_sess = lookup_session_by_app(usess, app);
3088 if (!ua_sess) {
3089 /* The application has problem or is probably dead. */
3090 goto error;
3091 }
3092
3093 /* Lookup channel in the ust app session */
3094 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
3095 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
3096 if (ua_chan_node == NULL) {
3097 /* Channel does not exist, skip disabling */
3098 goto error;
3099 }
3100 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
3101
3102 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
3103 ua_event_node = lttng_ht_iter_get_node_str(&iter);
3104 if (ua_event_node == NULL) {
3105 /* Event does not exist, skip disabling */
3106 goto error;
3107 }
3108 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
3109
3110 ret = disable_ust_app_event(ua_sess, ua_event, app);
3111 if (ret < 0) {
3112 goto error;
3113 }
3114
3115 error:
3116 rcu_read_unlock();
3117 return ret;
3118 }
3119
3120 /*
3121 * Calibrate registered applications.
3122 */
3123 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
3124 {
3125 int ret = 0;
3126 struct lttng_ht_iter iter;
3127 struct ust_app *app;
3128
3129 rcu_read_lock();
3130
3131 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
3132 if (!app->compatible) {
3133 /*
3134 * TODO: In time, we should notice the caller of this error by
3135 * telling him that this is a version error.
3136 */
3137 continue;
3138 }
3139
3140 health_code_update();
3141
3142 ret = ustctl_calibrate(app->sock, calibrate);
3143 if (ret < 0) {
3144 switch (ret) {
3145 case -ENOSYS:
3146 /* Means that it's not implemented on the tracer side. */
3147 ret = 0;
3148 break;
3149 default:
3150 DBG2("Calibrate app PID %d returned with error %d",
3151 app->pid, ret);
3152 break;
3153 }
3154 }
3155 }
3156
3157 DBG("UST app global domain calibration finished");
3158
3159 rcu_read_unlock();
3160
3161 health_code_update();
3162
3163 return ret;
3164 }
3165
3166 /*
3167 * Receive registration and populate the given msg structure.
3168 *
3169 * On success return 0 else a negative value returned by the ustctl call.
3170 */
3171 int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
3172 {
3173 int ret;
3174 uint32_t pid, ppid, uid, gid;
3175
3176 assert(msg);
3177
3178 ret = ustctl_recv_reg_msg(sock, &msg->type, &msg->major, &msg->minor,
3179 &pid, &ppid, &uid, &gid,
3180 &msg->bits_per_long,
3181 &msg->uint8_t_alignment,
3182 &msg->uint16_t_alignment,
3183 &msg->uint32_t_alignment,
3184 &msg->uint64_t_alignment,
3185 &msg->long_alignment,
3186 &msg->byte_order,
3187 msg->name);
3188 if (ret < 0) {
3189 switch (-ret) {
3190 case EPIPE:
3191 case ECONNRESET:
3192 case LTTNG_UST_ERR_EXITING:
3193 DBG3("UST app recv reg message failed. Application died");
3194 break;
3195 case LTTNG_UST_ERR_UNSUP_MAJOR:
3196 ERR("UST app recv reg unsupported version %d.%d. Supporting %d.%d",
3197 msg->major, msg->minor, LTTNG_UST_ABI_MAJOR_VERSION,
3198 LTTNG_UST_ABI_MINOR_VERSION);
3199 break;
3200 default:
3201 ERR("UST app recv reg message failed with ret %d", ret);
3202 break;
3203 }
3204 goto error;
3205 }
3206 msg->pid = (pid_t) pid;
3207 msg->ppid = (pid_t) ppid;
3208 msg->uid = (uid_t) uid;
3209 msg->gid = (gid_t) gid;
3210
3211 error:
3212 return ret;
3213 }
3214
3215 static struct ust_app_channel *find_channel_by_objd(struct ust_app *app,
3216 int objd)
3217 {
3218 struct lttng_ht_node_ulong *node;
3219 struct lttng_ht_iter iter;
3220 struct ust_app_channel *ua_chan = NULL;
3221
3222 assert(app);
3223
3224 lttng_ht_lookup(app->ust_objd, (void *)((unsigned long) objd), &iter);
3225 node = lttng_ht_iter_get_node_ulong(&iter);
3226 if (node == NULL) {
3227 DBG2("UST app channel find by objd %d not found", objd);
3228 goto error;
3229 }
3230
3231 ua_chan = caa_container_of(node, struct ust_app_channel, ust_objd_node);
3232
3233 error:
3234 return ua_chan;
3235 }
3236
3237 static int reply_ust_register_channel(int sock, int sobjd, int cobjd,
3238 size_t nr_fields, struct ustctl_field *fields)
3239 {
3240 int ret, ret_code = 0;
3241 uint32_t chan_id, reg_count;
3242 enum ustctl_channel_header type;
3243 struct ust_app *app;
3244 struct ust_app_channel *ua_chan;
3245 struct ust_app_session *ua_sess;
3246
3247 rcu_read_lock();
3248
3249 /* Lookup application. If not found, there is a code flow error. */
3250 app = find_app_by_notify_sock(sock);
3251 assert(app);
3252
3253 /* Lookup channel by UST object descriptor. Should always be found. */
3254 ua_chan = find_channel_by_objd(app, cobjd);
3255 assert(ua_chan);
3256 assert(ua_chan->session);
3257 ua_sess = ua_chan->session;
3258 assert(ua_sess);
3259
3260 pthread_mutex_lock(&ua_sess->registry.lock);
3261
3262 if (ust_registry_is_max_id(ua_chan->session->registry.used_channel_id)) {
3263 ret_code = -1;
3264 chan_id = -1U;
3265 type = -1;
3266 goto reply;
3267 }
3268
3269 /* Don't assign ID to metadata. */
3270 if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) {
3271 chan_id = -1U;
3272 } else {
3273 chan_id = ust_registry_get_next_chan_id(&ua_chan->session->registry);
3274 }
3275
3276 reg_count = ust_registry_get_event_count(&ua_chan->registry);
3277 if (reg_count < 31) {
3278 type = USTCTL_CHANNEL_HEADER_COMPACT;
3279 } else {
3280 type = USTCTL_CHANNEL_HEADER_LARGE;
3281 }
3282
3283 ua_chan->registry.nr_ctx_fields = nr_fields;
3284 ua_chan->registry.ctx_fields = fields;
3285 ua_chan->registry.chan_id = chan_id;
3286 ua_chan->registry.header_type = type;
3287
3288 /* Append to metadata */
3289 if (!ret_code) {
3290 ret_code = ust_metadata_channel_statedump(&ua_chan->session->registry,
3291 &ua_chan->registry);
3292 if (ret_code) {
3293 ERR("Error appending channel metadata (errno = %d)", ret_code);
3294 goto reply;
3295 }
3296 }
3297
3298 reply:
3299 DBG3("UST app replying to register channel with id %u, type: %d, ret: %d",
3300 chan_id, type, ret_code);
3301
3302 ret = ustctl_reply_register_channel(sock, chan_id, type, ret_code);
3303 if (ret < 0) {
3304 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3305 ERR("UST app reply channel failed with ret %d", ret);
3306 } else {
3307 DBG3("UST app reply channel failed. Application died");
3308 }
3309 goto error;
3310 }
3311
3312 error:
3313 pthread_mutex_unlock(&ua_sess->registry.lock);
3314 rcu_read_unlock();
3315 return ret;
3316 }
3317
3318 static int add_event_ust_registry(int sock, int sobjd, int cobjd, char *name,
3319 char *sig, size_t nr_fields, struct ustctl_field *fields, int loglevel,
3320 char *model_emf_uri)
3321 {
3322 int ret, ret_code;
3323 uint32_t event_id = 0;
3324 struct ust_app *app;
3325 struct ust_app_channel *ua_chan;
3326 struct ust_app_session *ua_sess;
3327
3328 rcu_read_lock();
3329
3330 /* Lookup application. If not found, there is a code flow error. */
3331 app = find_app_by_notify_sock(sock);
3332 assert(app);
3333
3334 /* Lookup channel by UST object descriptor. Should always be found. */
3335 ua_chan = find_channel_by_objd(app, cobjd);
3336 assert(ua_chan);
3337 assert(ua_chan->session);
3338 ua_sess = ua_chan->session;
3339
3340 pthread_mutex_lock(&ua_sess->registry.lock);
3341
3342 ret_code = ust_registry_create_event(&ua_sess->registry, &ua_chan->registry, sobjd, cobjd,
3343 name, sig, nr_fields, fields, loglevel, model_emf_uri, &event_id);
3344
3345 /*
3346 * The return value is returned to ustctl so in case of an error, the
3347 * application can be notified. In case of an error, it's important not to
3348 * return a negative error or else the application will get closed.
3349 */
3350 ret = ustctl_reply_register_event(sock, event_id, ret_code);
3351 if (ret < 0) {
3352 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3353 ERR("UST app reply event failed with ret %d", ret);
3354 } else {
3355 DBG3("UST app reply event failed. Application died");
3356 }
3357 /*
3358 * No need to wipe the create event since the application socket will
3359 * get close on error hence cleaning up everything by itself.
3360 */
3361 goto error;
3362 }
3363
3364 error:
3365 pthread_mutex_unlock(&ua_sess->registry.lock);
3366 rcu_read_unlock();
3367 return ret;
3368 }
3369
3370 int ust_app_recv_notify(int sock)
3371 {
3372 int ret;
3373 enum ustctl_notify_cmd cmd;
3374
3375 DBG3("UST app receiving notify from sock %d", sock);
3376
3377 ret = ustctl_recv_notify(sock, &cmd);
3378 if (ret < 0) {
3379 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3380 ERR("UST app recv notify failed with ret %d", ret);
3381 } else {
3382 DBG3("UST app recv notify failed. Application died");
3383 }
3384 goto error;
3385 }
3386
3387 switch (cmd) {
3388 case USTCTL_NOTIFY_CMD_EVENT:
3389 {
3390 int sobjd, cobjd, loglevel;
3391 char name[LTTNG_UST_SYM_NAME_LEN], *sig, *model_emf_uri;
3392 size_t nr_fields;
3393 struct ustctl_field *fields;
3394
3395 DBG2("UST app ustctl register event received");
3396
3397 ret = ustctl_recv_register_event(sock, &sobjd, &cobjd, name, &loglevel,
3398 &sig, &nr_fields, &fields, &model_emf_uri);
3399 if (ret < 0) {
3400 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3401 ERR("UST app recv event failed with ret %d", ret);
3402 } else {
3403 DBG3("UST app recv event failed. Application died");
3404 }
3405 goto error;
3406 }
3407
3408 /* Add event to the UST registry coming from the notify socket. */
3409 ret = add_event_ust_registry(sock, sobjd, cobjd, name, sig, nr_fields,
3410 fields, loglevel, model_emf_uri);
3411 if (ret < 0) {
3412 goto error;
3413 }
3414
3415 break;
3416 }
3417 case USTCTL_NOTIFY_CMD_CHANNEL:
3418 {
3419 int sobjd, cobjd;
3420 size_t nr_fields;
3421 struct ustctl_field *fields;
3422
3423 DBG2("UST app ustctl register channel received");
3424
3425 ret = ustctl_recv_register_channel(sock, &sobjd, &cobjd, &nr_fields,
3426 &fields);
3427 if (ret < 0) {
3428 if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) {
3429 ERR("UST app recv channel failed with ret %d", ret);
3430 } else {
3431 DBG3("UST app recv channel failed. Application died");
3432 }
3433 goto error;
3434 }
3435
3436 ret = reply_ust_register_channel(sock, sobjd, cobjd, nr_fields,
3437 fields);
3438 if (ret < 0) {
3439 goto error;
3440 }
3441
3442 break;
3443 }
3444 default:
3445 /* Should NEVER happen. */
3446 assert(0);
3447 }
3448
3449 error:
3450 return ret;
3451 }
This page took 0.160332 seconds and 6 git commands to generate.