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