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