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