Flush buffers on UST stop trace
[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 ua_sess->metadata->handle = ua_sess->metadata->obj->handle;
569
570 error:
571 return ret;
572 }
573
574 /*
575 * Create stream onto the UST tracer for a UST session.
576 */
577 static int create_ust_stream(struct ust_app *app,
578 struct ust_app_session *ua_sess)
579 {
580 int ret;
581
582 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
583 &ua_sess->metadata->stream_obj);
584 if (ret < 0) {
585 ERR("UST create metadata stream failed");
586 goto error;
587 }
588
589 error:
590 return ret;
591 }
592
593 /*
594 * Create the specified channel onto the UST tracer for a UST session.
595 */
596 static int create_ust_channel(struct ust_app *app,
597 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
598 {
599 int ret;
600
601 /* TODO: remove cast and use lttng-ust-abi.h */
602 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
603 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
604 if (ret < 0) {
605 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
606 "and session handle %d with ret %d",
607 ua_chan->name, app->key.pid, app->key.sock,
608 ua_sess->handle, ret);
609 goto error;
610 }
611
612 ua_chan->handle = ua_chan->obj->handle;
613 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
614 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
615 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
616
617 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
618 ua_chan->name, app->key.pid, app->key.sock);
619
620 /* If channel is not enabled, disable it on the tracer */
621 if (!ua_chan->enabled) {
622 ret = disable_ust_channel(app, ua_sess, ua_chan);
623 if (ret < 0) {
624 goto error;
625 }
626 }
627
628 error:
629 return ret;
630 }
631
632 /*
633 * Create the specified event onto the UST tracer for a UST session.
634 */
635 static
636 int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess,
637 struct ust_app_channel *ua_chan, struct ust_app_event *ua_event)
638 {
639 int ret = 0;
640
641 /* Create UST event on tracer */
642 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
643 &ua_event->obj);
644 if (ret < 0) {
645 ERR("Error ustctl create event %s for app pid: %d with ret %d",
646 ua_event->attr.name, app->key.pid, ret);
647 goto error;
648 }
649
650 ua_event->handle = ua_event->obj->handle;
651
652 DBG2("UST app event %s created successfully for pid:%d",
653 ua_event->attr.name, app->key.pid);
654
655 /* If event not enabled, disable it on the tracer */
656 if (!ua_event->enabled) {
657 ret = disable_ust_event(app, ua_sess, ua_event);
658 if (ret < 0) {
659 goto error;
660 }
661 }
662
663 error:
664 return ret;
665 }
666
667 /*
668 * Copy data between an UST app event and a LTT event.
669 */
670 static void shadow_copy_event(struct ust_app_event *ua_event,
671 struct ltt_ust_event *uevent)
672 {
673 struct cds_lfht_iter iter;
674 struct ltt_ust_context *uctx;
675 struct ust_app_ctx *ua_ctx;
676
677 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
678 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
679
680 /* Copy event attributes */
681 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
682
683 cds_lfht_for_each_entry(uevent->ctx, &iter, uctx, node) {
684 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
685 if (ua_ctx == NULL) {
686 continue;
687 }
688 hashtable_node_init(&ua_ctx->node,
689 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
690 hashtable_add_unique(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 cds_lfht_iter iter;
701 struct cds_lfht_node *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("Shadow copy of UST app channel %s", 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 cds_lfht_for_each_entry(uchan->ctx, &iter, uctx, node) {
715 ua_ctx = alloc_ust_app_ctx(&uctx->ctx);
716 if (ua_ctx == NULL) {
717 continue;
718 }
719 hashtable_node_init(&ua_ctx->node,
720 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
721 hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
722 }
723
724 /* Copy all events from ltt ust channel to ust app channel */
725 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
726 struct cds_lfht_iter uiter;
727
728 ua_event_node = hashtable_lookup(ua_chan->events,
729 (void *) uevent->attr.name, strlen(uevent->attr.name),
730 &uiter);
731 if (ua_event_node == NULL) {
732 DBG2("UST event %s not found on shadow copy channel",
733 uevent->attr.name);
734 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
735 if (ua_event == NULL) {
736 continue;
737 }
738 shadow_copy_event(ua_event, uevent);
739 hashtable_add_unique(ua_chan->events, &ua_event->node);
740 }
741 }
742
743 DBG3("Shadow copy channel done");
744 }
745
746 /*
747 * Copy data between a UST app session and a regular LTT session.
748 */
749 static void shadow_copy_session(struct ust_app_session *ua_sess,
750 struct ltt_ust_session *usess,
751 struct ust_app *app)
752 {
753 struct cds_lfht_node *ua_chan_node;
754 struct cds_lfht_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->uid = usess->uid;
770
771 ret = snprintf(ua_sess->path, PATH_MAX,
772 "%s/%s-%d-%s",
773 usess->pathname, app->name, app->key.pid,
774 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, &iter,
785 uchan, node) {
786 struct cds_lfht_iter uiter;
787
788 ua_chan_node = hashtable_lookup(ua_sess->channels,
789 (void *)uchan->name, strlen(uchan->name),
790 &uiter);
791 if (ua_chan_node != NULL) {
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... continuing */
800 continue;
801 }
802
803 shadow_copy_channel(ua_chan, uchan);
804 hashtable_add_unique(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 cds_lfht_iter *iter)
814 {
815 /* Get right UST app session from app */
816 (void) hashtable_lookup(app->sessions,
817 (void *) ((unsigned long) usess->uid), sizeof(void *),
818 iter);
819 }
820
821 /*
822 * Return ust app session from the app session hashtable using the UST session
823 * uid.
824 */
825 static struct ust_app_session *lookup_session_by_app(
826 struct ltt_ust_session *usess, struct ust_app *app)
827 {
828 struct cds_lfht_iter iter;
829 struct cds_lfht_node *node;
830
831 __lookup_session_by_app(usess, app, &iter);
832 node = hashtable_iter_get_node(&iter);
833 if (node == NULL) {
834 goto error;
835 }
836
837 return caa_container_of(node, struct ust_app_session, node);
838
839 error:
840 return NULL;
841 }
842
843 /*
844 * Create a UST session onto the tracer of app and add it the session
845 * hashtable.
846 *
847 * Return ust app session or NULL on error.
848 */
849 static struct ust_app_session *create_ust_app_session(
850 struct ltt_ust_session *usess, struct ust_app *app)
851 {
852 int ret;
853 struct ust_app_session *ua_sess;
854
855 ua_sess = lookup_session_by_app(usess, app);
856 if (ua_sess == NULL) {
857 DBG2("UST app pid: %d session uid %d not found, creating it",
858 app->key.pid, usess->uid);
859 ua_sess = alloc_ust_app_session();
860 if (ua_sess == NULL) {
861 /* Only malloc can failed so something is really wrong */
862 goto error;
863 }
864 shadow_copy_session(ua_sess, usess, app);
865 }
866
867 if (ua_sess->handle == -1) {
868 ret = ustctl_create_session(app->key.sock);
869 if (ret < 0) {
870 ERR("Error creating session for app pid %d, sock %d",
871 app->key.pid, app->key.sock);
872 /* TODO: free() ua_sess */
873 goto error;
874 }
875
876 DBG2("UST app ustctl create session handle %d", ret);
877 ua_sess->handle = ret;
878
879 /* Add ust app session to app's HT */
880 hashtable_node_init(&ua_sess->node,
881 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
882 hashtable_add_unique(app->sessions, &ua_sess->node);
883
884 DBG2("UST app session created successfully with handle %d", ret);
885 }
886
887 return ua_sess;
888
889 error:
890 return NULL;
891 }
892
893 /*
894 * Create a context for the channel on the tracer.
895 */
896 static
897 int create_ust_app_channel_context(struct ust_app_session *ua_sess,
898 struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx,
899 struct ust_app *app)
900 {
901 int ret = 0;
902 struct cds_lfht_iter iter;
903 struct cds_lfht_node *node;
904 struct ust_app_ctx *ua_ctx;
905
906 DBG2("UST app adding context to channel %s", ua_chan->name);
907
908 node = hashtable_lookup(ua_chan->ctx, (void *)((unsigned long)uctx->ctx),
909 sizeof(void *), &iter);
910 if (node != NULL) {
911 ret = -EEXIST;
912 goto error;
913 }
914
915 ua_ctx = alloc_ust_app_ctx(uctx);
916 if (ua_ctx == NULL) {
917 /* malloc failed */
918 ret = -1;
919 goto error;
920 }
921
922 hashtable_node_init(&ua_ctx->node,
923 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
924 hashtable_add_unique(ua_chan->ctx, &ua_ctx->node);
925
926 ret = create_ust_channel_context(ua_chan, ua_ctx, app);
927 if (ret < 0) {
928 goto error;
929 }
930
931 error:
932 return ret;
933 }
934
935 /*
936 * Create an UST context and enable it for the event on the tracer.
937 */
938 static
939 int create_ust_app_event_context(struct ust_app_session *ua_sess,
940 struct ust_app_event *ua_event, struct lttng_ust_context *uctx,
941 struct ust_app *app)
942 {
943 int ret = 0;
944 struct cds_lfht_iter iter;
945 struct cds_lfht_node *node;
946 struct ust_app_ctx *ua_ctx;
947
948 DBG2("UST app adding context to event %s", ua_event->name);
949
950 node = hashtable_lookup(ua_event->ctx, (void *)((unsigned long)uctx->ctx),
951 sizeof(void *), &iter);
952 if (node != NULL) {
953 ret = -EEXIST;
954 goto error;
955 }
956
957 ua_ctx = alloc_ust_app_ctx(uctx);
958 if (ua_ctx == NULL) {
959 /* malloc failed */
960 ret = -1;
961 goto error;
962 }
963
964 hashtable_node_init(&ua_ctx->node,
965 (void *)((unsigned long) ua_ctx->ctx.ctx), sizeof(void *));
966 hashtable_add_unique(ua_event->ctx, &ua_ctx->node);
967
968 ret = create_ust_event_context(ua_event, ua_ctx, app);
969 if (ret < 0) {
970 goto error;
971 }
972
973 error:
974 return ret;
975 }
976
977 /*
978 * Enable on the tracer side a ust app event for the session and channel.
979 */
980 static
981 int enable_ust_app_event(struct ust_app_session *ua_sess,
982 struct ust_app_event *ua_event, struct ust_app *app)
983 {
984 int ret;
985
986 ret = enable_ust_event(app, ua_sess, ua_event);
987 if (ret < 0) {
988 goto error;
989 }
990
991 ua_event->enabled = 1;
992
993 error:
994 return ret;
995 }
996
997 /*
998 * Disable on the tracer side a ust app event for the session and channel.
999 */
1000 static int disable_ust_app_event(struct ust_app_session *ua_sess,
1001 struct ust_app_event *ua_event, struct ust_app *app)
1002 {
1003 int ret;
1004
1005 ret = disable_ust_event(app, ua_sess, ua_event);
1006 if (ret < 0) {
1007 goto error;
1008 }
1009
1010 ua_event->enabled = 0;
1011
1012 error:
1013 return ret;
1014 }
1015
1016 /*
1017 * Lookup ust app channel for session and disable it on the tracer side.
1018 */
1019 static
1020 int disable_ust_app_channel(struct ust_app_session *ua_sess,
1021 struct ust_app_channel *ua_chan, struct ust_app *app)
1022 {
1023 int ret;
1024
1025 ret = disable_ust_channel(app, ua_sess, ua_chan);
1026 if (ret < 0) {
1027 goto error;
1028 }
1029
1030 ua_chan->enabled = 0;
1031
1032 error:
1033 return ret;
1034 }
1035
1036 /*
1037 * Lookup ust app channel for session and enable it on the tracer side.
1038 */
1039 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
1040 struct ltt_ust_channel *uchan, struct ust_app *app)
1041 {
1042 int ret = 0;
1043 struct cds_lfht_iter iter;
1044 struct cds_lfht_node *ua_chan_node;
1045 struct ust_app_channel *ua_chan;
1046
1047 ua_chan_node = hashtable_lookup(ua_sess->channels,
1048 (void *)uchan->name, strlen(uchan->name), &iter);
1049 if (ua_chan_node == NULL) {
1050 DBG2("Unable to find channel %s in ust session uid %u",
1051 uchan->name, ua_sess->uid);
1052 goto error;
1053 }
1054
1055 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1056
1057 ret = enable_ust_channel(app, ua_sess, ua_chan);
1058 if (ret < 0) {
1059 goto error;
1060 }
1061
1062 error:
1063 return ret;
1064 }
1065
1066 /*
1067 * Create UST app channel and create it on the tracer.
1068 */
1069 static struct ust_app_channel *create_ust_app_channel(
1070 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
1071 struct ust_app *app)
1072 {
1073 int ret = 0;
1074 struct cds_lfht_iter iter;
1075 struct cds_lfht_node *ua_chan_node;
1076 struct ust_app_channel *ua_chan;
1077
1078 /* Lookup channel in the ust app session */
1079 ua_chan_node = hashtable_lookup(ua_sess->channels,
1080 (void *)uchan->name, strlen(uchan->name), &iter);
1081 if (ua_chan_node == NULL) {
1082 DBG2("Unable to find channel %s in ust session uid %u",
1083 uchan->name, ua_sess->uid);
1084 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
1085 if (ua_chan == NULL) {
1086 goto error;
1087 }
1088 shadow_copy_channel(ua_chan, uchan);
1089
1090 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
1091 } else {
1092 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1093 }
1094
1095 ret = create_ust_channel(app, ua_sess, ua_chan);
1096 if (ret < 0) {
1097 goto error;
1098 }
1099
1100 return ua_chan;
1101
1102 error:
1103 return NULL;
1104 }
1105
1106 /*
1107 * Create UST app event and create it on the tracer side.
1108 */
1109 static
1110 int create_ust_app_event(struct ust_app_session *ua_sess,
1111 struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent,
1112 struct ust_app *app)
1113 {
1114 int ret = 0;
1115 struct cds_lfht_iter iter;
1116 struct cds_lfht_node *ua_event_node;
1117 struct ust_app_event *ua_event;
1118
1119 /* Get event node */
1120 ua_event_node = hashtable_lookup(ua_chan->events,
1121 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
1122 if (ua_event_node != NULL) {
1123 ERR("UST app event %s already exist. Stopping creation.",
1124 uevent->attr.name);
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 error;
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 rcu_read_lock();
1141 delete_ust_app_event(app->key.sock, ua_event);
1142 rcu_read_unlock();
1143 goto error;
1144 }
1145
1146 ua_event->enabled = 1;
1147
1148 hashtable_add_unique(ua_chan->events, &ua_event->node);
1149
1150 DBG2("UST app create event %s for PID %d completed",
1151 ua_event->name, app->key.pid);
1152
1153 end:
1154 error:
1155 return ret;
1156 }
1157
1158 /*
1159 * Create UST metadata and open it on the tracer side.
1160 */
1161 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
1162 char *pathname, struct ust_app *app)
1163 {
1164 int ret = 0;
1165 mode_t old_umask;
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 ERR("UST app session %d creating metadata failed",
1172 ua_sess->handle);
1173 goto error;
1174 }
1175
1176 ret = open_ust_metadata(app, ua_sess);
1177 if (ret < 0) {
1178 goto error;
1179 }
1180
1181 DBG2("UST metadata opened for app pid %d", app->key.pid);
1182 }
1183
1184 /* Open UST metadata stream */
1185 if (ua_sess->metadata->stream_obj == NULL) {
1186 ret = create_ust_stream(app, ua_sess);
1187 if (ret < 0) {
1188 goto error;
1189 }
1190
1191 old_umask = umask(0);
1192 ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
1193 if (ret < 0) {
1194 PERROR("mkdir UST metadata");
1195 goto error;
1196 }
1197 umask(old_umask);
1198
1199 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
1200 "%s/metadata", ua_sess->path);
1201 if (ret < 0) {
1202 PERROR("asprintf UST create stream");
1203 goto error;
1204 }
1205
1206 DBG2("UST metadata stream object created for app pid %d",
1207 app->key.pid);
1208 } else {
1209 ERR("Attempting to create stream without metadata opened");
1210 goto error;
1211 }
1212
1213 return 0;
1214
1215 error:
1216 return -1;
1217 }
1218
1219 /*
1220 * Return pointer to traceable apps list.
1221 */
1222 struct cds_lfht *ust_app_get_ht(void)
1223 {
1224 return ust_app_ht;
1225 }
1226
1227 /*
1228 * Return ust app pointer or NULL if not found.
1229 */
1230 struct ust_app *ust_app_find_by_pid(pid_t pid)
1231 {
1232 struct cds_lfht_node *node;
1233 struct cds_lfht_iter iter;
1234
1235 rcu_read_lock();
1236 node = hashtable_lookup(ust_app_ht,
1237 (void *)((unsigned long) pid), sizeof(void *), &iter);
1238 if (node == NULL) {
1239 DBG2("UST app no found with pid %d", pid);
1240 goto error;
1241 }
1242 rcu_read_unlock();
1243
1244 DBG2("Found UST app by pid %d", pid);
1245
1246 return caa_container_of(node, struct ust_app, node);
1247
1248 error:
1249 rcu_read_unlock();
1250 return NULL;
1251 }
1252
1253 /*
1254 * Using pid and uid (of the app), allocate a new ust_app struct and
1255 * add it to the global traceable app list.
1256 *
1257 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
1258 * bitness is not supported.
1259 */
1260 int ust_app_register(struct ust_register_msg *msg, int sock)
1261 {
1262 struct ust_app *lta;
1263
1264 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
1265 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
1266 ERR("Registration failed: application \"%s\" (pid: %d) has "
1267 "%d-bit long, but no consumerd for this long size is available.\n",
1268 msg->name, msg->pid, msg->bits_per_long);
1269 close(sock);
1270 return -EINVAL;
1271 }
1272 lta = zmalloc(sizeof(struct ust_app));
1273 if (lta == NULL) {
1274 PERROR("malloc");
1275 return -ENOMEM;
1276 }
1277
1278 lta->ppid = msg->ppid;
1279 lta->uid = msg->uid;
1280 lta->gid = msg->gid;
1281 lta->bits_per_long = msg->bits_per_long;
1282 lta->v_major = msg->major;
1283 lta->v_minor = msg->minor;
1284 strncpy(lta->name, msg->name, sizeof(lta->name));
1285 lta->name[16] = '\0';
1286 lta->sessions = hashtable_new(0);
1287
1288 /* Set key map */
1289 lta->key.pid = msg->pid;
1290 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
1291 sizeof(void *));
1292 lta->key.sock = sock;
1293 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
1294 sizeof(void *));
1295
1296 rcu_read_lock();
1297 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
1298 hashtable_add_unique(ust_app_ht, &lta->node);
1299 rcu_read_unlock();
1300
1301 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
1302 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
1303 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
1304
1305 return 0;
1306 }
1307
1308 /*
1309 * Unregister app by removing it from the global traceable app list and freeing
1310 * the data struct.
1311 *
1312 * The socket is already closed at this point so no close to sock.
1313 */
1314 void ust_app_unregister(int sock)
1315 {
1316 struct ust_app *lta;
1317 struct cds_lfht_node *node;
1318 struct cds_lfht_iter iter;
1319 int ret;
1320
1321 rcu_read_lock();
1322 lta = find_app_by_sock(sock);
1323 if (lta == NULL) {
1324 ERR("Unregister app sock %d not found!", sock);
1325 goto error;
1326 }
1327
1328 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
1329
1330 /* Get the node reference for a call_rcu */
1331 node = hashtable_lookup(ust_app_ht,
1332 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
1333 if (node == NULL) {
1334 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
1335 goto error;
1336 }
1337
1338 ret = hashtable_del(ust_app_ht, &iter);
1339 assert(!ret);
1340 call_rcu(&node->head, delete_ust_app_rcu);
1341 error:
1342 rcu_read_unlock();
1343 return;
1344 }
1345
1346 /*
1347 * Return traceable_app_count
1348 */
1349 unsigned long ust_app_list_count(void)
1350 {
1351 unsigned long count;
1352
1353 rcu_read_lock();
1354 count = hashtable_get_count(ust_app_ht);
1355 rcu_read_unlock();
1356
1357 return count;
1358 }
1359
1360 /*
1361 * Fill events array with all events name of all registered apps.
1362 */
1363 int ust_app_list_events(struct lttng_event **events)
1364 {
1365 int ret, handle;
1366 size_t nbmem, count = 0;
1367 struct cds_lfht_iter iter;
1368 struct ust_app *app;
1369 struct lttng_event *tmp;
1370
1371 nbmem = UST_APP_EVENT_LIST_SIZE;
1372 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1373 if (tmp == NULL) {
1374 PERROR("zmalloc ust app events");
1375 ret = -ENOMEM;
1376 goto error;
1377 }
1378
1379 rcu_read_lock();
1380
1381 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1382 struct lttng_ust_tracepoint_iter iter;
1383
1384 handle = ustctl_tracepoint_list(app->key.sock);
1385 if (handle < 0) {
1386 ERR("UST app list events getting handle failed for app pid %d",
1387 app->key.pid);
1388 continue;
1389 }
1390
1391 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
1392 &iter)) != -ENOENT) {
1393 if (count >= nbmem) {
1394 DBG2("Reallocating event list from %zu to %zu entries", nbmem,
1395 2 * nbmem);
1396 nbmem *= 2;
1397 tmp = realloc(tmp, nbmem * sizeof(struct lttng_event));
1398 if (tmp == NULL) {
1399 PERROR("realloc ust app events");
1400 ret = -ENOMEM;
1401 goto rcu_error;
1402 }
1403 }
1404 memcpy(tmp[count].name, iter.name, LTTNG_UST_SYM_NAME_LEN);
1405 memcpy(tmp[count].loglevel, iter.loglevel, LTTNG_UST_SYM_NAME_LEN);
1406 tmp[count].loglevel_value = iter.loglevel_value;
1407 tmp[count].type = LTTNG_UST_TRACEPOINT;
1408 tmp[count].pid = app->key.pid;
1409 tmp[count].enabled = -1;
1410 count++;
1411 }
1412 }
1413
1414 ret = count;
1415 *events = tmp;
1416
1417 DBG2("UST app list events done (%zu events)", count);
1418
1419 rcu_error:
1420 rcu_read_unlock();
1421 error:
1422 return ret;
1423 }
1424
1425 /*
1426 * Free and clean all traceable apps of the global list.
1427 */
1428 void ust_app_clean_list(void)
1429 {
1430 int ret;
1431 struct cds_lfht_iter iter;
1432 struct ust_app *app;
1433
1434 DBG2("UST app cleaning registered apps hash table");
1435
1436 rcu_read_lock();
1437
1438 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1439 ret = hashtable_del(ust_app_ht, &iter);
1440 assert(!ret);
1441 call_rcu(&iter.node->head, delete_ust_app_rcu);
1442 }
1443
1444 hashtable_destroy(ust_app_ht);
1445 hashtable_destroy(ust_app_sock_key_map);
1446
1447 rcu_read_unlock();
1448 }
1449
1450 /*
1451 * Init UST app hash table.
1452 */
1453 void ust_app_ht_alloc(void)
1454 {
1455 ust_app_ht = hashtable_new(0);
1456 ust_app_sock_key_map = hashtable_new(0);
1457 }
1458
1459 /*
1460 * For a specific UST session, disable the channel for all registered apps.
1461 */
1462 int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
1463 struct ltt_ust_channel *uchan)
1464 {
1465 int ret = 0;
1466 struct cds_lfht_iter iter;
1467 struct cds_lfht_node *ua_chan_node;
1468 struct ust_app *app;
1469 struct ust_app_session *ua_sess;
1470 struct ust_app_channel *ua_chan;
1471
1472 if (usess == NULL || uchan == NULL) {
1473 ERR("Disabling UST global channel with NULL values");
1474 ret = -1;
1475 goto error;
1476 }
1477
1478 DBG2("UST app disabling channel %s from global domain for session uid %d",
1479 uchan->name, usess->uid);
1480
1481 rcu_read_lock();
1482
1483 /* For every registered applications */
1484 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1485 ua_sess = lookup_session_by_app(usess, app);
1486 if (ua_sess == NULL) {
1487 continue;
1488 }
1489
1490 /* Get channel */
1491 ua_chan_node = hashtable_lookup(ua_sess->channels,
1492 (void *)uchan->name, strlen(uchan->name), &iter);
1493 /* If the session if found for the app, the channel must be there */
1494 assert(ua_chan_node);
1495
1496 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1497 /* The channel must not be already disabled */
1498 assert(ua_chan->enabled == 1);
1499
1500 /* Disable channel onto application */
1501 ret = disable_ust_app_channel(ua_sess, ua_chan, app);
1502 if (ret < 0) {
1503 /* XXX: We might want to report this error at some point... */
1504 continue;
1505 }
1506 }
1507
1508 rcu_read_unlock();
1509
1510 error:
1511 return ret;
1512 }
1513
1514 /*
1515 * For a specific UST session, enable the channel for all registered apps.
1516 */
1517 int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
1518 struct ltt_ust_channel *uchan)
1519 {
1520 int ret = 0;
1521 struct cds_lfht_iter iter;
1522 struct ust_app *app;
1523 struct ust_app_session *ua_sess;
1524
1525 if (usess == NULL || uchan == NULL) {
1526 ERR("Adding UST global channel to NULL values");
1527 ret = -1;
1528 goto error;
1529 }
1530
1531 DBG2("UST app enabling channel %s to global domain for session uid %d",
1532 uchan->name, usess->uid);
1533
1534 rcu_read_lock();
1535
1536 /* For every registered applications */
1537 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1538 ua_sess = lookup_session_by_app(usess, app);
1539 if (ua_sess == NULL) {
1540 continue;
1541 }
1542
1543 /* Enable channel onto application */
1544 ret = enable_ust_app_channel(ua_sess, uchan, app);
1545 if (ret < 0) {
1546 /* XXX: We might want to report this error at some point... */
1547 continue;
1548 }
1549 }
1550
1551 rcu_read_unlock();
1552
1553 error:
1554 return ret;
1555 }
1556
1557 /*
1558 * Disable an event in a channel and for a specific session.
1559 */
1560 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
1561 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1562 {
1563 int ret = 0;
1564 struct cds_lfht_iter iter, uiter;
1565 struct cds_lfht_node *ua_chan_node, *ua_event_node;
1566 struct ust_app *app;
1567 struct ust_app_session *ua_sess;
1568 struct ust_app_channel *ua_chan;
1569 struct ust_app_event *ua_event;
1570
1571 DBG("UST app disabling event %s for all apps in channel "
1572 "%s for session uid %d", uevent->attr.name, uchan->name, usess->uid);
1573
1574 rcu_read_lock();
1575
1576 /* For all registered applications */
1577 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1578 ua_sess = lookup_session_by_app(usess, app);
1579 if (ua_sess == NULL) {
1580 /* Next app */
1581 continue;
1582 }
1583
1584 /* Lookup channel in the ust app session */
1585 ua_chan_node = hashtable_lookup(ua_sess->channels,
1586 (void *)uchan->name, strlen(uchan->name), &uiter);
1587 if (ua_chan_node == NULL) {
1588 DBG2("Channel %s not found in session uid %d for app pid %d."
1589 "Skipping", uchan->name, usess->uid, app->key.pid);
1590 continue;
1591 }
1592 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1593
1594 ua_event_node = hashtable_lookup(ua_chan->events,
1595 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
1596 if (ua_event_node == NULL) {
1597 DBG2("Event %s not found in channel %s for app pid %d."
1598 "Skipping", uevent->attr.name, uchan->name, app->key.pid);
1599 continue;
1600 }
1601 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1602
1603 ret = disable_ust_app_event(ua_sess, ua_event, app);
1604 if (ret < 0) {
1605 /* XXX: Report error someday... */
1606 continue;
1607 }
1608 }
1609
1610 rcu_read_unlock();
1611
1612 return ret;
1613 }
1614
1615 /*
1616 * For a specific UST session and UST channel, the event for all
1617 * registered apps.
1618 */
1619 int ust_app_disable_all_event_glb(struct ltt_ust_session *usess,
1620 struct ltt_ust_channel *uchan)
1621 {
1622 int ret = 0;
1623 struct cds_lfht_iter iter, uiter;
1624 struct cds_lfht_node *ua_chan_node;
1625 struct ust_app *app;
1626 struct ust_app_session *ua_sess;
1627 struct ust_app_channel *ua_chan;
1628 struct ust_app_event *ua_event;
1629
1630 DBG("UST app disabling all event for all apps in channel "
1631 "%s for session uid %d", uchan->name, usess->uid);
1632
1633 rcu_read_lock();
1634
1635 /* For all registered applications */
1636 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1637 ua_sess = lookup_session_by_app(usess, app);
1638 /* If ua_sess is NULL, there is a code flow error */
1639 assert(ua_sess);
1640
1641 /* Lookup channel in the ust app session */
1642 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1643 strlen(uchan->name), &uiter);
1644 /* If the channel is not found, there is a code flow error */
1645 assert(ua_chan_node);
1646
1647 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1648
1649 /* Disable each events of channel */
1650 cds_lfht_for_each_entry(ua_chan->events, &uiter, ua_event, node) {
1651 ret = disable_ust_app_event(ua_sess, ua_event, app);
1652 if (ret < 0) {
1653 /* XXX: Report error someday... */
1654 continue;
1655 }
1656 }
1657 }
1658
1659 rcu_read_unlock();
1660
1661 return ret;
1662 }
1663
1664 /*
1665 * For a specific UST session, create the channel for all registered apps.
1666 */
1667 int ust_app_create_channel_glb(struct ltt_ust_session *usess,
1668 struct ltt_ust_channel *uchan)
1669 {
1670 int ret = 0;
1671 struct cds_lfht_iter iter;
1672 struct ust_app *app;
1673 struct ust_app_session *ua_sess;
1674 struct ust_app_channel *ua_chan;
1675
1676 if (usess == NULL || uchan == NULL) {
1677 ERR("Adding UST global channel to NULL values");
1678 ret = -1;
1679 goto error;
1680 }
1681
1682 DBG2("UST app adding channel %s to global domain for session uid %d",
1683 uchan->name, usess->uid);
1684
1685 rcu_read_lock();
1686
1687 /* For every registered applications */
1688 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1689 /*
1690 * Create session on the tracer side and add it to app session HT. Note
1691 * that if session exist, it will simply return a pointer to the ust
1692 * app session.
1693 */
1694 ua_sess = create_ust_app_session(usess, app);
1695 if (ua_sess == NULL) {
1696 continue;
1697 }
1698
1699 /* Create channel onto application */
1700 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1701 if (ua_chan == NULL) {
1702 continue;
1703 }
1704 }
1705
1706 rcu_read_unlock();
1707
1708 error:
1709 return ret;
1710 }
1711
1712 /*
1713 * Enable event for a specific session and channel on the tracer.
1714 */
1715 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
1716 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1717 {
1718 int ret = 0;
1719 struct cds_lfht_iter iter, uiter;
1720 struct cds_lfht_node *ua_chan_node, *ua_event_node;
1721 struct ust_app *app;
1722 struct ust_app_session *ua_sess;
1723 struct ust_app_channel *ua_chan;
1724 struct ust_app_event *ua_event;
1725
1726 DBG("UST app enabling event %s for all apps for session uid %d",
1727 uevent->attr.name, usess->uid);
1728
1729 /*
1730 * NOTE: At this point, this function is called only if the session and
1731 * channel passed are already created for all apps. and enabled on the
1732 * tracer also.
1733 */
1734
1735 rcu_read_lock();
1736
1737 /* For all registered applications */
1738 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1739 ua_sess = lookup_session_by_app(usess, app);
1740 /* If ua_sess is NULL, there is a code flow error */
1741 assert(ua_sess);
1742
1743 /* Lookup channel in the ust app session */
1744 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1745 strlen(uchan->name), &uiter);
1746 /* If the channel is not found, there is a code flow error */
1747 assert(ua_chan_node);
1748
1749 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1750
1751 ua_event_node = hashtable_lookup(ua_chan->events,
1752 (void*)uevent->attr.name, strlen(uevent->attr.name), &uiter);
1753 if (ua_event_node == NULL) {
1754 DBG3("UST app enable event %s not found for app PID %d."
1755 "Skipping app", uevent->attr.name, app->key.pid);
1756 continue;
1757 }
1758 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
1759
1760 ret = enable_ust_app_event(ua_sess, ua_event, app);
1761 if (ret < 0) {
1762 goto error;
1763 }
1764 }
1765
1766 error:
1767 rcu_read_unlock();
1768 return ret;
1769 }
1770
1771 /*
1772 * For a specific existing UST session and UST channel, creates the event for
1773 * all registered apps.
1774 */
1775 int ust_app_create_event_glb(struct ltt_ust_session *usess,
1776 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1777 {
1778 int ret = 0;
1779 struct cds_lfht_iter iter, uiter;
1780 struct cds_lfht_node *ua_chan_node;
1781 struct ust_app *app;
1782 struct ust_app_session *ua_sess;
1783 struct ust_app_channel *ua_chan;
1784
1785 DBG("UST app creating event %s for all apps for session uid %d",
1786 uevent->attr.name, usess->uid);
1787
1788 /*
1789 * NOTE: At this point, this function is called only if the session and
1790 * channel passed are already created for all apps. and enabled on the
1791 * tracer also.
1792 */
1793
1794 rcu_read_lock();
1795
1796 /* For all registered applications */
1797 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1798 ua_sess = lookup_session_by_app(usess, app);
1799 /* If ua_sess is NULL, there is a code flow error */
1800 assert(ua_sess);
1801
1802 /* Lookup channel in the ust app session */
1803 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
1804 strlen(uchan->name), &uiter);
1805 /* If the channel is not found, there is a code flow error */
1806 assert(ua_chan_node);
1807
1808 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1809
1810 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1811 if (ret < 0) {
1812 continue;
1813 }
1814 }
1815
1816 rcu_read_unlock();
1817
1818 return ret;
1819 }
1820
1821 /*
1822 * Start tracing for a specific UST session and app.
1823 */
1824 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
1825 {
1826 int ret = 0;
1827 struct cds_lfht_iter iter;
1828 struct ust_app_session *ua_sess;
1829 struct ust_app_channel *ua_chan;
1830 struct ltt_ust_stream *ustream;
1831 int consumerd_fd;
1832
1833 DBG("Starting tracing for ust app pid %d", app->key.pid);
1834
1835 rcu_read_lock();
1836
1837 ua_sess = lookup_session_by_app(usess, app);
1838 if (ua_sess == NULL) {
1839 goto error_rcu_unlock;
1840 }
1841
1842 /* Upon restart, we skip the setup, already done */
1843 if (ua_sess->started) {
1844 goto skip_setup;
1845 }
1846
1847 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1848 if (ret < 0) {
1849 goto error_rcu_unlock;
1850 }
1851
1852 /* For each channel */
1853 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1854 /* Create all streams */
1855 while (1) {
1856 /* Create UST stream */
1857 ustream = zmalloc(sizeof(*ustream));
1858 if (ustream == NULL) {
1859 PERROR("zmalloc ust stream");
1860 goto error_rcu_unlock;
1861 }
1862
1863 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1864 &ustream->obj);
1865 if (ret < 0) {
1866 /* Got all streams */
1867 break;
1868 }
1869 ustream->handle = ustream->obj->handle;
1870
1871 /* Order is important */
1872 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
1873 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1874 ua_sess->path, ua_chan->name,
1875 ua_chan->streams.count++);
1876 if (ret < 0) {
1877 PERROR("asprintf UST create stream");
1878 continue;
1879 }
1880 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1881 ustream->pathname);
1882 }
1883 }
1884
1885 switch (app->bits_per_long) {
1886 case 64:
1887 consumerd_fd = ust_consumerd64_fd;
1888 break;
1889 case 32:
1890 consumerd_fd = ust_consumerd32_fd;
1891 break;
1892 default:
1893 ret = -EINVAL;
1894 goto error_rcu_unlock;
1895 }
1896
1897 /* Setup UST consumer socket and send fds to it */
1898 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
1899 if (ret < 0) {
1900 goto error_rcu_unlock;
1901 }
1902 ua_sess->started = 1;
1903
1904 skip_setup:
1905 /* This start the UST tracing */
1906 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1907 if (ret < 0) {
1908 ERR("Error starting tracing for app pid: %d", app->key.pid);
1909 goto error_rcu_unlock;
1910 }
1911
1912 rcu_read_unlock();
1913
1914 /* Quiescent wait after starting trace */
1915 ustctl_wait_quiescent(app->key.sock);
1916
1917 return 0;
1918
1919 error_rcu_unlock:
1920 rcu_read_unlock();
1921 return -1;
1922 }
1923
1924 /*
1925 * Stop tracing for a specific UST session and app.
1926 */
1927 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1928 {
1929 int ret = 0;
1930 struct cds_lfht_iter iter;
1931 struct ust_app_session *ua_sess;
1932 struct ust_app_channel *ua_chan;
1933
1934 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1935
1936 rcu_read_lock();
1937
1938 ua_sess = lookup_session_by_app(usess, app);
1939 if (ua_sess == NULL) {
1940 /* Only malloc can failed so something is really wrong */
1941 goto error_rcu_unlock;
1942 }
1943
1944 /* Flush all buffers before stopping */
1945 ret = ustctl_sock_flush_buffer(app->key.sock, ua_sess->metadata->obj);
1946 if (ret < 0) {
1947 ERR("UST app PID %d metadata flush failed", app->key.pid);
1948 ERR("Ended with ret %d", ret);
1949 }
1950
1951 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1952 ret = ustctl_sock_flush_buffer(app->key.sock, ua_chan->obj);
1953 if (ret < 0) {
1954 ERR("UST app PID %d channel %s flush failed",
1955 app->key.pid, ua_chan->name);
1956 ERR("Ended with ret %d", ret);
1957 /* Continuing flushing all buffers */
1958 continue;
1959 }
1960 }
1961
1962 /* This inhibits UST tracing */
1963 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1964 if (ret < 0) {
1965 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1966 goto error_rcu_unlock;
1967 }
1968
1969 rcu_read_unlock();
1970
1971 /* Quiescent wait after stopping trace */
1972 ustctl_wait_quiescent(app->key.sock);
1973
1974 return 0;
1975
1976 error_rcu_unlock:
1977 rcu_read_unlock();
1978 return -1;
1979 }
1980
1981 /*
1982 * Destroy a specific UST session in apps.
1983 */
1984 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1985 {
1986 struct ust_app_session *ua_sess;
1987 struct lttng_ust_object_data obj;
1988 struct cds_lfht_iter iter;
1989 struct cds_lfht_node *node;
1990 int ret;
1991
1992 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1993
1994 rcu_read_lock();
1995
1996 __lookup_session_by_app(usess, app, &iter);
1997 node = hashtable_iter_get_node(&iter);
1998 if (node == NULL) {
1999 /* Only malloc can failed so something is really wrong */
2000 goto error_rcu_unlock;
2001 }
2002 ua_sess = caa_container_of(node, struct ust_app_session, node);
2003 ret = hashtable_del(app->sessions, &iter);
2004 assert(!ret);
2005 delete_ust_app_session(app->key.sock, ua_sess);
2006 obj.handle = ua_sess->handle;
2007 obj.shm_fd = -1;
2008 obj.wait_fd = -1;
2009 obj.memory_map_size = 0;
2010 ustctl_release_object(app->key.sock, &obj);
2011
2012 rcu_read_unlock();
2013
2014 /* Quiescent wait after stopping trace */
2015 ustctl_wait_quiescent(app->key.sock);
2016
2017 return 0;
2018
2019 error_rcu_unlock:
2020 rcu_read_unlock();
2021 return -1;
2022 }
2023
2024 /*
2025 * Start tracing for the UST session.
2026 */
2027 int ust_app_start_trace_all(struct ltt_ust_session *usess)
2028 {
2029 int ret = 0;
2030 struct cds_lfht_iter iter;
2031 struct ust_app *app;
2032
2033 DBG("Starting all UST traces");
2034
2035 rcu_read_lock();
2036
2037 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2038 ret = ust_app_start_trace(usess, app);
2039 if (ret < 0) {
2040 /* Continue to next apps even on error */
2041 continue;
2042 }
2043 }
2044
2045 rcu_read_unlock();
2046
2047 return 0;
2048 }
2049
2050 /*
2051 * Start tracing for the UST session.
2052 */
2053 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
2054 {
2055 int ret = 0;
2056 struct cds_lfht_iter iter;
2057 struct ust_app *app;
2058
2059 DBG("Stopping all UST traces");
2060
2061 rcu_read_lock();
2062
2063 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2064 ret = ust_app_stop_trace(usess, app);
2065 if (ret < 0) {
2066 /* Continue to next apps even on error */
2067 continue;
2068 }
2069 }
2070
2071 rcu_read_unlock();
2072
2073 return 0;
2074 }
2075
2076 /*
2077 * Destroy app UST session.
2078 */
2079 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
2080 {
2081 int ret = 0;
2082 struct cds_lfht_iter iter;
2083 struct ust_app *app;
2084
2085 DBG("Destroy all UST traces");
2086
2087 rcu_read_lock();
2088
2089 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2090 ret = ust_app_destroy_trace(usess, app);
2091 if (ret < 0) {
2092 /* Continue to next apps even on error */
2093 continue;
2094 }
2095 }
2096
2097 rcu_read_unlock();
2098
2099 return 0;
2100 }
2101
2102 /*
2103 * Add channels/events from UST global domain to registered apps at sock.
2104 */
2105 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
2106 {
2107 int ret = 0;
2108 struct cds_lfht_iter iter;
2109 struct ust_app *app;
2110 struct ust_app_session *ua_sess;
2111 struct ust_app_channel *ua_chan;
2112 struct ust_app_event *ua_event;
2113
2114 if (usess == NULL) {
2115 ERR("No UST session on global update. Returning");
2116 goto error;
2117 }
2118
2119 DBG2("UST app global update for app sock %d for session uid %d", sock,
2120 usess->uid);
2121
2122 rcu_read_lock();
2123
2124 app = find_app_by_sock(sock);
2125 if (app == NULL) {
2126 ERR("Failed to update app sock %d", sock);
2127 goto error;
2128 }
2129
2130 ua_sess = create_ust_app_session(usess, app);
2131 if (ua_sess == NULL) {
2132 goto error;
2133 }
2134
2135 /*
2136 * We can iterate safely here over all UST app session sicne the create ust
2137 * app session above made a shadow copy of the UST global domain from the
2138 * ltt ust session.
2139 */
2140 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
2141 ret = create_ust_channel(app, ua_sess, ua_chan);
2142 if (ret < 0) {
2143 /* FIXME: Should we quit here or continue... */
2144 continue;
2145 }
2146
2147 /* For each events */
2148 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
2149 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
2150 if (ret < 0) {
2151 /* FIXME: Should we quit here or continue... */
2152 continue;
2153 }
2154 }
2155 }
2156
2157 if (usess->start_trace) {
2158 ret = ust_app_start_trace(usess, app);
2159 if (ret < 0) {
2160 goto error;
2161 }
2162
2163 DBG2("UST trace started for app pid %d", app->key.pid);
2164 }
2165
2166 error:
2167 rcu_read_unlock();
2168 return;
2169 }
2170
2171 /*
2172 * Add context to a specific channel for global UST domain.
2173 */
2174 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
2175 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
2176 {
2177 int ret = 0;
2178 struct cds_lfht_node *ua_chan_node;
2179 struct cds_lfht_iter iter, uiter;
2180 struct ust_app_channel *ua_chan = NULL;
2181 struct ust_app_session *ua_sess;
2182 struct ust_app *app;
2183
2184 rcu_read_lock();
2185
2186 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2187 ua_sess = lookup_session_by_app(usess, app);
2188 if (ua_sess == NULL) {
2189 continue;
2190 }
2191
2192 /* Lookup channel in the ust app session */
2193 ua_chan_node = hashtable_lookup(ua_sess->channels,
2194 (void *)uchan->name, strlen(uchan->name), &uiter);
2195 if (ua_chan_node == NULL) {
2196 continue;
2197 }
2198 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2199 node);
2200
2201 ret = create_ust_app_channel_context(ua_sess, ua_chan, &uctx->ctx, app);
2202 if (ret < 0) {
2203 continue;
2204 }
2205 }
2206
2207 rcu_read_unlock();
2208 return ret;
2209 }
2210
2211 /*
2212 * Add context to a specific event in a channel for global UST domain.
2213 */
2214 int ust_app_add_ctx_event_glb(struct ltt_ust_session *usess,
2215 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
2216 struct ltt_ust_context *uctx)
2217 {
2218 int ret = 0;
2219 struct cds_lfht_node *ua_chan_node, *ua_event_node;
2220 struct cds_lfht_iter iter, uiter;
2221 struct ust_app_session *ua_sess;
2222 struct ust_app_event *ua_event;
2223 struct ust_app_channel *ua_chan = NULL;
2224 struct ust_app *app;
2225
2226 rcu_read_lock();
2227
2228 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
2229 ua_sess = lookup_session_by_app(usess, app);
2230 if (ua_sess == NULL) {
2231 continue;
2232 }
2233
2234 /* Lookup channel in the ust app session */
2235 ua_chan_node = hashtable_lookup(ua_sess->channels,
2236 (void *)uchan->name, strlen(uchan->name), &uiter);
2237 if (ua_chan_node == NULL) {
2238 continue;
2239 }
2240 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel,
2241 node);
2242
2243 ua_event_node = hashtable_lookup(ua_chan->events,
2244 (void *)uevent->attr.name, strlen(uevent->attr.name), &uiter);
2245 if (ua_event_node == NULL) {
2246 continue;
2247 }
2248 ua_event = caa_container_of(ua_event_node, struct ust_app_event,
2249 node);
2250
2251 ret = create_ust_app_event_context(ua_sess, ua_event, &uctx->ctx, app);
2252 if (ret < 0) {
2253 continue;
2254 }
2255 }
2256
2257 rcu_read_unlock();
2258 return ret;
2259 }
2260
2261 /*
2262 * Enable event for a channel from a UST session for a specific PID.
2263 */
2264 int ust_app_enable_event_pid(struct ltt_ust_session *usess,
2265 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2266 {
2267 int ret = 0;
2268 struct cds_lfht_iter iter;
2269 struct cds_lfht_node *ua_chan_node, *ua_event_node;
2270 struct ust_app *app;
2271 struct ust_app_session *ua_sess;
2272 struct ust_app_channel *ua_chan;
2273 struct ust_app_event *ua_event;
2274
2275 DBG("UST app enabling event %s for PID %d", uevent->attr.name, pid);
2276
2277 rcu_read_lock();
2278
2279 app = ust_app_find_by_pid(pid);
2280 if (app == NULL) {
2281 ERR("UST app enable event per PID %d not found", pid);
2282 ret = -1;
2283 goto error;
2284 }
2285
2286 ua_sess = lookup_session_by_app(usess, app);
2287 /* If ua_sess is NULL, there is a code flow error */
2288 assert(ua_sess);
2289
2290 /* Lookup channel in the ust app session */
2291 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
2292 strlen(uchan->name), &iter);
2293 /* If the channel is not found, there is a code flow error */
2294 assert(ua_chan_node);
2295
2296 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2297
2298 ua_event_node = hashtable_lookup(ua_chan->events,
2299 (void*)uevent->attr.name, strlen(uevent->attr.name), &iter);
2300 if (ua_event_node == NULL) {
2301 ret = create_ust_app_event(ua_sess, ua_chan, uevent, app);
2302 if (ret < 0) {
2303 goto error;
2304 }
2305 } else {
2306 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2307
2308 ret = enable_ust_app_event(ua_sess, ua_event, app);
2309 if (ret < 0) {
2310 goto error;
2311 }
2312 }
2313
2314 error:
2315 rcu_read_unlock();
2316 return ret;
2317 }
2318
2319 /*
2320 * Disable event for a channel from a UST session for a specific PID.
2321 */
2322 int ust_app_disable_event_pid(struct ltt_ust_session *usess,
2323 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent, pid_t pid)
2324 {
2325 int ret = 0;
2326 struct cds_lfht_iter iter;
2327 struct cds_lfht_node *ua_chan_node, *ua_event_node;
2328 struct ust_app *app;
2329 struct ust_app_session *ua_sess;
2330 struct ust_app_channel *ua_chan;
2331 struct ust_app_event *ua_event;
2332
2333 DBG("UST app disabling event %s for PID %d", uevent->attr.name, pid);
2334
2335 rcu_read_lock();
2336
2337 app = ust_app_find_by_pid(pid);
2338 if (app == NULL) {
2339 ERR("UST app disable event per PID %d not found", pid);
2340 ret = -1;
2341 goto error;
2342 }
2343
2344 ua_sess = lookup_session_by_app(usess, app);
2345 /* If ua_sess is NULL, there is a code flow error */
2346 assert(ua_sess);
2347
2348 /* Lookup channel in the ust app session */
2349 ua_chan_node = hashtable_lookup(ua_sess->channels, (void *)uchan->name,
2350 strlen(uchan->name), &iter);
2351 if (ua_chan_node == NULL) {
2352 /* Channel does not exist, skip disabling */
2353 goto error;
2354 }
2355 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
2356
2357 ua_event_node = hashtable_lookup(ua_chan->events,
2358 (void*)uevent->attr.name, strlen(uevent->attr.name), &iter);
2359 if (ua_event_node == NULL) {
2360 /* Event does not exist, skip disabling */
2361 goto error;
2362 }
2363 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
2364
2365 ret = disable_ust_app_event(ua_sess, ua_event, app);
2366 if (ret < 0) {
2367 goto error;
2368 }
2369
2370 error:
2371 rcu_read_unlock();
2372 return ret;
2373 }
This page took 0.108705 seconds and 5 git commands to generate.