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