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