e5bd47c7d2770721148af4505223823aa14d5d78
[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 app event safely. RCU read lock must be held before calling
40 * this function.
41 */
42 static void delete_ust_app_event(int sock, struct ust_app_event *ua_event)
43 {
44 /* TODO : remove context */
45 //struct ust_app_ctx *ltctx;
46 //cds_lfht_for_each_entry(lte->ctx, &iter, ltctx, node) {
47 // delete_ust_app_ctx(sock, ltctx);
48 //}
49
50 ustctl_release_object(sock, ua_event->obj);
51 free(ua_event->obj);
52 free(ua_event);
53 }
54
55 /*
56 * Delete ust app stream safely. RCU read lock must be held before calling
57 * this function.
58 */
59 static void delete_ust_app_stream(int sock, struct ltt_ust_stream *stream)
60 {
61 ustctl_release_object(sock, stream->obj);
62 free(stream->obj);
63 free(stream);
64 }
65
66 /*
67 * Delete ust app channel safely. RCU read lock must be held before calling
68 * this function.
69 */
70 static void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan)
71 {
72 int ret;
73 struct cds_lfht_iter iter;
74 struct ust_app_event *ua_event;
75 struct ltt_ust_stream *stream, *stmp;
76
77 cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) {
78 cds_list_del(&stream->list);
79 delete_ust_app_stream(sock, stream);
80 }
81
82 /* TODO : remove channel context */
83 //cds_lfht_for_each_entry(ltc->ctx, &iter, ltctx, node) {
84 // hashtable_del(ltc->ctx, &iter);
85 // delete_ust_app_ctx(sock, ltctx);
86 //}
87 //ret = hashtable_destroy(ltc->ctx);
88
89 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
90 hashtable_del(ua_chan->events, &iter);
91 delete_ust_app_event(sock, ua_event);
92 }
93
94 ret = hashtable_destroy(ua_chan->events);
95 if (ret < 0) {
96 ERR("UST app destroy session hashtable failed");
97 goto error;
98 }
99 ustctl_release_object(sock, ua_chan->obj);
100 free(ua_chan->obj);
101 free(ua_chan);
102
103 error:
104 return;
105 }
106
107 /*
108 * Delete ust app session safely. RCU read lock must be held before calling
109 * this function.
110 */
111 static void delete_ust_app_session(int sock,
112 struct ust_app_session *ua_sess)
113 {
114 int ret;
115 struct cds_lfht_iter iter;
116 struct ust_app_channel *ua_chan;
117
118 if (ua_sess->metadata) {
119 ustctl_release_object(sock, ua_sess->metadata->stream_obj);
120 free(ua_sess->metadata->stream_obj);
121 ustctl_release_object(sock, ua_sess->metadata->obj);
122 free(ua_sess->metadata->obj);
123 }
124
125 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
126 hashtable_del(ua_sess->channels, &iter);
127 delete_ust_app_channel(sock, ua_chan);
128 }
129
130 ret = hashtable_destroy(ua_sess->channels);
131 if (ret < 0) {
132 ERR("UST app destroy session hashtable failed");
133 goto error;
134 }
135
136 error:
137 return;
138 }
139
140 /*
141 * Delete a traceable application structure from the global list. Never call
142 * this function outside of a call_rcu call.
143 */
144 static void delete_ust_app(struct ust_app *app)
145 {
146 int ret;
147 struct cds_lfht_node *node;
148 struct cds_lfht_iter iter;
149 struct ust_app_session *ua_sess;
150 int sock;
151
152 rcu_read_lock();
153
154 /* Remove from key hash table */
155 node = hashtable_lookup(ust_app_sock_key_map,
156 (void *) ((unsigned long) app->key.sock), sizeof(void *), &iter);
157 if (node == NULL) {
158 /* Not suppose to happen */
159 ERR("UST app key %d not found in key hash table", app->key.sock);
160 goto end;
161 }
162
163 ret = hashtable_del(ust_app_sock_key_map, &iter);
164 if (ret) {
165 ERR("UST app unable to delete app sock %d from key hash table",
166 app->key.sock);
167 } else {
168 DBG2("UST app pair sock %d key %d deleted",
169 app->key.sock, app->key.pid);
170 }
171
172 /* Socket is already closed at this point */
173
174 /* Delete ust app sessions info */
175 sock = app->key.sock;
176 app->key.sock = -1;
177
178 cds_lfht_for_each_entry(app->sessions, &iter, ua_sess, node) {
179 hashtable_del(app->sessions, &iter);
180 delete_ust_app_session(app->key.sock, ua_sess);
181 }
182
183 ret = hashtable_destroy(app->sessions);
184 if (ret < 0) {
185 ERR("UST app destroy session hashtable failed");
186 goto end;
187 }
188
189 /*
190 * Wait until we have removed the key from the sock hash table
191 * before closing this socket, otherwise an application could
192 * re-use the socket ID and race with the teardown, using the
193 * same hash table entry.
194 */
195 close(sock);
196
197 DBG2("UST app pid %d deleted", app->key.pid);
198 free(app);
199 end:
200 rcu_read_unlock();
201 }
202
203 /*
204 * URCU intermediate call to delete an UST app.
205 */
206 static void delete_ust_app_rcu(struct rcu_head *head)
207 {
208 struct cds_lfht_node *node =
209 caa_container_of(head, struct cds_lfht_node, head);
210 struct ust_app *app =
211 caa_container_of(node, struct ust_app, node);
212
213 delete_ust_app(app);
214 }
215
216 /*
217 * Find an ust_app using the sock and return it. RCU read side lock must be
218 * held before calling this helper function.
219 */
220 static struct ust_app *find_app_by_sock(int sock)
221 {
222 struct cds_lfht_node *node;
223 struct ust_app_key *key;
224 struct cds_lfht_iter iter;
225
226 node = hashtable_lookup(ust_app_sock_key_map,
227 (void *)((unsigned long) sock), sizeof(void *), &iter);
228 if (node == NULL) {
229 DBG2("UST app find by sock %d key not found", sock);
230 goto error;
231 }
232
233 key = caa_container_of(node, struct ust_app_key, node);
234
235 node = hashtable_lookup(ust_app_ht,
236 (void *)((unsigned long) key->pid), sizeof(void *), &iter);
237 if (node == NULL) {
238 DBG2("UST app find by sock %d not found", sock);
239 goto error;
240 }
241 return caa_container_of(node, struct ust_app, node);
242
243 error:
244 return NULL;
245 }
246
247 /*
248 * Disable the specified channel on to UST tracer for the UST session.
249 */
250 static int disable_ust_channel(struct ust_app *app,
251 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
252 {
253 int ret;
254
255 ret = ustctl_disable(app->key.sock, ua_chan->obj);
256 if (ret < 0) {
257 ERR("UST app channel %s disable failed for app (pid: %d) "
258 "and session handle %d with ret %d",
259 ua_chan->name, app->key.pid, ua_sess->handle, ret);
260 goto error;
261 }
262
263 ua_chan->enabled = 0;
264
265 DBG2("UST app channel %s disabled successfully for app (pid: %d)",
266 ua_chan->name, app->key.pid);
267
268 error:
269 return ret;
270 }
271
272 /*
273 * Enable the specified channel on to UST tracer for the UST session.
274 */
275 static int enable_ust_channel(struct ust_app *app,
276 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
277 {
278 int ret;
279
280 ret = ustctl_enable(app->key.sock, ua_chan->obj);
281 if (ret < 0) {
282 ERR("UST app channel %s enable failed for app (pid: %d) "
283 "and session handle %d with ret %d",
284 ua_chan->name, app->key.pid, ua_sess->handle, ret);
285 goto error;
286 }
287
288 ua_chan->enabled = 1;
289
290 DBG2("UST app channel %s enabled successfully for app (pid: %d)",
291 ua_chan->name, app->key.pid);
292
293 error:
294 return ret;
295 }
296
297 /*
298 * Open metadata onto the UST tracer for a UST session.
299 */
300 static int open_ust_metadata(struct ust_app *app,
301 struct ust_app_session *ua_sess)
302 {
303 int ret;
304 struct lttng_ust_channel_attr uattr;
305
306 uattr.overwrite = ua_sess->metadata->attr.overwrite;
307 uattr.subbuf_size = ua_sess->metadata->attr.subbuf_size;
308 uattr.num_subbuf = ua_sess->metadata->attr.num_subbuf;
309 uattr.switch_timer_interval =
310 ua_sess->metadata->attr.switch_timer_interval;
311 uattr.read_timer_interval =
312 ua_sess->metadata->attr.read_timer_interval;
313 uattr.output = ua_sess->metadata->attr.output;
314
315 /* UST tracer metadata creation */
316 ret = ustctl_open_metadata(app->key.sock, ua_sess->handle, &uattr,
317 &ua_sess->metadata->obj);
318 if (ret < 0) {
319 ERR("UST app open metadata failed for app pid:%d",
320 app->key.pid);
321 goto error;
322 }
323
324 error:
325 return ret;
326 }
327
328 /*
329 * Create stream onto the UST tracer for a UST session.
330 */
331 static int create_ust_stream(struct ust_app *app,
332 struct ust_app_session *ua_sess)
333 {
334 int ret;
335
336 ret = ustctl_create_stream(app->key.sock, ua_sess->metadata->obj,
337 &ua_sess->metadata->stream_obj);
338 if (ret < 0) {
339 ERR("UST create metadata stream failed");
340 goto error;
341 }
342
343 error:
344 return ret;
345 }
346
347 /*
348 * Create the specified channel onto the UST tracer for a UST session.
349 */
350 static int create_ust_channel(struct ust_app *app,
351 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan)
352 {
353 int ret;
354
355 /* TODO: remove cast and use lttng-ust-abi.h */
356 ret = ustctl_create_channel(app->key.sock, ua_sess->handle,
357 (struct lttng_ust_channel_attr *)&ua_chan->attr, &ua_chan->obj);
358 if (ret < 0) {
359 DBG("Error creating channel %s for app (pid: %d, sock: %d) "
360 "and session handle %d with ret %d",
361 ua_chan->name, app->key.pid, app->key.sock,
362 ua_sess->handle, ret);
363 goto error;
364 }
365
366 ua_chan->handle = ua_chan->obj->handle;
367 ua_chan->attr.shm_fd = ua_chan->obj->shm_fd;
368 ua_chan->attr.wait_fd = ua_chan->obj->wait_fd;
369 ua_chan->attr.memory_map_size = ua_chan->obj->memory_map_size;
370
371 DBG2("UST app channel %s created successfully for pid:%d and sock:%d",
372 ua_chan->name, app->key.pid, app->key.sock);
373
374 error:
375 return ret;
376 }
377
378 /*
379 * Create the specified event onto the UST tracer for a UST session.
380 */
381 static int create_ust_event(struct ust_app *app,
382 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
383 struct ust_app_event *ua_event)
384 {
385 int ret = 0;
386
387 /* Create UST event on tracer */
388 ret = ustctl_create_event(app->key.sock, &ua_event->attr, ua_chan->obj,
389 &ua_event->obj);
390 if (ret < 0) {
391 ERR("Error ustctl create event %s for app pid: %d with ret %d",
392 ua_event->attr.name, app->key.pid, ret);
393 goto error;
394 }
395
396 ua_event->handle = ua_event->obj->handle;
397 ua_event->enabled = 1;
398
399 DBG2("UST app event %s created successfully for pid:%d",
400 ua_event->attr.name, app->key.pid);
401
402 error:
403 return ret;
404 }
405
406 /*
407 * Alloc new UST app session.
408 */
409 static struct ust_app_session *alloc_ust_app_session(void)
410 {
411 struct ust_app_session *ua_sess;
412
413 /* Init most of the default value by allocating and zeroing */
414 ua_sess = zmalloc(sizeof(struct ust_app_session));
415 if (ua_sess == NULL) {
416 PERROR("malloc");
417 goto error;
418 }
419
420 ua_sess->handle = -1;
421 ua_sess->channels = hashtable_new_str(0);
422
423 return ua_sess;
424
425 error:
426 return NULL;
427 }
428
429 /*
430 * Alloc new UST app channel.
431 */
432 static struct ust_app_channel *alloc_ust_app_channel(char *name,
433 struct lttng_ust_channel *attr)
434 {
435 struct ust_app_channel *ua_chan;
436
437 /* Init most of the default value by allocating and zeroing */
438 ua_chan = zmalloc(sizeof(struct ust_app_channel));
439 if (ua_chan == NULL) {
440 PERROR("malloc");
441 goto error;
442 }
443
444 /* Setup channel name */
445 strncpy(ua_chan->name, name, sizeof(ua_chan->name));
446 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
447
448 ua_chan->handle = -1;
449 ua_chan->ctx = hashtable_new(0);
450 ua_chan->events = hashtable_new_str(0);
451 hashtable_node_init(&ua_chan->node, (void *) ua_chan->name,
452 strlen(ua_chan->name));
453
454 CDS_INIT_LIST_HEAD(&ua_chan->streams.head);
455
456 /* Copy attributes */
457 if (attr) {
458 memcpy(&ua_chan->attr, attr, sizeof(ua_chan->attr));
459 }
460
461 DBG3("UST app channel %s allocated", ua_chan->name);
462
463 return ua_chan;
464
465 error:
466 return NULL;
467 }
468
469 /*
470 * Alloc new UST app event.
471 */
472 static struct ust_app_event *alloc_ust_app_event(char *name,
473 struct lttng_ust_event *attr)
474 {
475 struct ust_app_event *ua_event;
476
477 /* Init most of the default value by allocating and zeroing */
478 ua_event = zmalloc(sizeof(struct ust_app_event));
479 if (ua_event == NULL) {
480 PERROR("malloc");
481 goto error;
482 }
483
484 strncpy(ua_event->name, name, sizeof(ua_event->name));
485 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
486 ua_event->ctx = hashtable_new(0);
487 hashtable_node_init(&ua_event->node, (void *) ua_event->name,
488 strlen(ua_event->name));
489
490 /* Copy attributes */
491 if (attr) {
492 memcpy(&ua_event->attr, attr, sizeof(ua_event->attr));
493 }
494
495 DBG3("UST app event %s allocated", ua_event->name);
496
497 return ua_event;
498
499 error:
500 return NULL;
501 }
502
503 /*
504 * Copy data between an UST app event and a LTT event.
505 */
506 static void shadow_copy_event(struct ust_app_event *ua_event,
507 struct ltt_ust_event *uevent)
508 {
509 strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name));
510 ua_event->name[sizeof(ua_event->name) - 1] = '\0';
511
512 /* Copy event attributes */
513 memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr));
514
515 /* TODO: support copy context */
516 }
517
518 /*
519 * Copy data between an UST app channel and a LTT channel.
520 */
521 static void shadow_copy_channel(struct ust_app_channel *ua_chan,
522 struct ltt_ust_channel *uchan)
523 {
524 struct cds_lfht_iter iter;
525 struct cds_lfht_node *ua_event_node;
526 struct ltt_ust_event *uevent;
527 struct ust_app_event *ua_event;
528
529 DBG2("Shadow copy of UST app channel %s", ua_chan->name);
530
531 strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name));
532 ua_chan->name[sizeof(ua_chan->name) - 1] = '\0';
533 /* Copy event attributes */
534 memcpy(&ua_chan->attr, &uchan->attr, sizeof(ua_chan->attr));
535
536 /* TODO: support copy context */
537
538 /* Copy all events from ltt ust channel to ust app channel */
539 cds_lfht_for_each_entry(uchan->events, &iter, uevent, node) {
540 struct cds_lfht_iter uiter;
541
542 ua_event_node = hashtable_lookup(ua_chan->events,
543 (void *) uevent->attr.name, strlen(uevent->attr.name),
544 &uiter);
545 if (ua_event_node == NULL) {
546 DBG2("UST event %s not found on shadow copy channel",
547 uevent->attr.name);
548 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
549 if (ua_event == NULL) {
550 continue;
551 }
552 shadow_copy_event(ua_event, uevent);
553 hashtable_add_unique(ua_chan->events, &ua_event->node);
554 }
555 }
556
557 DBG3("Shadow copy channel done");
558 }
559
560 /*
561 * Copy data between a UST app session and a regular LTT session.
562 */
563 static void shadow_copy_session(struct ust_app_session *ua_sess,
564 struct ltt_ust_session *usess,
565 struct ust_app *app)
566 {
567 struct cds_lfht_node *ua_chan_node;
568 struct cds_lfht_iter iter;
569 struct ltt_ust_channel *uchan;
570 struct ust_app_channel *ua_chan;
571 time_t rawtime;
572 struct tm *timeinfo;
573 char datetime[16];
574 int ret;
575
576 /* Get date and time for unique app path */
577 time(&rawtime);
578 timeinfo = localtime(&rawtime);
579 strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo);
580
581 DBG2("Shadow copy of session handle %d", ua_sess->handle);
582
583 ua_sess->uid = usess->uid;
584
585 ret = snprintf(ua_sess->path, PATH_MAX,
586 "%s/%s-%d-%s",
587 usess->pathname, app->name, app->key.pid,
588 datetime);
589 if (ret < 0) {
590 PERROR("asprintf UST shadow copy session");
591 /* TODO: We cannot return an error from here.. */
592 assert(0);
593 }
594
595 /* TODO: support all UST domain */
596
597 /* Iterate over all channels in global domain. */
598 cds_lfht_for_each_entry(usess->domain_global.channels, &iter,
599 uchan, node) {
600 struct cds_lfht_iter uiter;
601
602 ua_chan_node = hashtable_lookup(ua_sess->channels,
603 (void *)uchan->name, strlen(uchan->name),
604 &uiter);
605 if (ua_chan_node != NULL) {
606 continue;
607 }
608
609 DBG2("Channel %s not found on shadow session copy, creating it",
610 uchan->name);
611 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
612 if (ua_chan == NULL) {
613 /* malloc failed... continuing */
614 continue;
615 }
616
617 shadow_copy_channel(ua_chan, uchan);
618 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
619 }
620 }
621
622 /*
623 * Lookup sesison wrapper.
624 */
625 static
626 void __lookup_session_by_app(struct ltt_ust_session *usess,
627 struct ust_app *app, struct cds_lfht_iter *iter)
628 {
629 /* Get right UST app session from app */
630 (void) hashtable_lookup(app->sessions,
631 (void *) ((unsigned long) usess->uid), sizeof(void *),
632 iter);
633 }
634
635 /*
636 * Return ust app session from the app session hashtable using the UST session
637 * uid.
638 */
639 static struct ust_app_session *lookup_session_by_app(
640 struct ltt_ust_session *usess, struct ust_app *app)
641 {
642 struct cds_lfht_iter iter;
643 struct cds_lfht_node *node;
644
645 __lookup_session_by_app(usess, app, &iter);
646 node = hashtable_iter_get_node(&iter);
647 if (node == NULL) {
648 goto error;
649 }
650
651 return caa_container_of(node, struct ust_app_session, node);
652
653 error:
654 return NULL;
655 }
656
657 /*
658 * Create a UST session onto the tracer of app and add it the session
659 * hashtable.
660 *
661 * Return ust app session or NULL on error.
662 */
663 static struct ust_app_session *create_ust_app_session(
664 struct ltt_ust_session *usess, struct ust_app *app)
665 {
666 int ret;
667 struct ust_app_session *ua_sess;
668
669 ua_sess = lookup_session_by_app(usess, app);
670 if (ua_sess == NULL) {
671 DBG2("UST app pid: %d session uid %d not found, creating it",
672 app->key.pid, usess->uid);
673 ua_sess = alloc_ust_app_session();
674 if (ua_sess == NULL) {
675 /* Only malloc can failed so something is really wrong */
676 goto error;
677 }
678 shadow_copy_session(ua_sess, usess, app);
679 }
680
681 if (ua_sess->handle == -1) {
682 ret = ustctl_create_session(app->key.sock);
683 if (ret < 0) {
684 ERR("Error creating session for app pid %d, sock %d",
685 app->key.pid, app->key.sock);
686 /* TODO: free() ua_sess */
687 goto error;
688 }
689
690 DBG2("UST app ustctl create session handle %d", ret);
691 ua_sess->handle = ret;
692
693 /* Add ust app session to app's HT */
694 hashtable_node_init(&ua_sess->node,
695 (void *)((unsigned long) ua_sess->uid), sizeof(void *));
696 hashtable_add_unique(app->sessions, &ua_sess->node);
697
698 DBG2("UST app session created successfully with handle %d", ret);
699 }
700
701 return ua_sess;
702
703 error:
704 return NULL;
705 }
706
707 /*
708 * Lookup ust app channel for session and disable it on the tracer side.
709 */
710 static int disable_ust_app_channel(struct ust_app_session *ua_sess,
711 struct ltt_ust_channel *uchan, struct ust_app *app)
712 {
713 int ret = 0;
714 struct cds_lfht_iter iter;
715 struct cds_lfht_node *ua_chan_node;
716 struct ust_app_channel *ua_chan;
717
718 ua_chan_node = hashtable_lookup(ua_sess->channels,
719 (void *)uchan->name, strlen(uchan->name), &iter);
720 if (ua_chan_node == NULL) {
721 DBG2("Unable to find channel %s in ust session uid %u",
722 uchan->name, ua_sess->uid);
723 goto error;
724 }
725
726 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
727
728 ret = disable_ust_channel(app, ua_sess, ua_chan);
729 if (ret < 0) {
730 goto error;
731 }
732
733 error:
734 return ret;
735 }
736
737 /*
738 * Lookup ust app channel for session and enable it on the tracer side.
739 */
740 static int enable_ust_app_channel(struct ust_app_session *ua_sess,
741 struct ltt_ust_channel *uchan, struct ust_app *app)
742 {
743 int ret = 0;
744 struct cds_lfht_iter iter;
745 struct cds_lfht_node *ua_chan_node;
746 struct ust_app_channel *ua_chan;
747
748 ua_chan_node = hashtable_lookup(ua_sess->channels,
749 (void *)uchan->name, strlen(uchan->name), &iter);
750 if (ua_chan_node == NULL) {
751 DBG2("Unable to find channel %s in ust session uid %u",
752 uchan->name, ua_sess->uid);
753 goto error;
754 }
755
756 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
757
758 ret = enable_ust_channel(app, ua_sess, ua_chan);
759 if (ret < 0) {
760 goto error;
761 }
762
763 error:
764 return ret;
765 }
766
767 /*
768 * Create UST app channel and create it on the tracer.
769 */
770 static struct ust_app_channel *create_ust_app_channel(
771 struct ust_app_session *ua_sess, struct ltt_ust_channel *uchan,
772 struct ust_app *app)
773 {
774 int ret = 0;
775 struct cds_lfht_iter iter;
776 struct cds_lfht_node *ua_chan_node;
777 struct ust_app_channel *ua_chan;
778
779 /* Lookup channel in the ust app session */
780 ua_chan_node = hashtable_lookup(ua_sess->channels,
781 (void *)uchan->name, strlen(uchan->name), &iter);
782 if (ua_chan_node == NULL) {
783 DBG2("Unable to find channel %s in ust session uid %u",
784 uchan->name, ua_sess->uid);
785 ua_chan = alloc_ust_app_channel(uchan->name, &uchan->attr);
786 if (ua_chan == NULL) {
787 goto error;
788 }
789 shadow_copy_channel(ua_chan, uchan);
790
791 hashtable_add_unique(ua_sess->channels, &ua_chan->node);
792 } else {
793 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
794 }
795
796 ret = create_ust_channel(app, ua_sess, ua_chan);
797 if (ret < 0) {
798 goto error;
799 }
800
801 return ua_chan;
802
803 error:
804 return NULL;
805 }
806
807 /*
808 * Create UST app event and create it on the tracer side.
809 */
810 static struct ust_app_event *create_ust_app_event(
811 struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan,
812 struct ltt_ust_event *uevent, struct ust_app *app)
813 {
814 int ret;
815 struct cds_lfht_iter iter;
816 struct cds_lfht_node *ua_event_node;
817 struct ust_app_event *ua_event;
818
819 /* Get event node */
820 ua_event_node = hashtable_lookup(ua_chan->events,
821 (void *)uevent->attr.name, strlen(uevent->attr.name), &iter);
822 if (ua_event_node == NULL) {
823 DBG2("UST app event %s not found, creating it", uevent->attr.name);
824 /* Does not exist so create one */
825 ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr);
826 if (ua_event == NULL) {
827 /* Only malloc can failed so something is really wrong */
828 goto error;
829 }
830 shadow_copy_event(ua_event, uevent);
831
832 hashtable_add_unique(ua_chan->events, &ua_event->node);
833 } else {
834 ua_event = caa_container_of(ua_event_node, struct ust_app_event, node);
835 }
836
837 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
838 if (ret < 0) {
839 goto error;
840 }
841
842 return ua_event;
843
844 error:
845 return NULL;
846 }
847
848 /*
849 * Create UST metadata and open it on the tracer side.
850 */
851 static int create_ust_app_metadata(struct ust_app_session *ua_sess,
852 char *pathname, struct ust_app *app)
853 {
854 int ret = 0;
855
856 if (ua_sess->metadata == NULL) {
857 /* Allocate UST metadata */
858 ua_sess->metadata = trace_ust_create_metadata(pathname);
859 if (ua_sess->metadata == NULL) {
860 ERR("UST app session %d creating metadata failed",
861 ua_sess->handle);
862 goto error;
863 }
864
865 ret = open_ust_metadata(app, ua_sess);
866 if (ret < 0) {
867 goto error;
868 }
869
870 DBG2("UST metadata opened for app pid %d", app->key.pid);
871 }
872
873 /* Open UST metadata stream */
874 if (ua_sess->metadata->stream_obj == NULL) {
875 ret = create_ust_stream(app, ua_sess);
876 if (ret < 0) {
877 goto error;
878 }
879
880 ret = mkdir(ua_sess->path, S_IRWXU | S_IRWXG);
881 if (ret < 0) {
882 PERROR("mkdir UST metadata");
883 goto error;
884 }
885
886 ret = snprintf(ua_sess->metadata->pathname, PATH_MAX,
887 "%s/metadata", ua_sess->path);
888 if (ret < 0) {
889 PERROR("asprintf UST create stream");
890 goto error;
891 }
892
893 DBG2("UST metadata stream object created for app pid %d",
894 app->key.pid);
895 } else {
896 ERR("Attempting to create stream without metadata opened");
897 goto error;
898 }
899
900 return 0;
901
902 error:
903 return -1;
904 }
905
906 /*
907 * Return pointer to traceable apps list.
908 */
909 struct cds_lfht *ust_app_get_ht(void)
910 {
911 return ust_app_ht;
912 }
913
914 /*
915 * Return ust app pointer or NULL if not found.
916 */
917 struct ust_app *ust_app_find_by_pid(pid_t pid)
918 {
919 struct cds_lfht_node *node;
920 struct cds_lfht_iter iter;
921
922 rcu_read_lock();
923 node = hashtable_lookup(ust_app_ht,
924 (void *)((unsigned long) pid), sizeof(void *), &iter);
925 if (node == NULL) {
926 DBG2("UST app no found with pid %d", pid);
927 goto error;
928 }
929 rcu_read_unlock();
930
931 DBG2("Found UST app by pid %d", pid);
932
933 return caa_container_of(node, struct ust_app, node);
934
935 error:
936 rcu_read_unlock();
937 return NULL;
938 }
939
940 /*
941 * Using pid and uid (of the app), allocate a new ust_app struct and
942 * add it to the global traceable app list.
943 *
944 * On success, return 0, else return malloc -ENOMEM, or -EINVAL if app
945 * bitness is not supported.
946 */
947 int ust_app_register(struct ust_register_msg *msg, int sock)
948 {
949 struct ust_app *lta;
950
951 if ((msg->bits_per_long == 64 && ust_consumerd64_fd == -EINVAL)
952 || (msg->bits_per_long == 32 && ust_consumerd32_fd == -EINVAL)) {
953 ERR("Registration failed: application \"%s\" (pid: %d) has "
954 "%d-bit long, but no consumerd for this long size is available.\n",
955 msg->name, msg->pid, msg->bits_per_long);
956 close(sock);
957 return -EINVAL;
958 }
959 lta = zmalloc(sizeof(struct ust_app));
960 if (lta == NULL) {
961 PERROR("malloc");
962 return -ENOMEM;
963 }
964
965 lta->ppid = msg->ppid;
966 lta->uid = msg->uid;
967 lta->gid = msg->gid;
968 lta->bits_per_long = msg->bits_per_long;
969 lta->v_major = msg->major;
970 lta->v_minor = msg->minor;
971 strncpy(lta->name, msg->name, sizeof(lta->name));
972 lta->name[16] = '\0';
973 lta->sessions = hashtable_new(0);
974
975 /* Set key map */
976 lta->key.pid = msg->pid;
977 hashtable_node_init(&lta->node, (void *)((unsigned long)lta->key.pid),
978 sizeof(void *));
979 lta->key.sock = sock;
980 hashtable_node_init(&lta->key.node, (void *)((unsigned long)lta->key.sock),
981 sizeof(void *));
982
983 rcu_read_lock();
984 hashtable_add_unique(ust_app_sock_key_map, &lta->key.node);
985 hashtable_add_unique(ust_app_ht, &lta->node);
986 rcu_read_unlock();
987
988 DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s"
989 " (version %d.%d)", lta->key.pid, lta->ppid, lta->uid, lta->gid,
990 lta->key.sock, lta->name, lta->v_major, lta->v_minor);
991
992 return 0;
993 }
994
995 /*
996 * Unregister app by removing it from the global traceable app list and freeing
997 * the data struct.
998 *
999 * The socket is already closed at this point so no close to sock.
1000 */
1001 void ust_app_unregister(int sock)
1002 {
1003 struct ust_app *lta;
1004 struct cds_lfht_node *node;
1005 struct cds_lfht_iter iter;
1006
1007 rcu_read_lock();
1008 lta = find_app_by_sock(sock);
1009 if (lta == NULL) {
1010 ERR("Unregister app sock %d not found!", sock);
1011 goto error;
1012 }
1013
1014 DBG("PID %d unregistering with sock %d", lta->key.pid, sock);
1015
1016 /* Get the node reference for a call_rcu */
1017 node = hashtable_lookup(ust_app_ht,
1018 (void *)((unsigned long) lta->key.pid), sizeof(void *), &iter);
1019 if (node == NULL) {
1020 ERR("Unable to find app sock %d by pid %d", sock, lta->key.pid);
1021 goto error;
1022 }
1023
1024 hashtable_del(ust_app_ht, &iter);
1025 call_rcu(&node->head, delete_ust_app_rcu);
1026 error:
1027 rcu_read_unlock();
1028 return;
1029 }
1030
1031 /*
1032 * Return traceable_app_count
1033 */
1034 unsigned long ust_app_list_count(void)
1035 {
1036 unsigned long count;
1037
1038 rcu_read_lock();
1039 count = hashtable_get_count(ust_app_ht);
1040 rcu_read_unlock();
1041
1042 return count;
1043 }
1044
1045 /*
1046 * Fill events array with all events name of all registered apps.
1047 */
1048 int ust_app_list_events(struct lttng_event **events)
1049 {
1050 int ret, handle;
1051 size_t nbmem, count = 0;
1052 struct cds_lfht_iter iter;
1053 struct ust_app *app;
1054 struct lttng_event *tmp;
1055
1056 nbmem = UST_APP_EVENT_LIST_SIZE;
1057 tmp = zmalloc(nbmem * sizeof(struct lttng_event));
1058 if (tmp == NULL) {
1059 PERROR("zmalloc ust app events");
1060 ret = -ENOMEM;
1061 goto error;
1062 }
1063
1064 rcu_read_lock();
1065
1066 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1067 handle = ustctl_tracepoint_list(app->key.sock);
1068 if (handle < 0) {
1069 ERR("UST app list events getting handle failed for app pid %d",
1070 app->key.pid);
1071 continue;
1072 }
1073
1074 while ((ret = ustctl_tracepoint_list_get(app->key.sock, handle,
1075 tmp[count].name)) != -ENOENT) {
1076 if (count > nbmem) {
1077 DBG2("Reallocating event list from %zu to %zu bytes", nbmem,
1078 nbmem + UST_APP_EVENT_LIST_SIZE);
1079 nbmem += UST_APP_EVENT_LIST_SIZE;
1080 tmp = realloc(tmp, nbmem);
1081 if (tmp == NULL) {
1082 PERROR("realloc ust app events");
1083 ret = -ENOMEM;
1084 goto rcu_error;
1085 }
1086 }
1087
1088 tmp[count].type = LTTNG_UST_TRACEPOINT;
1089 tmp[count].pid = app->key.pid;
1090 tmp[count].enabled = -1;
1091 count++;
1092 }
1093 }
1094
1095 ret = count;
1096 *events = tmp;
1097
1098 DBG2("UST app list events done (%zu events)", count);
1099
1100 rcu_error:
1101 rcu_read_unlock();
1102 error:
1103 return ret;
1104 }
1105
1106 /*
1107 * Free and clean all traceable apps of the global list.
1108 */
1109 void ust_app_clean_list(void)
1110 {
1111 int ret;
1112 struct cds_lfht_node *node;
1113 struct cds_lfht_iter iter;
1114 struct ust_app *app;
1115
1116 DBG2("UST app cleaning registered apps hash table");
1117
1118 rcu_read_lock();
1119
1120 cds_lfht_for_each(ust_app_ht, &iter, node) {
1121 app = caa_container_of(node, struct ust_app, node);
1122
1123 ret = hashtable_del(ust_app_ht, &iter);
1124 if (!ret) {
1125 call_rcu(&node->head, delete_ust_app_rcu);
1126 }
1127 }
1128
1129 hashtable_destroy(ust_app_ht);
1130 hashtable_destroy(ust_app_sock_key_map);
1131
1132 rcu_read_unlock();
1133 }
1134
1135 /*
1136 * Init UST app hash table.
1137 */
1138 void ust_app_ht_alloc(void)
1139 {
1140 ust_app_ht = hashtable_new(0);
1141 ust_app_sock_key_map = hashtable_new(0);
1142 }
1143
1144 /*
1145 * For a specific UST session, disable the channel for all registered apps.
1146 */
1147 int ust_app_disable_channel_all(struct ltt_ust_session *usess,
1148 struct ltt_ust_channel *uchan)
1149 {
1150 int ret = 0;
1151 struct cds_lfht_iter iter;
1152 struct ust_app *app;
1153 struct ust_app_session *ua_sess;
1154
1155 if (usess == NULL || uchan == NULL) {
1156 ERR("Disabling UST global channel with NULL values");
1157 ret = -1;
1158 goto error;
1159 }
1160
1161 DBG2("UST app disablling channel %s from global domain for session uid %d",
1162 uchan->name, usess->uid);
1163
1164 rcu_read_lock();
1165
1166 /* For every registered applications */
1167 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1168 ua_sess = lookup_session_by_app(usess, app);
1169 if (ua_sess == NULL) {
1170 continue;
1171 }
1172
1173 /* Create channel onto application */
1174 ret = disable_ust_app_channel(ua_sess, uchan, app);
1175 if (ret < 0) {
1176 /* XXX: We might want to report this error at some point... */
1177 continue;
1178 }
1179 }
1180
1181 rcu_read_unlock();
1182
1183 error:
1184 return ret;
1185 }
1186
1187 /*
1188 * For a specific UST session, enable the channel for all registered apps.
1189 */
1190 int ust_app_enable_channel_all(struct ltt_ust_session *usess,
1191 struct ltt_ust_channel *uchan)
1192 {
1193 int ret = 0;
1194 struct cds_lfht_iter iter;
1195 struct ust_app *app;
1196 struct ust_app_session *ua_sess;
1197
1198 if (usess == NULL || uchan == NULL) {
1199 ERR("Adding UST global channel to NULL values");
1200 ret = -1;
1201 goto error;
1202 }
1203
1204 DBG2("UST app enabling channel %s to global domain for session uid %d",
1205 uchan->name, usess->uid);
1206
1207 rcu_read_lock();
1208
1209 /* For every registered applications */
1210 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1211 ua_sess = lookup_session_by_app(usess, app);
1212 if (ua_sess == NULL) {
1213 continue;
1214 }
1215
1216 /* Enable channel onto application */
1217 ret = enable_ust_app_channel(ua_sess, uchan, app);
1218 if (ret < 0) {
1219 /* XXX: We might want to report this error at some point... */
1220 continue;
1221 }
1222 }
1223
1224 rcu_read_unlock();
1225
1226 error:
1227 return ret;
1228 }
1229
1230 /*
1231 * For a specific UST session, create the channel for all registered apps.
1232 */
1233 int ust_app_create_channel_all(struct ltt_ust_session *usess,
1234 struct ltt_ust_channel *uchan)
1235 {
1236 int ret = 0;
1237 struct cds_lfht_iter iter;
1238 struct ust_app *app;
1239 struct ust_app_session *ua_sess;
1240 struct ust_app_channel *ua_chan;
1241
1242 if (usess == NULL || uchan == NULL) {
1243 ERR("Adding UST global channel to NULL values");
1244 ret = -1;
1245 goto error;
1246 }
1247
1248 DBG2("UST app adding channel %s to global domain for session uid %d",
1249 uchan->name, usess->uid);
1250
1251 rcu_read_lock();
1252
1253 /* For every registered applications */
1254 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1255 /* Create session on the tracer side and add it to app session HT */
1256 ua_sess = create_ust_app_session(usess, app);
1257 if (ua_sess == NULL) {
1258 continue;
1259 }
1260
1261 /* Create channel onto application */
1262 ua_chan = create_ust_app_channel(ua_sess, uchan, app);
1263 if (ua_chan == NULL) {
1264 continue;
1265 }
1266 }
1267
1268 rcu_read_unlock();
1269
1270 error:
1271 return ret;
1272 }
1273
1274 /*
1275 * For a specific UST session and UST channel, create the event for all
1276 * registered apps.
1277 */
1278 int ust_app_create_event_all(struct ltt_ust_session *usess,
1279 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
1280 {
1281 int ret = 0;
1282 struct cds_lfht_iter iter;
1283 struct cds_lfht_node *ua_chan_node;
1284 struct ust_app *app;
1285 struct ust_app_session *ua_sess;
1286 struct ust_app_channel *ua_chan;
1287 struct ust_app_event *ua_event;
1288
1289 DBG("UST app creating event %s for all apps for session uid %d",
1290 uevent->attr.name, usess->uid);
1291
1292 rcu_read_lock();
1293
1294 /* For all registered applications */
1295 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1296 struct cds_lfht_iter uiter;
1297
1298 /* Create session on the tracer side and add it to app session HT */
1299 ua_sess = create_ust_app_session(usess, app);
1300 if (ua_sess == NULL) {
1301 continue;
1302 }
1303
1304 /* Lookup channel in the ust app session */
1305 ua_chan_node = hashtable_lookup(ua_sess->channels,
1306 (void *)uchan->name, strlen(uchan->name),
1307 &uiter);
1308 if (ua_chan_node == NULL) {
1309 ERR("Channel %s not found in session uid %d. Skipping",
1310 uchan->name, usess->uid);
1311 continue;
1312 }
1313 ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node);
1314
1315 ua_event = create_ust_app_event(ua_sess, ua_chan, uevent, app);
1316 if (ua_event == NULL) {
1317 continue;
1318 }
1319 }
1320
1321 rcu_read_unlock();
1322
1323 return ret;
1324 }
1325
1326 /*
1327 * Start tracing for a specific UST session and app.
1328 */
1329 int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
1330 {
1331 int ret = 0;
1332 struct cds_lfht_iter iter;
1333 struct ust_app_session *ua_sess;
1334 struct ust_app_channel *ua_chan;
1335 struct ltt_ust_stream *ustream;
1336 int consumerd_fd;
1337
1338 DBG("Starting tracing for ust app pid %d", app->key.pid);
1339
1340 rcu_read_lock();
1341
1342 ua_sess = lookup_session_by_app(usess, app);
1343 if (ua_sess == NULL) {
1344 /* Only malloc can failed so something is really wrong */
1345 goto error_rcu_unlock;
1346 }
1347
1348 /* Upon restart, we skip the setup, already done */
1349 if (ua_sess->started) {
1350 goto skip_setup;
1351 }
1352
1353 ret = create_ust_app_metadata(ua_sess, usess->pathname, app);
1354 if (ret < 0) {
1355 goto error_rcu_unlock;
1356 }
1357
1358 /* For each channel */
1359 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1360 /* Create all streams */
1361 while (1) {
1362 /* Create UST stream */
1363 ustream = zmalloc(sizeof(*ustream));
1364 if (ustream == NULL) {
1365 PERROR("zmalloc ust stream");
1366 goto error_rcu_unlock;
1367 }
1368
1369 ret = ustctl_create_stream(app->key.sock, ua_chan->obj,
1370 &ustream->obj);
1371 if (ret < 0) {
1372 /* Got all streams */
1373 break;
1374 }
1375 ustream->handle = ustream->obj->handle;
1376
1377 /* Order is important */
1378 cds_list_add_tail(&ustream->list, &ua_chan->streams.head);
1379 ret = snprintf(ustream->pathname, PATH_MAX, "%s/%s_%u",
1380 ua_sess->path, ua_chan->name,
1381 ua_chan->streams.count++);
1382 if (ret < 0) {
1383 PERROR("asprintf UST create stream");
1384 continue;
1385 }
1386 DBG2("UST stream %d ready at %s", ua_chan->streams.count,
1387 ustream->pathname);
1388 }
1389 }
1390
1391 switch (app->bits_per_long) {
1392 case 64:
1393 consumerd_fd = ust_consumerd64_fd;
1394 break;
1395 case 32:
1396 consumerd_fd = ust_consumerd32_fd;
1397 break;
1398 default:
1399 ret = -EINVAL;
1400 goto error_rcu_unlock;
1401 }
1402
1403 /* Setup UST consumer socket and send fds to it */
1404 ret = ust_consumer_send_session(consumerd_fd, ua_sess);
1405 if (ret < 0) {
1406 goto error_rcu_unlock;
1407 }
1408 ua_sess->started = 1;
1409
1410 skip_setup:
1411 /* This start the UST tracing */
1412 ret = ustctl_start_session(app->key.sock, ua_sess->handle);
1413 if (ret < 0) {
1414 ERR("Error starting tracing for app pid: %d", app->key.pid);
1415 goto error_rcu_unlock;
1416 }
1417
1418 rcu_read_unlock();
1419
1420 /* Quiescent wait after starting trace */
1421 ustctl_wait_quiescent(app->key.sock);
1422
1423 return 0;
1424
1425 error_rcu_unlock:
1426 rcu_read_unlock();
1427 return -1;
1428 }
1429
1430 /*
1431 * Stop tracing for a specific UST session and app.
1432 */
1433 int ust_app_stop_trace(struct ltt_ust_session *usess, struct ust_app *app)
1434 {
1435 int ret = 0;
1436 struct ust_app_session *ua_sess;
1437
1438 DBG("Stopping tracing for ust app pid %d", app->key.pid);
1439
1440 rcu_read_lock();
1441
1442 ua_sess = lookup_session_by_app(usess, app);
1443 if (ua_sess == NULL) {
1444 /* Only malloc can failed so something is really wrong */
1445 goto error_rcu_unlock;
1446 }
1447
1448 #if 0 /* only useful when periodical flush will be supported */
1449 /* need to keep a handle on shm in session for this. */
1450 /* Flush all buffers before stopping */
1451 ret = ustctl_flush_buffer(usess->sock, usess->metadata->obj);
1452 if (ret < 0) {
1453 ERR("UST metadata flush failed");
1454 }
1455
1456 cds_list_for_each_entry(ustchan, &usess->channels.head, list) {
1457 ret = ustctl_flush_buffer(usess->sock, ustchan->obj);
1458 if (ret < 0) {
1459 ERR("UST flush buffer error");
1460 }
1461 }
1462 #endif
1463
1464 /* This inhibits UST tracing */
1465 ret = ustctl_stop_session(app->key.sock, ua_sess->handle);
1466 if (ret < 0) {
1467 ERR("Error stopping tracing for app pid: %d", app->key.pid);
1468 goto error_rcu_unlock;
1469 }
1470
1471 rcu_read_unlock();
1472
1473 /* Quiescent wait after stopping trace */
1474 ustctl_wait_quiescent(app->key.sock);
1475
1476 return 0;
1477
1478 error_rcu_unlock:
1479 rcu_read_unlock();
1480 return -1;
1481 }
1482
1483 /*
1484 * Destroy a specific UST session in apps.
1485 */
1486 int ust_app_destroy_trace(struct ltt_ust_session *usess, struct ust_app *app)
1487 {
1488 struct ust_app_session *ua_sess;
1489 struct lttng_ust_object_data obj;
1490 struct cds_lfht_iter iter;
1491 struct cds_lfht_node *node;
1492
1493 DBG("Destroy tracing for ust app pid %d", app->key.pid);
1494
1495 rcu_read_lock();
1496
1497 __lookup_session_by_app(usess, app, &iter);
1498 node = hashtable_iter_get_node(&iter);
1499 if (node == NULL) {
1500 /* Only malloc can failed so something is really wrong */
1501 goto error_rcu_unlock;
1502 }
1503 ua_sess = caa_container_of(node, struct ust_app_session, node);
1504 hashtable_del(app->sessions, &iter);
1505 delete_ust_app_session(app->key.sock, ua_sess);
1506 obj.handle = ua_sess->handle;
1507 obj.shm_fd = -1;
1508 obj.wait_fd = -1;
1509 obj.memory_map_size = 0;
1510 ustctl_release_object(app->key.sock, &obj);
1511
1512 rcu_read_unlock();
1513
1514 /* Quiescent wait after stopping trace */
1515 ustctl_wait_quiescent(app->key.sock);
1516
1517 return 0;
1518
1519 error_rcu_unlock:
1520 rcu_read_unlock();
1521 return -1;
1522 }
1523
1524 /*
1525 * Start tracing for the UST session.
1526 */
1527 int ust_app_start_trace_all(struct ltt_ust_session *usess)
1528 {
1529 int ret = 0;
1530 struct cds_lfht_iter iter;
1531 struct ust_app *app;
1532
1533 DBG("Starting all UST traces");
1534
1535 rcu_read_lock();
1536
1537 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1538 ret = ust_app_start_trace(usess, app);
1539 if (ret < 0) {
1540 /* Continue to next apps even on error */
1541 continue;
1542 }
1543 }
1544
1545 rcu_read_unlock();
1546
1547 return 0;
1548 }
1549
1550 /*
1551 * Start tracing for the UST session.
1552 */
1553 int ust_app_stop_trace_all(struct ltt_ust_session *usess)
1554 {
1555 int ret = 0;
1556 struct cds_lfht_iter iter;
1557 struct ust_app *app;
1558
1559 DBG("Stopping all UST traces");
1560
1561 rcu_read_lock();
1562
1563 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1564 ret = ust_app_stop_trace(usess, app);
1565 if (ret < 0) {
1566 /* Continue to next apps even on error */
1567 continue;
1568 }
1569 }
1570
1571 rcu_read_unlock();
1572
1573 return 0;
1574 }
1575
1576 /*
1577 * Destroy app UST session.
1578 */
1579 int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
1580 {
1581 int ret = 0;
1582 struct cds_lfht_iter iter;
1583 struct ust_app *app;
1584
1585 DBG("Destroy all UST traces");
1586
1587 rcu_read_lock();
1588
1589 cds_lfht_for_each_entry(ust_app_ht, &iter, app, node) {
1590 ret = ust_app_destroy_trace(usess, app);
1591 if (ret < 0) {
1592 /* Continue to next apps even on error */
1593 continue;
1594 }
1595 }
1596
1597 rcu_read_unlock();
1598
1599 return 0;
1600 }
1601
1602 /*
1603 * Add channels/events from UST global domain to registered apps at sock.
1604 */
1605 void ust_app_global_update(struct ltt_ust_session *usess, int sock)
1606 {
1607 int ret = 0;
1608 struct cds_lfht_iter iter;
1609 struct ust_app *app;
1610 struct ust_app_session *ua_sess;
1611 struct ust_app_channel *ua_chan;
1612 struct ust_app_event *ua_event;
1613
1614 if (usess == NULL) {
1615 ERR("No UST session on global update. Returning");
1616 goto error;
1617 }
1618
1619 DBG2("UST app global update for app sock %d for session uid %d", sock,
1620 usess->uid);
1621
1622 rcu_read_lock();
1623
1624 app = find_app_by_sock(sock);
1625 if (app == NULL) {
1626 ERR("Failed to update app sock %d", sock);
1627 goto error;
1628 }
1629
1630 ua_sess = create_ust_app_session(usess, app);
1631 if (ua_sess == NULL) {
1632 goto error;
1633 }
1634
1635 /*
1636 * We can iterate safely here over all UST app session sicne the create ust
1637 * app session above made a shadow copy of the UST global domain from the
1638 * ltt ust session.
1639 */
1640 cds_lfht_for_each_entry(ua_sess->channels, &iter, ua_chan, node) {
1641 ret = create_ust_channel(app, ua_sess, ua_chan);
1642 if (ret < 0) {
1643 /* FIXME: Should we quit here or continue... */
1644 continue;
1645 }
1646
1647 /* For each events */
1648 cds_lfht_for_each_entry(ua_chan->events, &iter, ua_event, node) {
1649 ret = create_ust_event(app, ua_sess, ua_chan, ua_event);
1650 if (ret < 0) {
1651 /* FIXME: Should we quit here or continue... */
1652 continue;
1653 }
1654 }
1655 }
1656
1657 if (usess->start_trace) {
1658 ret = ust_app_start_trace(usess, app);
1659 if (ret < 0) {
1660 goto error;
1661 }
1662
1663 DBG2("UST trace started for app pid %d", app->key.pid);
1664 }
1665
1666 error:
1667 rcu_read_unlock();
1668 return;
1669 }
This page took 0.109286 seconds and 3 git commands to generate.