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