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