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