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