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