956c5dafec34e9a550d08b6bb6468e472639524b
[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 ua_sess = (void*) -1UL;
898 goto error;
899 }
900
901 ua_sess->handle = ret;
902
903 /* Add ust app session to app's HT */
904 lttng_ht_node_init_ulong(&ua_sess->node, (unsigned long) ua_sess->id);
905 lttng_ht_add_unique_ulong(app->sessions, &ua_sess->node);
906
907 DBG2("UST app session created successfully with handle %d", ret);
908 }
909
910 end:
911 return ua_sess;
912
913 error:
914 delete_ust_app_session(-1, ua_sess);
915 return NULL;
916 }
917
918 /*
919 * Create a context for the channel on the tracer.
920 */
921 static
922 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
923 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
924 struct ust_app *app)
925 {
926 int ret = 0;
927 struct lttng_ht_iter iter;
928 struct lttng_ht_node_ulong *node;
929 struct ust_app_ctx *ua_ctx;
930
931 DBG2("UST app adding context to channel %s", ua_chan->name);
932
933 lttng_ht_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx), &iter);
934 node = lttng_ht_iter_get_node_ulong(&iter);
935 if (node != NULL) {
936 ret = -EEXIST;
937 goto error;
938 }
939
940 ua_ctx = alloc_ust_app_ctx(uctx);
941 if (ua_ctx == NULL) {
942 /* malloc failed */
943 ret = -1;
944 goto error;
945 }
946
947 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
948 lttng_ht_add_unique_ulong(ua_chan->ctx, &ua_ctx->node);
949
950 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
951 if (ret < 0) {
952 goto error;
953 }
954
955 error:
956 return ret;
957 }
958
959 /*
960 * Create an UST context and enable it for the event on the tracer.
961 */
962 static
963 int create_ust_app_event_context(struct ust_app_session *ua_sess,
964 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
965 struct ust_app *app)
966 {
967 int ret = 0;
968 struct lttng_ht_iter iter;
969 struct lttng_ht_node_ulong *node;
970 struct ust_app_ctx *ua_ctx;
971
972 DBG2("UST app adding context to event %s", ua_event->name);
973
974 lttng_ht_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx), &iter);
975 node = lttng_ht_iter_get_node_ulong(&iter);
976 if (node != NULL) {
977 ret = -EEXIST;
978 goto error;
979 }
980
981 ua_ctx = alloc_ust_app_ctx(uctx);
982 if (ua_ctx == NULL) {
983 /* malloc failed */
984 ret = -1;
985 goto error;
986 }
987
988 lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx);
989 lttng_ht_add_unique_ulong(ua_event->ctx, &ua_ctx->node);
990
991 ret = create_ust_event_context(ua_event, ua_ctx, app);
992 if (ret < 0) {
993 goto error;
994 }
995
996 error:
997 return ret;
998 }
999
1000 /*
1001 * Enable on the tracer side a ust app event for the session and channel.
1002 */
1003 static
1004 int enable_ust_app_event(struct ust_app_session *ua_sess,
1005 struct ust_app_event *ua_event, struct ust_app *app)
1006 {
1007 int ret;
1008
1009 ret = enable_ust_event(app, ua_sess, ua_event);
1010 if (ret < 0) {
1011 goto error;
1012 }
1013
1014 ua_event->enabled = 1;
1015
1016 error:
1017 return ret;
1018 }
1019
1020 /*
1021 * Disable on the tracer side a ust app event for the session and channel.
1022 */
1023 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1024 struct ust_app_event *ua_event, struct ust_app *app)
1025 {
1026 int ret;
1027
1028 ret = disable_ust_event(app, ua_sess, ua_event);
1029 if (ret < 0) {
1030 goto error;
1031 }
1032
1033 ua_event->enabled = 0;
1034
1035 error:
1036 return ret;
1037 }
1038
1039 /*
1040 * Lookup ust app channel for session and disable it on the tracer side.
1041 */
1042 static
1043 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1044 struct ust_app_channel *ua_chan, struct ust_app *app)
1045 {
1046 int ret;
1047
1048 ret = disable_ust_channel(app, ua_sess, ua_chan);
1049 if (ret < 0) {
1050 goto error;
1051 }
1052
1053 ua_chan->enabled = 0;
1054
1055 error:
1056 return ret;
1057 }
1058
1059 /*
1060 * Lookup ust app channel for session and enable it on the tracer side.
1061 */
1062 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1063 struct ltt_ust_channel *uchan, struct ust_app *app)
1064 {
1065 int ret = 0;
1066 struct lttng_ht_iter iter;
1067 struct lttng_ht_node_str *ua_chan_node;
1068 struct ust_app_channel *ua_chan;
1069
1070 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1071 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1072 if (ua_chan_node == NULL) {
1073 DBG2("Unable to find channel %s in ust session id %u",
1074 uchan->name, ua_sess->id);
1075 goto error;
1076 }
1077
1078 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1079
1080 ret = enable_ust_channel(app, ua_sess, ua_chan);
1081 if (ret < 0) {
1082 goto error;
1083 }
1084
1085 error:
1086 return ret;
1087 }
1088
1089 /*
1090 * Create UST app channel and create it on the tracer.
1091 */
1092 static struct ust_app_channel *create_ust_app_channel(
1093 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1094 struct ust_app *app)
1095 {
1096 int ret = 0;
1097 struct lttng_ht_iter iter;
1098 struct lttng_ht_node_str *ua_chan_node;
1099 struct ust_app_channel *ua_chan;
1100
1101 /* Lookup channel in the ust app session */
1102 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
1103 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
1104 if (ua_chan_node != NULL) {
1105 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1106 goto end;
1107 }
1108
1109 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1110 if (ua_chan == NULL) {
1111 /* Only malloc can fail here */
1112 goto error;
1113 }
1114 shadow_copy_channel(ua_chan, uchan);
1115
1116 ret = create_ust_channel(app, ua_sess, ua_chan);
1117 if (ret < 0) {
1118 /* Not found previously means that it does not exist on the tracer */
1119 assert(ret != -EEXIST);
1120 goto error;
1121 }
1122
1123 lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node);
1124
1125 DBG2("UST app create channel %s for PID %d completed", ua_chan->name,
1126 app->pid);
1127
1128 end:
1129 return ua_chan;
1130
1131 error:
1132 delete_ust_app_channel(-1, ua_chan);
1133 return NULL;
1134 }
1135
1136 /*
1137 * Create UST app event and create it on the tracer side.
1138 */
1139 static
1140 int create_ust_app_event(struct ust_app_session *ua_sess,
1141 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1142 struct ust_app *app)
1143 {
1144 int ret = 0;
1145 struct lttng_ht_iter iter;
1146 struct lttng_ht_node_str *ua_event_node;
1147 struct ust_app_event *ua_event;
1148
1149 /* Get event node */
1150 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
1151 ua_event_node = lttng_ht_iter_get_node_str(&iter);
1152 if (ua_event_node != NULL) {
1153 ret = -EEXIST;
1154 goto end;
1155 }
1156
1157 /* Does not exist so create one */
1158 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
1159 if (ua_event == NULL) {
1160 /* Only malloc can failed so something is really wrong */
1161 ret = -ENOMEM;
1162 goto end;
1163 }
1164 shadow_copy_event(ua_event, uevent);
1165
1166 /* Create it on the tracer side */
1167 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1168 if (ret < 0) {
1169 /* Not found previously means that it does not exist on the tracer */
1170 assert(ret != -EEXIST);
1171 goto error;
1172 }
1173
1174 lttng_ht_add_unique_str(ua_chan->events, &ua_event->node);
1175
1176 DBG2("UST app create event %s for PID %d completed", ua_event->name,
1177 app->pid);
1178
1179 end:
1180 return ret;
1181
1182 error:
1183 /* Valid. Calling here is already in a read side lock */
1184 delete_ust_app_event(-1, ua_event);
1185 return ret;
1186 }
1187
1188 /*
1189 * Create UST metadata and open it on the tracer side.
1190 */
1191 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1192 char *pathname, struct ust_app *app)
1193 {
1194 int ret = 0;
1195
1196 if (ua_sess->metadata == NULL) {
1197 /* Allocate UST metadata */
1198 ua_sess->metadata = trace_ust_create_metadata(pathname);
1199 if (ua_sess->metadata == NULL) {
1200 /* malloc() failed */
1201 goto error;
1202 }
1203
1204 ret = open_ust_metadata(app, ua_sess);
1205 if (ret < 0) {
1206 DBG3("Opening metadata failed. Cleaning up memory");
1207
1208 /* Cleanup failed metadata struct */
1209 free(ua_sess->metadata);
1210 /*
1211 * This is very important because delete_ust_app_session check if
1212 * the pointer is null or not in order to delete the metadata.
1213 */
1214 ua_sess->metadata = NULL;
1215 goto error;
1216 }
1217
1218 DBG2("UST metadata opened for app pid %d", app->pid);
1219 }
1220
1221 /* Open UST metadata stream */
1222 if (ua_sess->metadata->stream_obj == NULL) {
1223 ret = create_ust_stream(app, ua_sess);
1224 if (ret < 0) {
1225 goto error;
1226 }
1227
1228 ret = run_as_mkdir(ua_sess->path, S_IRWXU | S_IRWXG,
1229 ua_sess->uid, ua_sess->gid);
1230 if (ret < 0) {
1231 PERROR("mkdir UST metadata");
1232 goto error;
1233 }
1234
1235 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1236 "%s/metadata", ua_sess->path);
1237 if (ret < 0) {
1238 PERROR("asprintf UST create stream");
1239 goto error;
1240 }
1241
1242 DBG2("UST metadata stream object created for app pid %d",
1243 app->pid);
1244 } else {
1245 ERR("Attempting to create stream without metadata opened");
1246 goto error;
1247 }
1248
1249 return 0;
1250
1251 error:
1252 return -1;
1253 }
1254
1255 /*
1256 * Return pointer to traceable apps list.
1257 */
1258 struct lttng_ht *ust_app_get_ht(void)
1259 {
1260 return ust_app_ht;
1261 }
1262
1263 /*
1264 * Return ust app pointer or NULL if not found.
1265 */
1266 struct ust_app *ust_app_find_by_pid(pid_t pid)
1267 {
1268 struct lttng_ht_node_ulong *node;
1269 struct lttng_ht_iter iter;
1270
1271 rcu_read_lock();
1272 lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter);
1273 node = lttng_ht_iter_get_node_ulong(&iter);
1274 if (node == NULL) {
1275 DBG2("UST app no found with pid %d", pid);
1276 goto error;
1277 }
1278 rcu_read_unlock();
1279
1280 DBG2("Found UST app by pid %d", pid);
1281
1282 return caa_container_of(node, struct ust_app, pid_n);
1283
1284 error:
1285 rcu_read_unlock();
1286 return NULL;
1287 }
1288
1289 /*
1290 * Using pid and uid (of the app), allocate a new ust_app struct and
1291 * add it to the global traceable app list.
1292 *
1293 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1294 * bitness is not supported.
1295 */
1296 int ust_app_register(struct ust_register_msg *msg, int sock)
1297 {
1298 struct ust_app *lta;
1299 int ret;
1300
1301 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1302 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
1303 ERR("Registration failed: application \"%s\" (pid: %d) has "
1304 "%d-bit long, but no consumerd for this long size is available.\n",
1305 msg->name, msg->pid, msg->bits_per_long);
1306 ret = close(sock);
1307 if (ret) {
1308 PERROR("close");
1309 }
1310 lttng_fd_put(LTTNG_FD_APPS, 1);
1311 return -EINVAL;
1312 }
1313 if (msg->major != LTTNG_UST_COMM_MAJOR) {
1314 ERR("Registration failed: application \"%s\" (pid: %d) has "
1315 "communication protocol version %u.%u, but sessiond supports 2.x.\n",
1316 msg->name, msg->pid, msg->major, msg->minor);
1317 ret = close(sock);
1318 if (ret) {
1319 PERROR("close");
1320 }
1321 lttng_fd_put(LTTNG_FD_APPS, 1);
1322 return -EINVAL;
1323 }
1324 lta = zmalloc(sizeof(struct ust_app));
1325 if (lta == NULL) {
1326 PERROR("malloc");
1327 return -ENOMEM;
1328 }
1329
1330 lta->ppid = msg->ppid;
1331 lta->uid = msg->uid;
1332 lta->gid = msg->gid;
1333 lta->compatible = 0; /* Not compatible until proven */
1334 lta->bits_per_long = msg->bits_per_long;
1335 lta->v_major = msg->major;
1336 lta->v_minor = msg->minor;
1337 strncpy(lta->name, msg->name, sizeof(lta->name));
1338 lta->name[16] = '\0';
1339 lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1340
1341 lta->pid = msg->pid;
1342 lttng_ht_node_init_ulong(&lta->pid_n, (unsigned long)lta->pid);
1343 lta->sock = sock;
1344 lttng_ht_node_init_ulong(&lta->sock_n, (unsigned long)lta->sock);
1345
1346 rcu_read_lock();
1347
1348 /*
1349 * On a re-registration, we want to kick out the previous registration of
1350 * that pid
1351 */
1352 lttng_ht_add_replace_ulong(ust_app_ht, &lta->pid_n);
1353
1354 /*
1355 * The socket _should_ be unique until _we_ call close. So, a add_unique
1356 * for the ust_app_ht_by_sock is used which asserts fail if the entry was
1357 * already in the table.
1358 */
1359 lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &lta->sock_n);
1360
1361 rcu_read_unlock();
1362
1363 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1364 " (version %d.%d)", lta->pid, lta->ppid, lta->uid, lta->gid,
1365 lta->sock, lta->name, lta->v_major, lta->v_minor);
1366
1367 return 0;
1368 }
1369
1370 /*
1371 * Unregister app by removing it from the global traceable app list and freeing
1372 * the data struct.
1373 *
1374 * The socket is already closed at this point so no close to sock.
1375 */
1376 void ust_app_unregister(int sock)
1377 {
1378 struct ust_app *lta;
1379 struct lttng_ht_node_ulong *node;
1380 struct lttng_ht_iter iter;
1381 int ret;
1382
1383 rcu_read_lock();
1384
1385 /* Get the node reference for a call_rcu */
1386 lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter);
1387 node = lttng_ht_iter_get_node_ulong(&iter);
1388 if (node == NULL) {
1389 ERR("Unable to find app by sock %d", sock);
1390 goto error;
1391 }
1392
1393 lta = caa_container_of(node, struct ust_app, sock_n);
1394
1395 DBG("PID %d unregistering with sock %d", lta->pid, sock);
1396
1397 /* Remove application from PID hash table */
1398 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1399 assert(!ret);
1400
1401 /* Assign second node for deletion */
1402 iter.iter.node = &lta->pid_n.node;
1403
1404 ret = lttng_ht_del(ust_app_ht, &iter);
1405 assert(!ret);
1406
1407 /* Free memory */
1408 call_rcu(&lta->pid_n.head, delete_ust_app_rcu);
1409
1410 error:
1411 rcu_read_unlock();
1412 return;
1413 }
1414
1415 /*
1416 * Return traceable_app_count
1417 */
1418 unsigned long ust_app_list_count(void)
1419 {
1420 unsigned long count;
1421
1422 rcu_read_lock();
1423 count = lttng_ht_get_count(ust_app_ht);
1424 rcu_read_unlock();
1425
1426 return count;
1427 }
1428
1429 /*
1430 * Fill events array with all events name of all registered apps.
1431 */
1432 int ust_app_list_events(struct lttng_event **events)
1433 {
1434 int ret, handle;
1435 size_t nbmem, count = 0;
1436 struct lttng_ht_iter iter;
1437 struct ust_app *app;
1438 struct lttng_event *tmp;
1439
1440 nbmem = UST_APP_EVENT_LIST_SIZE;
1441 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1442 if (tmp == NULL) {
1443 PERROR("zmalloc ust app events");
1444 ret = -ENOMEM;
1445 goto error;
1446 }
1447
1448 rcu_read_lock();
1449
1450 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1451 struct lttng_ust_tracepoint_iter uiter;
1452
1453 if (!app->compatible) {
1454 /*
1455 * TODO: In time, we should notice the caller of this error by
1456 * telling him that this is a version error.
1457 */
1458 continue;
1459 }
1460 handle = ustctl_tracepoint_list(app->sock);
1461 if (handle < 0) {
1462 ERR("UST app list events getting handle failed for app pid %d",
1463 app->pid);
1464 continue;
1465 }
1466
1467 while ((ret = ustctl_tracepoint_list_get(app->sock, handle,
1468 &uiter)) != -ENOENT) {
1469 if (count >= nbmem) {
1470 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1471 2 * nbmem);
1472 nbmem *= 2;
1473 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
1474 if (tmp == NULL) {
1475 PERROR("realloc ust app events");
1476 ret = -ENOMEM;
1477 goto rcu_error;
1478 }
1479 }
1480 memcpy(tmp[count].name, uiter.name, LTTNG_UST_SYM_NAME_LEN);
1481 tmp[count].loglevel = uiter.loglevel;
1482 tmp[count].type = (enum lttng_event_type) LTTNG_UST_TRACEPOINT;
1483 tmp[count].pid = app->pid;
1484 tmp[count].enabled = -1;
1485 count++;
1486 }
1487 }
1488
1489 ret = count;
1490 *events = tmp;
1491
1492 DBG2("UST app list events done (%zu events)", count);
1493
1494 rcu_error:
1495 rcu_read_unlock();
1496 error:
1497 return ret;
1498 }
1499
1500 /*
1501 * Fill events array with all events name of all registered apps.
1502 */
1503 int ust_app_list_event_fields(struct lttng_event_field **fields)
1504 {
1505 int ret, handle;
1506 size_t nbmem, count = 0;
1507 struct lttng_ht_iter iter;
1508 struct ust_app *app;
1509 struct lttng_event_field *tmp;
1510
1511 nbmem = UST_APP_EVENT_LIST_SIZE;
1512 tmp = zmalloc(nbmem * sizeof(struct lttng_event_field));
1513 if (tmp == NULL) {
1514 PERROR("zmalloc ust app event fields");
1515 ret = -ENOMEM;
1516 goto error;
1517 }
1518
1519 rcu_read_lock();
1520
1521 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1522 struct lttng_ust_field_iter uiter;
1523
1524 if (!app->compatible) {
1525 /*
1526 * TODO: In time, we should notice the caller of this error by
1527 * telling him that this is a version error.
1528 */
1529 continue;
1530 }
1531 handle = ustctl_tracepoint_field_list(app->sock);
1532 if (handle < 0) {
1533 ERR("UST app list event fields getting handle failed for app pid %d",
1534 app->pid);
1535 continue;
1536 }
1537
1538 while ((ret = ustctl_tracepoint_field_list_get(app->sock, handle,
1539 &uiter)) != -ENOENT) {
1540 if (count >= nbmem) {
1541 DBG2("Reallocating event field list from %zu to %zu entries", nbmem,
1542 2 * nbmem);
1543 nbmem *= 2;
1544 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event_field));
1545 if (tmp == NULL) {
1546 PERROR("realloc ust app event fields");
1547 ret = -ENOMEM;
1548 goto rcu_error;
1549 }
1550 }
1551
1552
1553 memcpy(tmp[count].field_name, uiter.field_name, LTTNG_UST_SYM_NAME_LEN);
1554 tmp[count].type = uiter.type;
1555
1556 memcpy(tmp[count].event.name, uiter.event_name, LTTNG_UST_SYM_NAME_LEN);
1557 tmp[count].event.loglevel = uiter.loglevel;
1558 tmp[count].event.type = LTTNG_UST_TRACEPOINT;
1559 tmp[count].event.pid = app->pid;
1560 tmp[count].event.enabled = -1;
1561 count++;
1562 }
1563 }
1564
1565 ret = count;
1566 *fields = tmp;
1567
1568 DBG2("UST app list event fields done (%zu events)", count);
1569
1570 rcu_error:
1571 rcu_read_unlock();
1572 error:
1573 return ret;
1574 }
1575
1576 /*
1577 * Free and clean all traceable apps of the global list.
1578 */
1579 void ust_app_clean_list(void)
1580 {
1581 int ret;
1582 struct lttng_ht_iter iter;
1583 struct lttng_ht_node_ulong *node;
1584
1585 DBG2("UST app cleaning registered apps hash table");
1586
1587 rcu_read_lock();
1588
1589 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, node, node) {
1590 ret = lttng_ht_del(ust_app_ht, &iter);
1591 assert(!ret);
1592 call_rcu(&node->head, delete_ust_app_rcu);
1593 }
1594
1595 /* Cleanup socket hash table */
1596 cds_lfht_for_each_entry(ust_app_ht_by_sock->ht, &iter.iter, node, node) {
1597 ret = lttng_ht_del(ust_app_ht_by_sock, &iter);
1598 assert(!ret);
1599 }
1600
1601 /* Destroy is done only when the ht is empty */
1602 lttng_ht_destroy(ust_app_ht);
1603 lttng_ht_destroy(ust_app_ht_by_sock);
1604
1605 rcu_read_unlock();
1606 }
1607
1608 /*
1609 * Init UST app hash table.
1610 */
1611 void ust_app_ht_alloc(void)
1612 {
1613 ust_app_ht = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1614 ust_app_ht_by_sock = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG);
1615 }
1616
1617 /*
1618 * For a specific UST session, disable the channel for all registered apps.
1619 */
1620 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1621 struct ltt_ust_channel *uchan)
1622 {
1623 int ret = 0;
1624 struct lttng_ht_iter iter;
1625 struct lttng_ht_node_str *ua_chan_node;
1626 struct ust_app *app;
1627 struct ust_app_session *ua_sess;
1628 struct ust_app_channel *ua_chan;
1629
1630 if (usess == NULL || uchan == NULL) {
1631 ERR("Disabling UST global channel with NULL values");
1632 ret = -1;
1633 goto error;
1634 }
1635
1636 DBG2("UST app disabling channel %s from global domain for session id %d",
1637 uchan->name, usess->id);
1638
1639 rcu_read_lock();
1640
1641 /* For every registered applications */
1642 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1643 struct lttng_ht_iter uiter;
1644 if (!app->compatible) {
1645 /*
1646 * TODO: In time, we should notice the caller of this error by
1647 * telling him that this is a version error.
1648 */
1649 continue;
1650 }
1651 ua_sess = lookup_session_by_app(usess, app);
1652 if (ua_sess == NULL) {
1653 continue;
1654 }
1655
1656 /* Get channel */
1657 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1658 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1659 /* If the session if found for the app, the channel must be there */
1660 assert(ua_chan_node);
1661
1662 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1663 /* The channel must not be already disabled */
1664 assert(ua_chan->enabled == 1);
1665
1666 /* Disable channel onto application */
1667 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1668 if (ret < 0) {
1669 /* XXX: We might want to report this error at some point... */
1670 continue;
1671 }
1672 }
1673
1674 rcu_read_unlock();
1675
1676 error:
1677 return ret;
1678 }
1679
1680 /*
1681 * For a specific UST session, enable the channel for all registered apps.
1682 */
1683 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1684 struct ltt_ust_channel *uchan)
1685 {
1686 int ret = 0;
1687 struct lttng_ht_iter iter;
1688 struct ust_app *app;
1689 struct ust_app_session *ua_sess;
1690
1691 if (usess == NULL || uchan == NULL) {
1692 ERR("Adding UST global channel to NULL values");
1693 ret = -1;
1694 goto error;
1695 }
1696
1697 DBG2("UST app enabling channel %s to global domain for session id %d",
1698 uchan->name, usess->id);
1699
1700 rcu_read_lock();
1701
1702 /* For every registered applications */
1703 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1704 if (!app->compatible) {
1705 /*
1706 * TODO: In time, we should notice the caller of this error by
1707 * telling him that this is a version error.
1708 */
1709 continue;
1710 }
1711 ua_sess = lookup_session_by_app(usess, app);
1712 if (ua_sess == NULL) {
1713 continue;
1714 }
1715
1716 /* Enable channel onto application */
1717 ret = enable_ust_app_channel(ua_sess, uchan, app);
1718 if (ret < 0) {
1719 /* XXX: We might want to report this error at some point... */
1720 continue;
1721 }
1722 }
1723
1724 rcu_read_unlock();
1725
1726 error:
1727 return ret;
1728 }
1729
1730 /*
1731 * Disable an event in a channel and for a specific session.
1732 */
1733 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1734 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1735 {
1736 int ret = 0;
1737 struct lttng_ht_iter iter, uiter;
1738 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
1739 struct ust_app *app;
1740 struct ust_app_session *ua_sess;
1741 struct ust_app_channel *ua_chan;
1742 struct ust_app_event *ua_event;
1743
1744 DBG("UST app disabling event %s for all apps in channel "
1745 "%s for session id %d", uevent->attr.name, uchan->name, usess->id);
1746
1747 rcu_read_lock();
1748
1749 /* For all registered applications */
1750 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1751 if (!app->compatible) {
1752 /*
1753 * TODO: In time, we should notice the caller of this error by
1754 * telling him that this is a version error.
1755 */
1756 continue;
1757 }
1758 ua_sess = lookup_session_by_app(usess, app);
1759 if (ua_sess == NULL) {
1760 /* Next app */
1761 continue;
1762 }
1763
1764 /* Lookup channel in the ust app session */
1765 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1766 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1767 if (ua_chan_node == NULL) {
1768 DBG2("Channel %s not found in session id %d for app pid %d."
1769 "Skipping", uchan->name, usess->id, app->pid);
1770 continue;
1771 }
1772 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1773
1774 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
1775 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
1776 if (ua_event_node == NULL) {
1777 DBG2("Event %s not found in channel %s for app pid %d."
1778 "Skipping", uevent->attr.name, uchan->name, app->pid);
1779 continue;
1780 }
1781 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1782
1783 ret = disable_ust_app_event(ua_sess, ua_event, app);
1784 if (ret < 0) {
1785 /* XXX: Report error someday... */
1786 continue;
1787 }
1788 }
1789
1790 rcu_read_unlock();
1791
1792 return ret;
1793 }
1794
1795 /*
1796 * For a specific UST session and UST channel, the event for all
1797 * registered apps.
1798 */
1799 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1800 struct ltt_ust_channel *uchan)
1801 {
1802 int ret = 0;
1803 struct lttng_ht_iter iter, uiter;
1804 struct lttng_ht_node_str *ua_chan_node;
1805 struct ust_app *app;
1806 struct ust_app_session *ua_sess;
1807 struct ust_app_channel *ua_chan;
1808 struct ust_app_event *ua_event;
1809
1810 DBG("UST app disabling all event for all apps in channel "
1811 "%s for session id %d", uchan->name, usess->id);
1812
1813 rcu_read_lock();
1814
1815 /* For all registered applications */
1816 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1817 if (!app->compatible) {
1818 /*
1819 * TODO: In time, we should notice the caller of this error by
1820 * telling him that this is a version error.
1821 */
1822 continue;
1823 }
1824 ua_sess = lookup_session_by_app(usess, app);
1825 /* If ua_sess is NULL, there is a code flow error */
1826 assert(ua_sess);
1827
1828 /* Lookup channel in the ust app session */
1829 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1830 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1831 /* If the channel is not found, there is a code flow error */
1832 assert(ua_chan_node);
1833
1834 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1835
1836 /* Disable each events of channel */
1837 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
1838 node.node) {
1839 ret = disable_ust_app_event(ua_sess, ua_event, app);
1840 if (ret < 0) {
1841 /* XXX: Report error someday... */
1842 continue;
1843 }
1844 }
1845 }
1846
1847 rcu_read_unlock();
1848
1849 return ret;
1850 }
1851
1852 /*
1853 * For a specific UST session, create the channel for all registered apps.
1854 */
1855 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
1856 struct ltt_ust_channel *uchan)
1857 {
1858 struct lttng_ht_iter iter;
1859 struct ust_app *app;
1860 struct ust_app_session *ua_sess;
1861 struct ust_app_channel *ua_chan;
1862
1863 /* Very wrong code flow */
1864 assert(usess);
1865 assert(uchan);
1866
1867 DBG2("UST app adding channel %s to global domain for session id %d",
1868 uchan->name, usess->id);
1869
1870 rcu_read_lock();
1871
1872 /* For every 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 /*
1882 * Create session on the tracer side and add it to app session HT. Note
1883 * that if session exist, it will simply return a pointer to the ust
1884 * app session.
1885 */
1886 ua_sess = create_ust_app_session(usess, app);
1887 if (ua_sess == NULL) {
1888 /* The malloc() failed. */
1889 goto error;
1890 } else if (ua_sess == (void *) -1UL) {
1891 /* The application's socket is not valid. Contiuing */
1892 continue;
1893 }
1894
1895 /* Create channel onto application */
1896 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1897 if (ua_chan == NULL) {
1898 /* Major problem here and it's maybe the tracer or malloc() */
1899 goto error;
1900 }
1901 }
1902
1903 rcu_read_unlock();
1904
1905 return 0;
1906
1907 error:
1908 return -1;
1909 }
1910
1911 /*
1912 * Enable event for a specific session and channel on the tracer.
1913 */
1914 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
1915 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1916 {
1917 int ret = 0;
1918 struct lttng_ht_iter iter, uiter;
1919 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
1920 struct ust_app *app;
1921 struct ust_app_session *ua_sess;
1922 struct ust_app_channel *ua_chan;
1923 struct ust_app_event *ua_event;
1924
1925 DBG("UST app enabling event %s for all apps for session id %d",
1926 uevent->attr.name, usess->id);
1927
1928 /*
1929 * NOTE: At this point, this function is called only if the session and
1930 * channel passed are already created for all apps. and enabled on the
1931 * tracer also.
1932 */
1933
1934 rcu_read_lock();
1935
1936 /* For all registered applications */
1937 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1938 if (!app->compatible) {
1939 /*
1940 * TODO: In time, we should notice the caller of this error by
1941 * telling him that this is a version error.
1942 */
1943 continue;
1944 }
1945 ua_sess = lookup_session_by_app(usess, app);
1946 /* If ua_sess is NULL, there is a code flow error */
1947 assert(ua_sess);
1948
1949 /* Lookup channel in the ust app session */
1950 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
1951 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
1952 /* If the channel is not found, there is a code flow error */
1953 assert(ua_chan_node);
1954
1955 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1956
1957 lttng_ht_lookup(ua_chan->events, (void*)uevent->attr.name, &uiter);
1958 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
1959 if (ua_event_node == NULL) {
1960 DBG3("UST app enable event %s not found for app PID %d."
1961 "Skipping app", uevent->attr.name, app->pid);
1962 continue;
1963 }
1964 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1965
1966 ret = enable_ust_app_event(ua_sess, ua_event, app);
1967 if (ret < 0) {
1968 goto error;
1969 }
1970 }
1971
1972 error:
1973 rcu_read_unlock();
1974 return ret;
1975 }
1976
1977 /*
1978 * For a specific existing UST session and UST channel, creates the event for
1979 * all registered apps.
1980 */
1981 int ust_app_create_event_glb(struct ltt_ust_session *usess,
1982 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1983 {
1984 int ret = 0;
1985 struct lttng_ht_iter iter, uiter;
1986 struct lttng_ht_node_str *ua_chan_node;
1987 struct ust_app *app;
1988 struct ust_app_session *ua_sess;
1989 struct ust_app_channel *ua_chan;
1990
1991 DBG("UST app creating event %s for all apps for session id %d",
1992 uevent->attr.name, usess->id);
1993
1994 rcu_read_lock();
1995
1996 /* For all registered applications */
1997 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
1998 if (!app->compatible) {
1999 /*
2000 * TODO: In time, we should notice the caller of this error by
2001 * telling him that this is a version error.
2002 */
2003 continue;
2004 }
2005 ua_sess = lookup_session_by_app(usess, app);
2006 /* If ua_sess is NULL, there is a code flow error */
2007 assert(ua_sess);
2008
2009 /* Lookup channel in the ust app session */
2010 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2011 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2012 /* If the channel is not found, there is a code flow error */
2013 assert(ua_chan_node);
2014
2015 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2016
2017 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2018 if (ret < 0) {
2019 if (ret != -EEXIST) {
2020 /* Possible value at this point: -ENOMEM. If so, we stop! */
2021 break;
2022 }
2023 DBG2("UST app event %s already exist on app PID %d",
2024 uevent->attr.name, app->pid);
2025 continue;
2026 }
2027 }
2028
2029 rcu_read_unlock();
2030
2031 return ret;
2032 }
2033
2034 /*
2035 * Start tracing for a specific UST session and app.
2036 */
2037 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
2038 {
2039 int ret = 0;
2040 struct lttng_ht_iter iter;
2041 struct ust_app_session *ua_sess;
2042 struct ust_app_channel *ua_chan;
2043 struct ltt_ust_stream *ustream;
2044 int consumerd_fd;
2045
2046 DBG("Starting tracing for ust app pid %d", app->pid);
2047
2048 rcu_read_lock();
2049
2050 if (!app->compatible) {
2051 goto end;
2052 }
2053
2054 ua_sess = lookup_session_by_app(usess, app);
2055 if (ua_sess == NULL) {
2056 goto error_rcu_unlock;
2057 }
2058
2059 /* Upon restart, we skip the setup, already done */
2060 if (ua_sess->started) {
2061 goto skip_setup;
2062 }
2063
2064 /* Indicate that the session has been started once */
2065 ua_sess->started = 1;
2066
2067 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
2068 if (ret < 0) {
2069 goto error_rcu_unlock;
2070 }
2071
2072 /* For each channel */
2073 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2074 node.node) {
2075 /* Create all streams */
2076 while (1) {
2077 /* Create UST stream */
2078 ustream = zmalloc(sizeof(*ustream));
2079 if (ustream == NULL) {
2080 PERROR("zmalloc ust stream");
2081 goto error_rcu_unlock;
2082 }
2083
2084 /* We are going to receive 2 fds, we need to reserve them. */
2085 ret = lttng_fd_get(LTTNG_FD_APPS, 2);
2086 if (ret < 0) {
2087 ERR("Exhausted number of available FD upon stream create");
2088 free(ustream);
2089 goto error_rcu_unlock;
2090 }
2091 ret = ustctl_create_stream(app->sock, ua_chan->obj,
2092 &ustream->obj);
2093 if (ret < 0) {
2094 /* Got all streams */
2095 lttng_fd_put(LTTNG_FD_APPS, 2);
2096 free(ustream);
2097 break;
2098 }
2099 ustream->handle = ustream->obj->handle;
2100
2101 /* Order is important */
2102 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
2103 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
2104 ua_sess->path, ua_chan->name,
2105 ua_chan->streams.count++);
2106 if (ret < 0) {
2107 PERROR("asprintf UST create stream");
2108 /*
2109 * XXX what should we do here with the
2110 * stream ?
2111 */
2112 continue;
2113 }
2114 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
2115 ustream->pathname);
2116 }
2117 }
2118
2119 switch (app->bits_per_long) {
2120 case 64:
2121 consumerd_fd = ust_consumerd64_fd;
2122 break;
2123 case 32:
2124 consumerd_fd = ust_consumerd32_fd;
2125 break;
2126 default:
2127 ret = -EINVAL;
2128 goto error_rcu_unlock;
2129 }
2130
2131 /* Setup UST consumer socket and send fds to it */
2132 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
2133 if (ret < 0) {
2134 goto error_rcu_unlock;
2135 }
2136
2137 skip_setup:
2138 /* This start the UST tracing */
2139 ret = ustctl_start_session(app->sock, ua_sess->handle);
2140 if (ret < 0) {
2141 ERR("Error starting tracing for app pid: %d", app->pid);
2142 goto error_rcu_unlock;
2143 }
2144
2145 /* Quiescent wait after starting trace */
2146 ustctl_wait_quiescent(app->sock);
2147
2148 end:
2149 rcu_read_unlock();
2150 return 0;
2151
2152 error_rcu_unlock:
2153 rcu_read_unlock();
2154 return -1;
2155 }
2156
2157 /*
2158 * Stop tracing for a specific UST session and app.
2159 */
2160 int ust_app_stop_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
2167 DBG("Stopping tracing for ust app pid %d", app->pid);
2168
2169 rcu_read_lock();
2170
2171 if (!app->compatible) {
2172 goto end;
2173 }
2174
2175 ua_sess = lookup_session_by_app(usess, app);
2176 if (ua_sess == NULL) {
2177 /* Only malloc can failed so something is really wrong */
2178 goto error_rcu_unlock;
2179 }
2180
2181 /*
2182 * If started = 0, it means that stop trace has been called for a session
2183 * that was never started. This is a code flow error and should never
2184 * happen.
2185 */
2186 assert(ua_sess->started == 1);
2187
2188 /* This inhibits UST tracing */
2189 ret = ustctl_stop_session(app->sock, ua_sess->handle);
2190 if (ret < 0) {
2191 ERR("Error stopping tracing for app pid: %d", app->pid);
2192 goto error_rcu_unlock;
2193 }
2194
2195 /* Quiescent wait after stopping trace */
2196 ustctl_wait_quiescent(app->sock);
2197
2198 /* Flushing buffers */
2199 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2200 node.node) {
2201 ret = ustctl_sock_flush_buffer(app->sock, ua_chan->obj);
2202 if (ret < 0) {
2203 ERR("UST app PID %d channel %s flush failed with ret %d",
2204 app->pid, ua_chan->name, ret);
2205 /* Continuing flushing all buffers */
2206 continue;
2207 }
2208 }
2209
2210 /* Flush all buffers before stopping */
2211 ret = ustctl_sock_flush_buffer(app->sock, ua_sess->metadata->obj);
2212 if (ret < 0) {
2213 ERR("UST app PID %d metadata flush failed with ret %d", app->pid,
2214 ret);
2215 }
2216
2217 end:
2218 rcu_read_unlock();
2219 return 0;
2220
2221 error_rcu_unlock:
2222 rcu_read_unlock();
2223 return -1;
2224 }
2225
2226 /*
2227 * Destroy a specific UST session in apps.
2228 */
2229 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
2230 {
2231 struct ust_app_session *ua_sess;
2232 struct lttng_ust_object_data obj;
2233 struct lttng_ht_iter iter;
2234 struct lttng_ht_node_ulong *node;
2235 int ret;
2236
2237 DBG("Destroy tracing for ust app pid %d", app->pid);
2238
2239 rcu_read_lock();
2240
2241 if (!app->compatible) {
2242 goto end;
2243 }
2244
2245 __lookup_session_by_app(usess, app, &iter);
2246 node = lttng_ht_iter_get_node_ulong(&iter);
2247 if (node == NULL) {
2248 /* Only malloc can failed so something is really wrong */
2249 goto error_rcu_unlock;
2250 }
2251 ua_sess = caa_container_of(node, struct ust_app_session, node);
2252 ret = lttng_ht_del(app->sessions, &iter);
2253 assert(!ret);
2254 obj.handle = ua_sess->handle;
2255 obj.shm_fd = -1;
2256 obj.wait_fd = -1;
2257 obj.memory_map_size = 0;
2258 ustctl_release_object(app->sock, &obj);
2259
2260 delete_ust_app_session(app->sock, ua_sess);
2261
2262 /* Quiescent wait after stopping trace */
2263 ustctl_wait_quiescent(app->sock);
2264
2265 end:
2266 rcu_read_unlock();
2267 return 0;
2268
2269 error_rcu_unlock:
2270 rcu_read_unlock();
2271 return -1;
2272 }
2273
2274 /*
2275 * Start tracing for the UST session.
2276 */
2277 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2278 {
2279 int ret = 0;
2280 struct lttng_ht_iter iter;
2281 struct ust_app *app;
2282
2283 DBG("Starting all UST traces");
2284
2285 rcu_read_lock();
2286
2287 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2288 ret = ust_app_start_trace(usess, app);
2289 if (ret < 0) {
2290 /* Continue to next apps even on error */
2291 continue;
2292 }
2293 }
2294
2295 rcu_read_unlock();
2296
2297 return 0;
2298 }
2299
2300 /*
2301 * Start tracing for the UST session.
2302 */
2303 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2304 {
2305 int ret = 0;
2306 struct lttng_ht_iter iter;
2307 struct ust_app *app;
2308
2309 DBG("Stopping all UST traces");
2310
2311 rcu_read_lock();
2312
2313 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2314 ret = ust_app_stop_trace(usess, app);
2315 if (ret < 0) {
2316 /* Continue to next apps even on error */
2317 continue;
2318 }
2319 }
2320
2321 rcu_read_unlock();
2322
2323 return 0;
2324 }
2325
2326 /*
2327 * Destroy app UST session.
2328 */
2329 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2330 {
2331 int ret = 0;
2332 struct lttng_ht_iter iter;
2333 struct ust_app *app;
2334
2335 DBG("Destroy all UST traces");
2336
2337 rcu_read_lock();
2338
2339 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2340 ret = ust_app_destroy_trace(usess, app);
2341 if (ret < 0) {
2342 /* Continue to next apps even on error */
2343 continue;
2344 }
2345 }
2346
2347 rcu_read_unlock();
2348
2349 return 0;
2350 }
2351
2352 /*
2353 * Add channels/events from UST global domain to registered apps at sock.
2354 */
2355 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2356 {
2357 int ret = 0;
2358 struct lttng_ht_iter iter, uiter, iter_ctx;
2359 struct ust_app *app;
2360 struct ust_app_session *ua_sess;
2361 struct ust_app_channel *ua_chan;
2362 struct ust_app_event *ua_event;
2363 struct ust_app_ctx *ua_ctx;
2364
2365 if (usess == NULL) {
2366 ERR("No UST session on global update. Returning");
2367 goto error;
2368 }
2369
2370 DBG2("UST app global update for app sock %d for session id %d", sock,
2371 usess->id);
2372
2373 rcu_read_lock();
2374
2375 app = find_app_by_sock(sock);
2376 if (app == NULL) {
2377 ERR("Failed to update app sock %d", sock);
2378 goto error;
2379 }
2380
2381 if (!app->compatible) {
2382 goto error;
2383 }
2384
2385 ua_sess = create_ust_app_session(usess, app);
2386 if (ua_sess == NULL) {
2387 goto error;
2388 }
2389
2390 /*
2391 * We can iterate safely here over all UST app session sicne the create ust
2392 * app session above made a shadow copy of the UST global domain from the
2393 * ltt ust session.
2394 */
2395 cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan,
2396 node.node) {
2397 ret = create_ust_channel(app, ua_sess, ua_chan);
2398 if (ret < 0) {
2399 /* FIXME: Should we quit here or continue... */
2400 continue;
2401 }
2402
2403 cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter_ctx.iter, ua_ctx,
2404 node.node) {
2405 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
2406 if (ret < 0) {
2407 /* FIXME: Should we quit here or continue... */
2408 continue;
2409 }
2410 }
2411
2412
2413 /* For each events */
2414 cds_lfht_for_each_entry(ua_chan->events->ht, &uiter.iter, ua_event,
2415 node.node) {
2416 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2417 if (ret < 0) {
2418 /* FIXME: Should we quit here or continue... */
2419 continue;
2420 }
2421
2422 /* Add context on events. */
2423 cds_lfht_for_each_entry(ua_event->ctx->ht, &iter_ctx.iter,
2424 ua_ctx, node.node) {
2425 ret = create_ust_event_context(ua_event, ua_ctx, app);
2426 if (ret < 0) {
2427 /* FIXME: Should we quit here or continue... */
2428 continue;
2429 }
2430 }
2431 }
2432 }
2433
2434 if (usess->start_trace) {
2435 ret = ust_app_start_trace(usess, app);
2436 if (ret < 0) {
2437 goto error;
2438 }
2439
2440 DBG2("UST trace started for app pid %d", app->pid);
2441 }
2442
2443 error:
2444 rcu_read_unlock();
2445 return;
2446 }
2447
2448 /*
2449 * Add context to a specific channel for global UST domain.
2450 */
2451 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2452 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2453 {
2454 int ret = 0;
2455 struct lttng_ht_node_str *ua_chan_node;
2456 struct lttng_ht_iter iter, uiter;
2457 struct ust_app_channel *ua_chan = NULL;
2458 struct ust_app_session *ua_sess;
2459 struct ust_app *app;
2460
2461 rcu_read_lock();
2462
2463 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2464 if (!app->compatible) {
2465 /*
2466 * TODO: In time, we should notice the caller of this error by
2467 * telling him that this is a version error.
2468 */
2469 continue;
2470 }
2471 ua_sess = lookup_session_by_app(usess, app);
2472 if (ua_sess == NULL) {
2473 continue;
2474 }
2475
2476 /* Lookup channel in the ust app session */
2477 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2478 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2479 if (ua_chan_node == NULL) {
2480 continue;
2481 }
2482 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2483 node);
2484
2485 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2486 if (ret < 0) {
2487 continue;
2488 }
2489 }
2490
2491 rcu_read_unlock();
2492 return ret;
2493 }
2494
2495 /*
2496 * Add context to a specific event in a channel for global UST domain.
2497 */
2498 int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2499 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2500 struct ltt_ust_context *uctx)
2501 {
2502 int ret = 0;
2503 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2504 struct lttng_ht_iter iter, uiter;
2505 struct ust_app_session *ua_sess;
2506 struct ust_app_event *ua_event;
2507 struct ust_app_channel *ua_chan = NULL;
2508 struct ust_app *app;
2509
2510 rcu_read_lock();
2511
2512 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2513 if (!app->compatible) {
2514 /*
2515 * TODO: In time, we should notice the caller of this error by
2516 * telling him that this is a version error.
2517 */
2518 continue;
2519 }
2520 ua_sess = lookup_session_by_app(usess, app);
2521 if (ua_sess == NULL) {
2522 continue;
2523 }
2524
2525 /* Lookup channel in the ust app session */
2526 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter);
2527 ua_chan_node = lttng_ht_iter_get_node_str(&uiter);
2528 if (ua_chan_node == NULL) {
2529 continue;
2530 }
2531 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2532 node);
2533
2534 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &uiter);
2535 ua_event_node = lttng_ht_iter_get_node_str(&uiter);
2536 if (ua_event_node == NULL) {
2537 continue;
2538 }
2539 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2540 node);
2541
2542 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2543 if (ret < 0) {
2544 continue;
2545 }
2546 }
2547
2548 rcu_read_unlock();
2549 return ret;
2550 }
2551
2552 /*
2553 * Enable event for a channel from a UST session for a specific PID.
2554 */
2555 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2556 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2557 {
2558 int ret = 0;
2559 struct lttng_ht_iter iter;
2560 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2561 struct ust_app *app;
2562 struct ust_app_session *ua_sess;
2563 struct ust_app_channel *ua_chan;
2564 struct ust_app_event *ua_event;
2565
2566 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2567
2568 rcu_read_lock();
2569
2570 app = ust_app_find_by_pid(pid);
2571 if (app == NULL) {
2572 ERR("UST app enable event per PID %d not found", pid);
2573 ret = -1;
2574 goto error;
2575 }
2576
2577 if (!app->compatible) {
2578 ret = 0;
2579 goto error;
2580 }
2581
2582 ua_sess = lookup_session_by_app(usess, app);
2583 /* If ua_sess is NULL, there is a code flow error */
2584 assert(ua_sess);
2585
2586 /* Lookup channel in the ust app session */
2587 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2588 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2589 /* If the channel is not found, there is a code flow error */
2590 assert(ua_chan_node);
2591
2592 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2593
2594 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2595 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2596 if (ua_event_node == NULL) {
2597 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2598 if (ret < 0) {
2599 goto error;
2600 }
2601 } else {
2602 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2603
2604 ret = enable_ust_app_event(ua_sess, ua_event, app);
2605 if (ret < 0) {
2606 goto error;
2607 }
2608 }
2609
2610 error:
2611 rcu_read_unlock();
2612 return ret;
2613 }
2614
2615 /*
2616 * Disable event for a channel from a UST session for a specific PID.
2617 */
2618 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2619 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2620 {
2621 int ret = 0;
2622 struct lttng_ht_iter iter;
2623 struct lttng_ht_node_str *ua_chan_node, *ua_event_node;
2624 struct ust_app *app;
2625 struct ust_app_session *ua_sess;
2626 struct ust_app_channel *ua_chan;
2627 struct ust_app_event *ua_event;
2628
2629 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2630
2631 rcu_read_lock();
2632
2633 app = ust_app_find_by_pid(pid);
2634 if (app == NULL) {
2635 ERR("UST app disable event per PID %d not found", pid);
2636 ret = -1;
2637 goto error;
2638 }
2639
2640 if (!app->compatible) {
2641 ret = 0;
2642 goto error;
2643 }
2644
2645 ua_sess = lookup_session_by_app(usess, app);
2646 /* If ua_sess is NULL, there is a code flow error */
2647 assert(ua_sess);
2648
2649 /* Lookup channel in the ust app session */
2650 lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter);
2651 ua_chan_node = lttng_ht_iter_get_node_str(&iter);
2652 if (ua_chan_node == NULL) {
2653 /* Channel does not exist, skip disabling */
2654 goto error;
2655 }
2656 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2657
2658 lttng_ht_lookup(ua_chan->events, (void *)uevent->attr.name, &iter);
2659 ua_event_node = lttng_ht_iter_get_node_str(&iter);
2660 if (ua_event_node == NULL) {
2661 /* Event does not exist, skip disabling */
2662 goto error;
2663 }
2664 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2665
2666 ret = disable_ust_app_event(ua_sess, ua_event, app);
2667 if (ret < 0) {
2668 goto error;
2669 }
2670
2671 error:
2672 rcu_read_unlock();
2673 return ret;
2674 }
2675
2676 /*
2677 * Validate version of UST apps and set the compatible bit.
2678 */
2679 int ust_app_validate_version(int sock)
2680 {
2681 int ret;
2682 struct ust_app *app;
2683
2684 rcu_read_lock();
2685
2686 app = find_app_by_sock(sock);
2687 assert(app);
2688
2689 ret = ustctl_tracer_version(sock, &app->version);
2690 if (ret < 0) {
2691 goto error;
2692 }
2693
2694 /* Validate version */
2695 if (app->version.major > UST_APP_MAJOR_VERSION) {
2696 goto error;
2697 }
2698
2699 DBG2("UST app PID %d is compatible with major version %d "
2700 "(supporting <= %d)", app->pid, app->version.major,
2701 UST_APP_MAJOR_VERSION);
2702 app->compatible = 1;
2703 rcu_read_unlock();
2704 return 0;
2705
2706 error:
2707 DBG2("UST app PID %d is not compatible with major version %d "
2708 "(supporting <= %d)", app->pid, app->version.major,
2709 UST_APP_MAJOR_VERSION);
2710 app->compatible = 0;
2711 rcu_read_unlock();
2712 return -1;
2713 }
2714
2715 /*
2716 * Calibrate registered applications.
2717 */
2718 int ust_app_calibrate_glb(struct lttng_ust_calibrate *calibrate)
2719 {
2720 int ret = 0;
2721 struct lttng_ht_iter iter;
2722 struct ust_app *app;
2723
2724 rcu_read_lock();
2725
2726 cds_lfht_for_each_entry(ust_app_ht->ht, &iter.iter, app, pid_n.node) {
2727 if (!app->compatible) {
2728 /*
2729 * TODO: In time, we should notice the caller of this error by
2730 * telling him that this is a version error.
2731 */
2732 continue;
2733 }
2734
2735 ret = ustctl_calibrate(app->sock, calibrate);
2736 if (ret < 0) {
2737 switch (ret) {
2738 case -ENOSYS:
2739 /* Means that it's not implemented on the tracer side. */
2740 ret = 0;
2741 break;
2742 default:
2743 /* TODO: Report error to user */
2744 DBG2("Calibrate app PID %d returned with error %d",
2745 app->pid, ret);
2746 break;
2747 }
2748 }
2749 }
2750
2751 DBG("UST app global domain calibration finished");
2752
2753 rcu_read_unlock();
2754
2755 return ret;
2756 }
This page took 0.145228 seconds and 3 git commands to generate.