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